Codeception / AspectMock

The most powerful and flexible mocking framework for PHPUnit / Codeception.
MIT License
790 stars 129 forks source link

Function parameters initialized with a class constant throw an exception #118

Open micoli opened 7 years ago

micoli commented 7 years ago

Hello,

In the following code :

<?php
// test/bootstrap.php
require __DIR__ . '/../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
        'debug' => true,
        'includePaths' => [
                __DIR__ . '/../src',
                'excludePaths' => [
                        __DIR__ . '/../tests'
                ]
        ]
]);
?>

<?php
// src/UserModel.php

namespace AM\Demo;

class UserModel {

     public function save()
     {
         return("Save is not mocking\n");
     }

     public function get($id, $type = \PDO::FETCH_ASSOC)
     {
     }

}
?>

<?php
// tests/AspectMockTest.php

use AspectMock\Test as test;
use AM\Demo\UserModel;

class AspectMockTest extends \PHPUnit_Framework_TestCase
{
        public function testDouble()
        {
           $mock = test::double('AM\Demo\UserModel', ['save' => "save is mocking"]);
        }

        protected function tearDown()
        {
          test::clean(); // remove all registered test doubles
        }
}

when we launch the test we get the following exception :

PHPUnit 5.7.3 by Sebastian Bergmann and contributors.

Starting test 'AspectMockTest::testDouble'.
E                                                                   1 / 1 (100%)

Time: 1.6 seconds, Memory: 24.75MB

There was 1 error:

1) AspectMockTest::testDouble
InvalidArgumentException: Class PDO was not found by locator

/testgo/vendor/goaop/parser-reflection/src/ReflectionEngine.php:112
/testgo/vendor/goaop/parser-reflection/src/ReflectionEngine.php:127
/testgo/vendor/goaop/parser-reflection/src/ReflectionClass.php:45
/testgo/vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:301
/testgo/vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:259
/testgo/vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:118
/testgo/vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:99
/testgo/vendor/goaop/parser-reflection/src/ReflectionParameter.php:99
/testgo/vendor/goaop/parser-reflection/src/Traits/ReflectionFunctionLikeTrait.php:164
/testgo/vendor/codeception/aspect-mock/src/AspectMock/Intercept/BeforeMockTransformer.php:56
/testgo/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:124
/testgo/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:83
/testgo/vendor/goaop/framework/src/Instrument/ClassLoading/SourceTransformingLoader.php:134
/testgo/vendor/goaop/framework/src/Instrument/ClassLoading/SourceTransformingLoader.php:101
/testgo/vendor/goaop/framework/src/Instrument/ClassLoading/AopComposerLoader.php:136
/testgo/vendor/goaop/framework/src/Instrument/ClassLoading/AopComposerLoader.php:136
/testgo/vendor/codeception/aspect-mock/src/AspectMock/Test.php:97
/testgo/tests/AspectMockOrmTest.php:53

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Do you have any solution or fix to help us please ?

nsekeras commented 6 years ago

I have the same problem, my unit tests sending me messages [InvalidArgumentException] Class ... was not found by locator

1 ......../vendor/goaop/parser-reflection/src/ReflectionEngine.php:125

2 ......../vendor/goaop/parser-reflection/src/ReflectionEngine.php:140

3 ......../vendor/goaop/parser-reflection/src/ReflectionClass.php:44

4 ......../vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:478

5 ......../vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:271

6 ......../vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:117

7 ......../vendor/goaop/parser-reflection/src/ValueResolver/NodeExpressionResolver.php:99

8 ......../vendor/goaop/parser-reflection/src/ReflectionParameter.php:100

9 ......../vendor/goaop/parser-reflection/src/Traits/ReflectionFunctionLikeTrait.php:163

10 ......../vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php:121

That class contains the constant I pass inside argument by default in some other function, and class, containing this function, I am trying to use inside my unit test. The issue is inside a transformer BeforeMockTransformer, line

$reflectedParams = $method->getParameters();

Yii2 Framework "codeception/aspect-mock": "3.0.1"

puniserv commented 6 years ago

I had this same problem. It was happening, when autoload function was loading parent class again by autoload function. Yii autoload function was not initializing, only composer.

I stopped using yii namespaces (namespace with app, for example app\models\User) and registered new composer psr-4 namespaces. Now it works!