Function parameters initialized with a class constant throw an exception

Open
#118 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
testing-qa

Research direction

Start by running tests/AspectMockTest.php with the bootstrap and UserModel.php example from the issue. Trace the failure through BeforeMockTransformer.php and the goaop parser-reflection stack, especially NodeExpressionResolver and ReflectionEngine. Done means the testDouble example runs without the PDO locator exception while preserving the class-constant default parameter.

Written by the indexing model from the issue text.

Description

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 ?

Dominant language
PHP
Stars
784
Forks
130
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Codeception/AspectMock

All issues in Codeception/AspectMock

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.