Skip to content

Commit af602d3

Browse files
committed
Added test case for "Environment"
1 parent d52d3b3 commit af602d3

File tree

7 files changed

+264
-19
lines changed

7 files changed

+264
-19
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Functional\Kernel\Environment;
4+
5+
use Okapi\CodeTransformer\Tests\ClassLoaderMockTrait;
6+
use Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Kernel\DevelopmentEnvironmentKernel;
7+
use Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Kernel\ProductionEnvironmentKernel;
8+
use Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Target\HelloWorld;
9+
use Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Transformer\StringTransformer;
10+
use Okapi\CodeTransformer\Tests\Util;
11+
use Okapi\Filesystem\Filesystem;
12+
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
13+
use PHPUnit\Framework\TestCase;
14+
use Throwable;
15+
16+
#[RunTestsInSeparateProcesses]
17+
class EnvironmentTest extends TestCase
18+
{
19+
use ClassLoaderMockTrait;
20+
21+
/**
22+
* @see StringTransformer::transform()
23+
*/
24+
public function testDevelopmentEnvironment(): void
25+
{
26+
Util::clearCache();
27+
DevelopmentEnvironmentKernel::init();
28+
29+
$class = HelloWorld::class;
30+
$this->assertWillBeTransformed($class);
31+
32+
$helloWorldClass = new $class();
33+
34+
$this->assertSame(
35+
'Hello from Code Transformer!',
36+
$helloWorldClass->say(),
37+
);
38+
}
39+
40+
/**
41+
* @see StringTransformer::transform()
42+
*/
43+
public function testCachedDevelopmentEnvironment(): void
44+
{
45+
DevelopmentEnvironmentKernel::init();
46+
47+
$class = HelloWorld::class;
48+
$this->assertTransformerLoadedFromCache($class);
49+
50+
$helloWorldClass = new $class();
51+
52+
$this->assertSame(
53+
'Hello from Code Transformer!',
54+
$helloWorldClass->say(),
55+
);
56+
}
57+
58+
/**
59+
* @see StringTransformer::transform()
60+
*/
61+
public function testChangedDevelopmentEnvironment(): void
62+
{
63+
// Change class
64+
$classFilePath = Util::getFilePath(HelloWorld::class);
65+
$originalClassFileContent = Filesystem::readFile($classFilePath);
66+
67+
$changedFileContent = str_replace(
68+
'Hello World!',
69+
'Hello Changed World!',
70+
$originalClassFileContent,
71+
);
72+
73+
sleep(1);
74+
Filesystem::writeFile($classFilePath, $changedFileContent);
75+
76+
$exception = null;
77+
try {
78+
DevelopmentEnvironmentKernel::init();
79+
80+
$class = HelloWorld::class;
81+
$this->assertWillBeTransformed($class);
82+
83+
$classInstance = new $class();
84+
$this->assertSame(
85+
'Hello Changed World from Code Transformer!',
86+
$classInstance->say(),
87+
);
88+
} catch (Throwable $e) {
89+
$exception = $e;
90+
}
91+
92+
// Restore class
93+
Filesystem::writeFile($classFilePath, $originalClassFileContent);
94+
95+
if ($exception !== null) {
96+
/** @noinspection PhpUnhandledExceptionInspection */
97+
throw $exception;
98+
}
99+
}
100+
101+
/**
102+
* @see StringTransformer::transform()
103+
*/
104+
public function testProductionEnvironment(): void
105+
{
106+
Util::clearCache();
107+
ProductionEnvironmentKernel::init();
108+
109+
$class = HelloWorld::class;
110+
$this->assertWillBeTransformed($class);
111+
112+
$helloWorldClass = new $class();
113+
114+
$this->assertSame(
115+
'Hello from Code Transformer!',
116+
$helloWorldClass->say(),
117+
);
118+
}
119+
120+
/**
121+
* @see StringTransformer::transform()
122+
*/
123+
public function testCachedProductionEnvironment(): void
124+
{
125+
ProductionEnvironmentKernel::init();
126+
127+
$class = HelloWorld::class;
128+
$this->assertTransformerLoadedFromCache($class);
129+
130+
$helloWorldClass = new $class();
131+
132+
$this->assertSame(
133+
'Hello from Code Transformer!',
134+
$helloWorldClass->say(),
135+
);
136+
}
137+
138+
/**
139+
* @see StringTransformer::transform()
140+
*/
141+
public function testChangedProductionEnvironment(): void
142+
{
143+
// Change class
144+
$classFilePath = Util::getFilePath(HelloWorld::class);
145+
$originalClassFileContent = Filesystem::readFile($classFilePath);
146+
147+
$changedFileContent = str_replace(
148+
'Hello World!',
149+
'Hello Changed World!',
150+
$originalClassFileContent,
151+
);
152+
153+
sleep(1);
154+
Filesystem::writeFile($classFilePath, $changedFileContent);
155+
156+
$exception = null;
157+
try {
158+
ProductionEnvironmentKernel::init();
159+
160+
$class = HelloWorld::class;
161+
$this->assertTransformerLoadedFromCache($class);
162+
163+
$classInstance = new $class();
164+
$this->assertSame(
165+
'Hello from Code Transformer!',
166+
$classInstance->say(),
167+
);
168+
} catch (Throwable $e) {
169+
$exception = $e;
170+
}
171+
172+
// Restore class
173+
Filesystem::writeFile($classFilePath, $originalClassFileContent);
174+
175+
if ($exception !== null) {
176+
/** @noinspection PhpUnhandledExceptionInspection */
177+
throw $exception;
178+
}
179+
}
180+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Kernel;
4+
5+
use Okapi\CodeTransformer\CodeTransformerKernel;
6+
use Okapi\CodeTransformer\Core\Options\Environment;
7+
use Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Transformer\StringTransformer;
8+
use Okapi\CodeTransformer\Tests\Util;
9+
10+
class DevelopmentEnvironmentKernel extends CodeTransformerKernel
11+
{
12+
protected ?string $cacheDir = Util::CACHE_DIR;
13+
14+
protected Environment $environment = Environment::DEVELOPMENT;
15+
16+
protected array $transformers = [
17+
StringTransformer::class,
18+
];
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Kernel;
4+
5+
use Okapi\CodeTransformer\Core\Options\Environment;
6+
7+
class ProductionEnvironmentKernel extends DevelopmentEnvironmentKernel
8+
{
9+
protected Environment $environment = Environment::PRODUCTION;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Target;
4+
5+
class HelloWorld
6+
{
7+
public function say(): string
8+
{
9+
return 'Hello World!';
10+
}
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Transformer;
4+
5+
use Microsoft\PhpParser\Node\StringLiteral;
6+
use Okapi\CodeTransformer\Tests\Functional\Kernel\Environment\Target\HelloWorld;
7+
use Okapi\CodeTransformer\Transformer;
8+
use Okapi\CodeTransformer\Transformer\Code;
9+
10+
class StringTransformer extends Transformer
11+
{
12+
public function getTargetClass(): string|array
13+
{
14+
return [HelloWorld::class];
15+
}
16+
17+
public function transform(Code $code): void
18+
{
19+
$sourceFileNode = $code->getSourceFileNode();
20+
21+
foreach ($sourceFileNode->getDescendantNodes() as $node) {
22+
// Find 'Hello World!' string
23+
if ($node instanceof StringLiteral) {
24+
if ($node->getStringContentsText() === 'Hello World!') {
25+
// Replace it with 'Hello from Code Transformer!'
26+
$code->edit(
27+
$node->children,
28+
"'Hello from Code Transformer!'",
29+
);
30+
} else if ($node->getStringContentsText() === 'Hello Changed World!') {
31+
// Replace it with 'Hello Changed World from Code Transformer!'
32+
$code->edit(
33+
$node->children,
34+
"'Hello Changed World from Code Transformer!'",
35+
);
36+
}
37+
}
38+
}
39+
}
40+
}

tests/Functional/Target/ChangedClass/ChangedClassTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ public function testChangedClass(): void
3636
public function testCachedChangedClass(): void
3737
{
3838
// Change class
39-
$classFilePath = Util::getFilePath(ChangedClass::class);
40-
$classFileContent = Filesystem::readFile($classFilePath);
41-
$tmpPath = Util::TMP_DIR . '/ChangedClass.php';
42-
Filesystem::writeFile($tmpPath, $classFileContent);
39+
$classFilePath = Util::getFilePath(ChangedClass::class);
40+
$originalClassFileContent = Filesystem::readFile($classFilePath);
4341

4442
$changedFileContent = str_replace(
4543
'Hello World!',
4644
'Hello Changed World!',
47-
$classFileContent,
45+
$originalClassFileContent,
4846
);
4947

5048
sleep(1);
@@ -67,13 +65,7 @@ public function testCachedChangedClass(): void
6765
}
6866

6967
// Restore class
70-
if (!file_exists($tmpPath)) {
71-
return;
72-
}
73-
74-
$tmpFileContent = Filesystem::readFile($tmpPath);
75-
76-
Filesystem::writeFile($classFilePath, $tmpFileContent);
68+
Filesystem::writeFile($classFilePath, $originalClassFileContent);
7769

7870
if ($exception !== null) {
7971
/** @noinspection PhpUnhandledExceptionInspection */

tests/Util.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class Util
1111
public const CACHE_DIR = self::TESTS_DIR . '/cache';
1212
private const CACHE_DIR_TRANSFORMED_TESTS = self::CACHE_DIR . '/transformed/tests';
1313

14-
public const TMP_DIR = __DIR__ . '/tmp';
15-
1614
public const CACHE_STATES_FILE = self::CACHE_DIR . '/cache_states.php';
1715

1816
public static function clearCache(): void
@@ -22,11 +20,6 @@ public static function clearCache(): void
2220
recursive: true,
2321
force: true,
2422
);
25-
Filesystem::rm(
26-
path: Util::TMP_DIR,
27-
recursive: true,
28-
force: true,
29-
);
3023
}
3124

3225
public static function getFilePath(string $class): string

0 commit comments

Comments
 (0)