Skip to content

Commit 9015d06

Browse files
committed
bugfix of SystemChange
1 parent cde66e4 commit 9015d06

File tree

22 files changed

+488
-23
lines changed

22 files changed

+488
-23
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ vendor/
33
!.editorconfig
44
!.gitignore
55
!.php-cs-fixer.dist.php
6-
tests/Unit/Extension/SystemSnapshot/Reader/db-*.sqlite
6+
tests/Unit/Extension/SystemSnapshot/Reader/db-*.sqlite
7+
tests/Integration/app/db.sqlite

phpunit.xml

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<directory>tests/Functional</directory>
99
</testsuite>
1010

11+
<testsuite name="Integration">
12+
<directory>tests/Integration</directory>
13+
</testsuite>
14+
1115
<testsuite name="Cli">
1216
<directory>tests/Functional/Cli</directory>
1317
</testsuite>

src/Extension/SystemSnapshot.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Runner\BeforeTestHook;
88
use ReflectionClass;
99
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\Reader\ReaderInterface;
10-
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SystemSnapshotInterface;
10+
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SnapshotsPerTestsInterface;
1111
use ThenLabs\PyramidalTests\Model\Record;
1212
use ThenLabs\PyramidalTests\Model\TestCaseModel;
1313

@@ -78,11 +78,11 @@ protected function getTestCaseModelDataFromTestName(string $testName): ?array
7878
return null;
7979
}
8080

81-
protected function implementsSystemSnapshotInterface(TestCaseModel $testCaseModel): bool
81+
protected function implementsSnapshotsPerTestsInterface(TestCaseModel $testCaseModel): bool
8282
{
8383
$class = new ReflectionClass($testCaseModel->getClassBuilder()->getFCQN());
8484

85-
return $class->implementsInterface(SystemSnapshotInterface::class);
85+
return $class->implementsInterface(SnapshotsPerTestsInterface::class);
8686
}
8787

8888
public static function addDiffExpectation(string $className, array $expectations): void
@@ -102,7 +102,7 @@ public function executeBeforeTest(string $test): void
102102
[$testCaseModel, $className] = $this->getTestCaseModelDataFromTestName($test);
103103

104104
if (! $testCaseModel ||
105-
! $this->implementsSystemSnapshotInterface($testCaseModel)
105+
! $this->implementsSnapshotsPerTestsInterface($testCaseModel)
106106
) {
107107
return;
108108
}
@@ -119,7 +119,7 @@ public function executeAfterTest(string $test, float $time): void
119119
[$testCaseModel, $className, $methodName] = $this->getTestCaseModelDataFromTestName($test);
120120

121121
if (! $testCaseModel ||
122-
! $this->implementsSystemSnapshotInterface($testCaseModel)
122+
! $this->implementsSnapshotsPerTestsInterface($testCaseModel)
123123
) {
124124
return;
125125
}

src/Extension/SystemSnapshot/Decorator/ExpectSystemChangeDecorator.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace ThenLabs\PyramidalTests\Extension\SystemSnapshot\Decorator;
55

