Skip to content

Commit 8fb5fcb

Browse files
authored
Add Include test
2 parents d108380 + db59b08 commit 8fb5fcb

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Aspect;
4+
5+
use Okapi\Aop\Attributes\After;
6+
use Okapi\Aop\Attributes\Aspect;
7+
use Okapi\Aop\Invocation\AfterMethodInvocation;
8+
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Target\SecureDatabaseService;
9+
10+
#[Aspect]
11+
class DatabaseModifierAspect
12+
{
13+
#[After(
14+
class: SecureDatabaseService::class,
15+
method: 'load',
16+
)]
17+
public function modifyData(AfterMethodInvocation $invocation): void
18+
{
19+
/** @var SecureDatabaseService $subject */
20+
$subject = $invocation->getSubject();
21+
22+
$subject->data = [
23+
'd' => 4,
24+
'e' => 5,
25+
'f' => 6,
26+
];
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'a' => 1,
5+
'b' => 2,
6+
'c' => 3,
7+
];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include;
4+
5+
use Okapi\Aop\Tests\ClassLoaderMockTrait;
6+
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Aspect\DatabaseModifierAspect;
7+
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Target\SecureDatabaseService;
8+
use Okapi\Aop\Tests\Util;
9+
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[RunTestsInSeparateProcesses]
13+
class IncludeTest extends TestCase
14+
{
15+
use ClassLoaderMockTrait;
16+
17+
/**
18+
* @see DatabaseModifierAspect::modifyData()
19+
*/
20+
public function testIncludeFile(): void
21+
{
22+
Util::clearCache();
23+
Kernel::init();
24+
25+
$this->assertWillBeWoven(SecureDatabaseService::class);
26+
27+
$service = new SecureDatabaseService();
28+
$service->load();
29+
30+
$data = $service->getData();
31+
32+
$this->assertEquals(
33+
[
34+
'd' => 4,
35+
'e' => 5,
36+
'f' => 6,
37+
],
38+
$data,
39+
);
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include;
4+
5+
use Okapi\Aop\AopKernel;
6+
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Aspect\DatabaseModifierAspect;
7+
use Okapi\Aop\Tests\Util;
8+
9+
class Kernel extends AopKernel
10+
{
11+
protected ?string $cacheDir = Util::CACHE_DIR;
12+
13+
protected array $aspects = [
14+
DatabaseModifierAspect::class,
15+
];
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Target;
4+
5+
class SecureDatabaseService
6+
{
7+
private ?array $data = null;
8+
9+
public function load(): self
10+
{
11+
if ($this->data === null) {
12+
$this->data = require dirname(__DIR__, 3) . '/AdviceBehavior/Include/Database/data.php';
13+
}
14+
15+
return $this;
16+
}
17+
18+
public function getData(): array
19+
{
20+
if ($this->data === null) {
21+
$this->load();
22+
}
23+
24+
return $this->data;
25+
}
26+
}

tests/Functional/AdviceBehavior/Readonly/ReadonlyTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly;
44

55
use Okapi\Aop\Tests\ClassLoaderMockTrait;
6+
use Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly\Aspect\ReadonlyAspect;
67
use Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly\Target\ReadonlyClass;
78
use Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly\Target\ReadonlyPromotedProperties;
89
use Okapi\Aop\Tests\Util;
@@ -14,6 +15,9 @@ class ReadonlyTest extends TestCase
1415
{
1516
use ClassLoaderMockTrait;
1617

18+
/**
19+
* @see ReadonlyAspect::doNothing()
20+
*/
1721
public function testReadonlyClass(): void
1822
{
1923
if (PHP_VERSION_ID < 80200) {

0 commit comments

Comments
 (0)