test::func doesn't work I expected
sapsaldog opened this issue · 1 comments
sapsaldog commented
I am not sure I just misunderstand how to use "test:func"
but those tests are not working as I expect
<?php
namespace NDS\LeapMarketing\Util\FileFunctions;
class TestClass
{
public static function test(string $target)
{
return strlen($target);
}
}
<?php
namespace demo;
use AspectMock\Test as test;
use NDS\LeapMarketing\Util\FileFunctions\TestClass;
class AspectMockTest extends \PHPUnit\Framework\TestCase
{
protected function tearDown(): void
{
test::clean(); // remove all registered test doubles
}
public function testFunc0()
{
$this->assertEquals(TestClass::test('haha'), 4);
}
public function testFunc1()
{
test::func('NDS\LeapMarketing\Util\FileFunctions', 'strlen', 10);
$this->assertEquals(TestClass::test('haha'), 10);
}
public function testFunc2()
{
test::func('NDS\LeapMarketing\Util\FileFunctions', 'strlen', 3);
$this->assertEquals(TestClass::test('haha'), 3);
}
}
The result of the tests is like below
PHPUnit 9.5.21 #StandWithUkraine
.FF 3 / 3 (100%)
Time: 00:00.104, Memory: 8.00 MB
There were 2 failures:
1) demo\AspectMockTest::testFunc1
Failed asserting that 10 matches expected 4.
/mnt/devvm/projects/leaf-marketing/tests/unit/Util/FileFunctions/AspectMockTest.php:25
phpvfscomposer:///mnt/devvm/projects/leaf-marketing/vendor/phpunit/phpunit/phpunit:97
2) demo\AspectMockTest::testFunc2
Failed asserting that 3 matches expected 4.
/mnt/devvm/projects/leaf-marketing/tests/unit/Util/FileFunctions/AspectMockTest.php:32
phpvfscomposer:///mnt/devvm/projects/leaf-marketing/vendor/phpunit/phpunit/phpunit:97
FAILURES!
Tests: 3, Assertions: 3, Failures: 2.
if I move the testFunc0 to the bottom, the test does work.
sapsaldog commented
I think I found a workaround.
I guess this is because of the initial loading mechanism.
add this function to a PHPUnit test class
protected function setUp(): void
{
test::func('\NDS\LeapMarketing\Util\FileFunctions', 'strlen', function($target){
return strlen($target);
});
}