I'm trying to use Codeception/AspectMock in Yii2 tests. I have two classes:
One:
namespace backend\baseModel;
class BaseModel
{
public static function getId()
{
return 9;
}
}
Second:
use \backend\baseModel\BaseModel;
class ModelA extends BaseModel
{
public static function getId()
{
return 5;
}
}
I am trying to test ModelA:
class TestModel extends \Codeception\Test\Unit
{
public function testGetId()
{
test::double(ModelA::getId(), ['getId' => 7]);
$this->assertSame(7, ModelA::getId());
}
}
I will do the test with an error:
1) TestMode: Get id
Test tests/unit/TestModelTest.php:testGetId
[InvalidArgumentException] Class backend\baseModel\BaseModel was not found by locator
#1 /var/www/vendor/goaop/parser-reflection/src/ReflectionEngine.php:125
#2 /var/www/vendor/goaop/parser-reflection/src/ReflectionEngine.php:140
#3 /var/www/vendor/goaop/parser-reflection/src/ReflectionClass.php:44
#4 /var/www/vendor/goaop/parser-reflection/src/ReflectionClass.php:150
#5 /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:446
#6 /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:924
#7 /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:317
#8 /var/www/vendor/goaop/parser-reflection/src/Traits/ReflectionClassLikeTrait.php:303
#9 /var/www/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:121
#10 /var/www/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:78
My configuration file:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', __DIR__.'/../../');
require_once(YII_APP_BASE_PATH . '/vendor/autoload.php');
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'cacheDir' => __DIR__ . '/_data/cache',
'includePaths' => [__DIR__.'/../../backend'],
]);
$kernel->loadFile(YII_APP_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php');
I tried the solution proposed here (in the first answer): Codeception/AspectMock Parent class not found by locator
The error stops appearing, but then the getId () method returns 5 instead of 7.
You have static method and you override getId function in ModelA, and in test you are invoking ModelA class, so it should return 5 in you case.