Skip to content

Commit

Permalink
Fix conditional assertions in Unit format
Browse files Browse the repository at this point in the history
  • Loading branch information
Naktibalda committed Dec 3, 2022
1 parent 3f4991d commit 18c5416
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Codeception/SuiteManager.php
Expand Up @@ -216,8 +216,9 @@ protected function isExecutedInCurrentEnvironment(TestInterface $test): bool

protected function configureTest(TestInterface $test): void
{
$di = clone($this->di);
$test->getMetadata()->setServices([
'di' => clone($this->di),
'di' => $di,
'dispatcher' => $this->dispatcher,
'modules' => $this->moduleContainer
]);
Expand All @@ -227,9 +228,11 @@ protected function configureTest(TestInterface $test): void
'modules' => $this->moduleContainer->all()
]);
if ($test instanceof TestCaseWrapper) {
$di->set(new Scenario($test));

$testCase = $test->getTestCase();
if ($testCase instanceof Unit) {
$this->configureTest($testCase);
$testCase->setMetadata($test->getMetadata());
}
}
if ($test instanceof ScenarioDriven) {
Expand Down
7 changes: 5 additions & 2 deletions src/Codeception/Test/Unit.php
Expand Up @@ -40,6 +40,11 @@ public function getMetadata(): Metadata
return $this->metadata;
}

public function setMetadata(?Metadata $metadata): void
{
$this->metadata = $metadata;
}

public function getResultAggregator(): ResultAggregator
{
throw new \LogicException('This method should not be called, TestCaseWrapper class must be used instead');
Expand All @@ -59,8 +64,6 @@ protected function _setUp()

/** @var Di $di */
$di = $this->getMetadata()->getService('di');
$di->set(new Scenario($this));

// auto-inject $tester property
if (($this->getMetadata()->getCurrent('actor')) && ($property = lcfirst(Configuration::config()['actor_suffix']))) {
$this->$property = $di->instantiate($this->getMetadata()->getCurrent('actor'));
Expand Down
12 changes: 12 additions & 0 deletions tests/cli/OrderCest.php
Expand Up @@ -46,6 +46,18 @@ public function checkForCanCantFailsInCest(CliGuy $I)
$I->seeFileContentsEqual("BIB([TFT][TFT])");
}

public function checkForCanCantFailsInTest(CliGuy $I)
{
$I->amInPath('tests/data/sandbox');
$I->executeCommand('run order CanCantFailTest.php --no-exit');
$I->seeFileFound('order.txt', 'tests/_output');
$I->expect(
'global bootstrap, initialization, beforeSuite, before, bootstrap, test,'
. ' fail, fail, test, test, fail, fail, test, after, afterSuite'
);
$I->seeFileContentsEqual("BIB([TFT][TFT])");
}

public function checkSimpleFiles(CliGuy $I)
{
$I->amInPath('tests/data/sandbox');
Expand Down
26 changes: 26 additions & 0 deletions tests/data/claypit/tests/order/CanCantFailTest.php
@@ -0,0 +1,26 @@
<?php

namespace Order;

use OrderGuy;

class CanCantFailTest extends \Codeception\Test\Unit
{
protected OrderGuy $tester;

public function testOne()
{
$I = $this->tester;
$I->appendToFile('T');
$I->canSeeFailNow();
$I->appendToFile('T');
}

public function testTwo()
{
$I = $this->tester;
$I->appendToFile('T');
$I->canSeeFailNow();
$I->appendToFile('T');
}
}

0 comments on commit 18c5416

Please sign in to comment.