6-
use Closure;
76
use ThenLabs\PyramidalTests\Decorator\AbstractDecorator;
87
use ThenLabs\PyramidalTests\DSL\DSL;
98
use ThenLabs\PyramidalTests\Model\TestCaseModel;
@@ -13,12 +12,6 @@
1312
*/
1413
class ExpectSystemChangeDecorator extends AbstractDecorator
1514
{
16-
public function getClosure(array $arguments): ?Closure
17-
{
18-
return function () {
19-
};
20-
}
21-
2215
public function applyTo(TestCaseModel $testCaseModel, array $arguments)
2316
{
2417
$firstArgument = $arguments[0];
@@ -34,6 +27,8 @@ public function applyTo(TestCaseModel $testCaseModel, array $arguments)
3427
$expectations = $firstArgument;
3528
}
3629

30+
$testCaseModel->addDiffExpectationsForSystemSnapshot($expectations);
31+
3732
$closure = function () use ($expectations) {
3833
$this->expectSystemChange($expectations);
3934

src/Extension/SystemSnapshot/ExpectSystemChangeTrait.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
namespace ThenLabs\PyramidalTests\Extension\SystemSnapshot;
55

6+
use PHPUnit\Framework\TestCase;
67
use ThenLabs\PyramidalTests\Extension\SystemSnapshot;
8+
use ThenLabs\PyramidalTests\Model\Record;
79

810
/**
911
* @author Andy Daniel Navarro Taño <[email protected]>
@@ -12,8 +14,33 @@ trait ExpectSystemChangeTrait
1214
{
1315
public function expectSystemChange(array $expectations): void
1416
{
15-
$className = get_class($this);
17+
if (! $this instanceof TestCase) {
18+
return;
19+
}
1620

17-
SystemSnapshot::addDiffExpectation($className, $expectations);
21+
$thisClass = get_class($this);
22+
23+
if ($this instanceof SnapshotsPerTestsInterface) {
24+
SystemSnapshot::addDiffExpectation($thisClass, $expectations);
25+
} /* elseif ($this instanceof SnapshotsInDecoratorsInterface) {
26+
$aux = function ($testCaseModel) use ($thisClass, $expectations, &$aux) {
27+
$testCaseClass = $testCaseModel->getClassBuilder()->getFCQN();
28+
29+
if ($thisClass === $testCaseClass) {
30+
$testCaseModel->addDiffExpectationsForSystemSnapshot($expectations);
31+
return true;
32+
} else {
33+
foreach ($testCaseModel->children() as $child) {
34+
$aux($child);
35+
}
36+
}
37+
};
38+
39+
foreach (Record::getAllTestCaseModels() as $rootTestCaseModel) {
40+
if (true === $aux($rootTestCaseModel)) {
41+
return;
42+
}
43+
}
44+
} */
1845
}
1946
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ThenLabs\PyramidalTests\Extension\SystemSnapshot;
5+
6+
/**
7+
* @author Andy Daniel Navarro Taño <[email protected]>
8+
*/
9+
interface SnapshotsInDecoratorsInterface
10+
{
11+
}

src/Extension/SystemSnapshot/SystemSnapshotInterface.php src/Extension/SystemSnapshot/SnapshotsPerTestsInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
/**
77
* @author Andy Daniel Navarro Taño <[email protected]>
88
*/
9-
interface SystemSnapshotInterface
9+
interface SnapshotsPerTestsInterface
1010
{
1111
}

src/Model/TestCaseModel.php

+126-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use ThenLabs\PyramidalTests\Annotation\ImportDecorators;
1616
use ThenLabs\PyramidalTests\Decorator\DecoratorsRegistry;
1717
use ThenLabs\PyramidalTests\Decorator\PackageInterface as DecoratorPackageInterface;
18+
use ThenLabs\PyramidalTests\Extension\SystemSnapshot;
19+
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SnapshotsInDecoratorsInterface;
1820

1921
AnnotationRegistry::registerFile(__DIR__.'/../Annotation/Decorator.php');
2022
AnnotationRegistry::registerFile(__DIR__.'/../Annotation/ImportDecorators.php');
@@ -61,6 +63,16 @@ class TestCaseModel extends AbstractModel implements CompositeComponentInterface
6163
*/
6264
protected $invokedSetUpBeforeClass = false;
6365

66+
/**
67+
* @var array
68+
*/
69+
protected $systemSnapshotBeforeFirstSetUpBeforeClassDecorator = [];
70+
71+
/**
72+
* @var bool
73+
*/
74+
protected $includedSetUpBeforeClassDecoratorForSystemSnapshot = false;
75+
6476
/**
6577
* @var Closure
6678
*/
@@ -86,6 +98,11 @@ class TestCaseModel extends AbstractModel implements CompositeComponentInterface
8698
*/
8799
protected $tearDownAfterClassClosure;
88100

101+
/**
102+
* @var array<string, Closure>
103+
*/
104+
protected $tearDownAfterClassDecorators = [];
105+
89106
/**
90107
* @var bool
91108
*/
@@ -96,11 +113,26 @@ class TestCaseModel extends AbstractModel implements CompositeComponentInterface
96113
*/
97114
protected $invokedTearDownAfterClass = false;
98115

116+
/**
117+
* @var array
118+
*/
119+
protected $systemSnapshotAfterLastTearDownAfterClassDecorator = [];
120+
121+
/**
122+
* @var bool
123+
*/
124+
protected $includedTearDownAfterClassDecoratorForSystemSnapshot = false;
125+
99126
/**
100127
* @var array
101128
*/
102129
protected $macros = [];
103130

131+
/**
132+
* @var array
133+
*/
134+
protected $diffExpectationsForSystemSnapshot = [];
135+
104136
public function __construct(string $title, Closure $closure)
105137
{
106138
parent::__construct($title, $closure);
@@ -141,7 +173,7 @@ public function buildClass(): void
141173

142174
if (count($setUpBeforeClassDecorators)) {
143175
$this->setUpBeforeClassClosure = function () use ($setUpBeforeClassDecorators, $currentSetUpBeforeClassClosure) {
144-
foreach ($setUpBeforeClassDecorators as $title => $setUpBeforeClassDecorator) {
176+
foreach ($setUpBeforeClassDecorators as $setUpBeforeClassDecorator) {
145177
$setUpBeforeClassDecorator();
146178
}
147179

@@ -227,6 +259,21 @@ public function buildClass(): void
227259
}
228260

229261
// tearDownAfterClass
262+
$tearDownAfterClassDecorators = $this->tearDownAfterClassDecorators;
263+
$currentTearDownAfterClassClosure = $this->tearDownAfterClassClosure;
264+
265+
if (count($tearDownAfterClassDecorators)) {
266+
$this->tearDownAfterClassClosure = function () use ($tearDownAfterClassDecorators, $currentTearDownAfterClassClosure) {
267+
foreach ($tearDownAfterClassDecorators as $tearDownAfterClassDecorator) {
268+
$tearDownAfterClassDecorator();
269+
}
270+
271+
if ($currentTearDownAfterClassClosure instanceof Closure) {
272+
$currentTearDownAfterClassClosure();
273+
}
274+
};
275+
}
276+
230277
if ($this->tearDownAfterClassClosure instanceof Closure) {
231278
$tearDownAfterClassClosure = $this->tearDownAfterClassClosure;
232279

@@ -446,6 +493,33 @@ public function __call($decoratorName, $arguments)
446493
throw new Exception("Decorator '{$decoratorName}' for class '{$this->baseClassBuilder->getParentClass()}' is missing.");
447494
}
448495

496+
if ($this->hasSnapshotsInDecoratorsInterface()) {
497+
if (! $this->includedSetUpBeforeClassDecoratorForSystemSnapshot) {
498+
$setUpBeforeClassDecoratorForSystemSnapshot = function () use ($thisTestCaseModel) {
499+
$thisTestCaseModel->setSystemSnapshotBeforeFirstSetUpBeforeClassDecorator(SystemSnapshot::getSnapshot());
500+
};
501+
502+
$this->setUpBeforeClassDecorators[] = $setUpBeforeClassDecoratorForSystemSnapshot;
503+
$this->includedSetUpBeforeClassDecoratorForSystemSnapshot = true;
504+
}
505+
506+
if (! $this->includedTearDownAfterClassDecoratorForSystemSnapshot) {
507+
$tearDownAfterClassDecoratorForSystemSnapshot = function () use ($thisTestCaseModel) {
508+
$systemSnapshot = SystemSnapshot::getSnapshot();
509+
$thisTestCaseModel->setSystemSnapshotAfterLastTearDownAfterClassDecorator($systemSnapshot);
510+
511+
SystemSnapshot\Assert::assertExpectedArrayDiff(
512+
$thisTestCaseModel->getSystemSnapshotBeforeFirstSetUpBeforeClassDecorator(),
513+
$thisTestCaseModel->getSystemSnapshotAfterLastTearDownAfterClassDecorator(),
514+
$thisTestCaseModel->getDiffExpectationsForSystemSnapshot()
515+
);
516+
};
517+
518+
$this->tearDownAfterClassDecorators[] = $tearDownAfterClassDecoratorForSystemSnapshot;
519+
$this->includedTearDownAfterClassDecoratorForSystemSnapshot = true;
520+
}
521+
}
522+
449523
$setUpBeforeClassDecorator = $decorator->getClosure($arguments);
450524

451525
if ($setUpBeforeClassDecorator instanceof Closure) {
@@ -525,4 +599,55 @@ public function addExecutedDecorator(string $title): void
525599
{
526600
$this->executedDecorators[] = $title;
527601
}
602+
603+
public function hasSnapshotsInDecoratorsInterface(): bool
604+
{
605+
$interfaceNames = [
606+
...$this->classBuilder->getInterfaces(),
607+
...$this->baseClassBuilder->getInterfaces(),
608+
];
609+
610+
foreach ($interfaceNames as $interfaceName) {
611+
if ($interfaceName === SnapshotsInDecoratorsInterface::class) {
612+
return true;
613+
}
614+
}
615+
616+
$reflection = new ReflectionClass($this->baseClassBuilder->getParentClass());
617+
618+
return $reflection->implementsInterface(SnapshotsInDecoratorsInterface::class);
619+
}
620+
621+
public function setSystemSnapshotBeforeFirstSetUpBeforeClassDecorator(array $systemSnapshot): void
622+
{
623+
$this->systemSnapshotBeforeFirstSetUpBeforeClassDecorator = $systemSnapshot;
624+
}
625+
626+
public function getSystemSnapshotBeforeFirstSetUpBeforeClassDecorator(): array
627+
{
628+
return $this->systemSnapshotBeforeFirstSetUpBeforeClassDecorator;
629+
}
630+
631+
public function setSystemSnapshotAfterLastTearDownAfterClassDecorator(array $systemSnapshot): void
632+
{
633+
$this->systemSnapshotAfterLastTearDownAfterClassDecorator = $systemSnapshot;
634+
}
635+
636+
public function getSystemSnapshotAfterLastTearDownAfterClassDecorator(): array
637+
{
638+
return $this->systemSnapshotAfterLastTearDownAfterClassDecorator;
639+
}
640+
641+
public function addDiffExpectationsForSystemSnapshot(array $expectations): void
642+
{
643+
$this->diffExpectationsForSystemSnapshot = array_merge_recursive(
644+
$this->diffExpectationsForSystemSnapshot,
645+
$expectations
646+
);
647+
}
648+
649+
public function getDiffExpectationsForSystemSnapshot(): array
650+
{
651+
return $this->diffExpectationsForSystemSnapshot;
652+
}
528653
}

tests/Functional/SystemSnapshot/projects/project2/tests/test-1.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use PHPUnit\Framework\TestCase;
44
use ThenLabs\PyramidalTests\Extension\SystemSnapshot;
55
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\Reader\ReaderInterface;
6-
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SystemSnapshotInterface;
6+
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SnapshotsPerTestsInterface;
77

88
SystemSnapshot::addReader('reader1', new class implements ReaderInterface {
99
public function getSnapshot(): array
@@ -16,7 +16,7 @@ public function getSnapshot(): array
1616
}
1717
});
1818

19-
class MyTestCase extends TestCase implements SystemSnapshotInterface
19+
class MyTestCase extends TestCase implements SnapshotsPerTestsInterface
2020
{
2121
}
2222

tests/Functional/SystemSnapshot/projects/project3/tests/test-1.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use ThenLabs\PyramidalTests\Extension\SystemSnapshot;
55
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\ExpectSystemChangeTrait;
66
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\Reader\ReaderInterface;
7-
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SystemSnapshotInterface;
7+
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SnapshotsPerTestsInterface;
88

99
SystemSnapshot::addReader('reader1', new class implements ReaderInterface {
1010
public function getSnapshot(): array
@@ -17,7 +17,7 @@ public function getSnapshot(): array
1717
}
1818
});
1919

20-
class MyTestCase extends TestCase implements SystemSnapshotInterface
20+
class MyTestCase extends TestCase implements SnapshotsPerTestsInterface
2121
{
2222
use ExpectSystemChangeTrait;
2323
}

tests/Functional/SystemSnapshot/projects/project4/tests/test-1.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\Decorators;
66
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\ExpectSystemChangeTrait;
77
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\Reader\ReaderInterface;
8-
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SystemSnapshotInterface;
8+
use ThenLabs\PyramidalTests\Extension\SystemSnapshot\SnapshotsPerTestsInterface;
99

1010
SystemSnapshot::addReader('reader1', new class implements ReaderInterface {
1111
public function getSnapshot(): array
@@ -16,7 +16,7 @@ public function getSnapshot(): array
1616
}
1717
});
1818

19-
class MyTestCase extends TestCase implements SystemSnapshotInterface
19+
class MyTestCase extends TestCase implements SnapshotsPerTestsInterface
2020
{
2121
use ExpectSystemChangeTrait;
2222
}

0 commit comments

Comments
 (0)