Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve ResultAggregator from TestInterface #6587

Closed
yesdevnull opened this issue Oct 18, 2022 · 0 comments · Fixed by #6590
Closed

Unable to retrieve ResultAggregator from TestInterface #6587

yesdevnull opened this issue Oct 18, 2022 · 0 comments · Fixed by #6590

Comments

@yesdevnull
Copy link
Contributor

What are you trying to achieve?

To access the ResultAggregator from within the _failed method in a Codeception module.

What do you get instead?

php vendor/bin/codecept run Unit -vvv
In TestCaseWrapper.php line 86:

  [LogicException]             
  ResultAggregator is not set  

Both Codeception\Test\Test and Codeception\Test\TestCaseWrapper (which extends Test) have the private property $resultAggregator on them so the following code:

class FailureHelper extends \Codeception\Module
{
    public function _failed(TestInterface $test, Exception $fail)
    {
        codecept_debug((array) $test);
    }
}

Generates the output (values have been truncated):

^ array:11 [
  "\x00Codeception\Test\Test\x00resultAggregator" => Codeception\ResultAggregator^ {#196}
  "\x00Codeception\Test\Test\x00ignored" => false
  "\x00Codeception\Test\Test\x00assertionCount" => 0
  "\x00Codeception\Test\Test\x00eventDispatcher" => Symfony\Component\EventDispatcher\EventDispatcher {#197 …3}
  "\x00*\x00hooks" => array:4 []
  "\x00*\x00reportUselessTests" => false
  "\x00Codeception\Test\Test\x00collectCodeCoverage" => false
  "\x00*\x00numAssertions" => 0
  "\x00Codeception\Test\TestCaseWrapper\x00metadata" => Codeception\Test\Metadata {#661}
  "\x00Codeception\Test\TestCaseWrapper\x00resultAggregator" => null
  "\x00Codeception\Test\TestCaseWrapper\x00testCase" => Tests\Unit\MyTest {#654}
]

Note the duplicate keys for resultAggregator but with different class prefixes.

I have a very hacky way of retrieving the ResultAggregator using the following:

$reflected = new \ReflectionClass($test);
$reflectedIterable = $reflected;
while ($parentReflected = $reflectedIterable->getParentClass()) {
    if (Test::class === $parentReflected->getName()) {
        $privateProperty = $parentReflected->getProperty('resultAggregator');
        $result = $privateProperty->getValue($test);
    }

    $reflectedIterable = $parentReflected;
}
// result should be set now - check it's not null before using
$result->shouldStop(); // etc

Demo repo

This demo repo contains an out-of-the-box installation of Codeception 5 (with extraneous suites removed) and a basic helper which demonstrates the bug.

Demo repository, in particular the FailureHelper.

Details

  • Codeception version: 5.0.3
  • PHP Version: 8.1.11
  • Operating System: macOS and CentOS/RHEL
  • Installation type: Composer
  • List of installed packages: composer.json and composer.lock
  • Suite configuration: Unit.suite.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant