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

Fix conditional assertions in Unit format #6610

Merged
merged 1 commit into from Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
}
}