Skip to content

Commit 71416e0

Browse files
committed
Added test cases for "already initialized kernel"
1 parent 93c4dbe commit 71416e0

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Functional;
4+
5+
use Okapi\CodeTransformer\Tests\Stubs\Kernel\EmptyKernel;
6+
use Okapi\CodeTransformer\Tests\Stubs\Kernel\ExceptionOnDoubleInitializationKernel;
7+
use Okapi\CodeTransformer\Tests\Util;
8+
use Okapi\Singleton\Exceptions\AlreadyInitializedException;
9+
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[RunTestsInSeparateProcesses]
13+
class AlreadyInitializedKernelTest extends TestCase
14+
{
15+
public function testInitializedKernelTwice(): void
16+
{
17+
Util::clearCache();
18+
19+
EmptyKernel::init();
20+
EmptyKernel::init();
21+
22+
$this->assertTrue(true);
23+
}
24+
25+
public function testInitializeKernelTwiceWithExceptionOnDoubleInitializationOption(): void
26+
{
27+
Util::clearCache();
28+
29+
$this->expectException(AlreadyInitializedException::class);
30+
31+
ExceptionOnDoubleInitializationKernel::init();
32+
ExceptionOnDoubleInitializationKernel::init();
33+
}
34+
}

tests/Stubs/Kernel/EmptyKernel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Tests\Stubs\Kernel;
4+
5+
use Okapi\CodeTransformer\CodeTransformerKernel;
6+
use Okapi\CodeTransformer\Tests\Util;
7+
8+
class EmptyKernel extends CodeTransformerKernel
9+
{
10+
protected ?string $cacheDir = Util::CACHE_DIR;
11+
12+
protected array $transformers = [];
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/** @noinspection PhpUnhandledExceptionInspection */
3+
namespace Okapi\CodeTransformer\Tests\Stubs\Kernel;
4+
5+
use Exception;
6+
7+
class ExceptionOnDoubleInitializationKernel extends EmptyKernel
8+
{
9+
private int $initCount = 0;
10+
11+
protected bool $throwExceptionOnDoubleInitialization = true;
12+
13+
protected function preInit(): void
14+
{
15+
$this->initCount++;
16+
17+
if ($this->initCount > 1) {
18+
throw new Exception('I should not be initialized twice!');
19+
}
20+
21+
parent::preInit();
22+
}
23+
}

0 commit comments

Comments
 (0)