Skip to content

Commit

Permalink
Add Include test
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterWoshid authored May 11, 2024
2 parents d108380 + db59b08 commit 8fb5fcb
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Aspect;

use Okapi\Aop\Attributes\After;
use Okapi\Aop\Attributes\Aspect;
use Okapi\Aop\Invocation\AfterMethodInvocation;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Target\SecureDatabaseService;

#[Aspect]
class DatabaseModifierAspect
{
#[After(
class: SecureDatabaseService::class,
method: 'load',
)]
public function modifyData(AfterMethodInvocation $invocation): void
{
/** @var SecureDatabaseService $subject */
$subject = $invocation->getSubject();

$subject->data = [
'd' => 4,
'e' => 5,
'f' => 6,
];
}
}
7 changes: 7 additions & 0 deletions tests/Functional/AdviceBehavior/Include/Database/data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'a' => 1,
'b' => 2,
'c' => 3,
];
41 changes: 41 additions & 0 deletions tests/Functional/AdviceBehavior/Include/IncludeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include;

use Okapi\Aop\Tests\ClassLoaderMockTrait;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Aspect\DatabaseModifierAspect;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Target\SecureDatabaseService;
use Okapi\Aop\Tests\Util;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

#[RunTestsInSeparateProcesses]
class IncludeTest extends TestCase
{
use ClassLoaderMockTrait;

/**
* @see DatabaseModifierAspect::modifyData()
*/
public function testIncludeFile(): void
{
Util::clearCache();
Kernel::init();

$this->assertWillBeWoven(SecureDatabaseService::class);

$service = new SecureDatabaseService();
$service->load();

$data = $service->getData();

$this->assertEquals(
[
'd' => 4,
'e' => 5,
'f' => 6,
],
$data,
);
}
}
16 changes: 16 additions & 0 deletions tests/Functional/AdviceBehavior/Include/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include;

use Okapi\Aop\AopKernel;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Aspect\DatabaseModifierAspect;
use Okapi\Aop\Tests\Util;

class Kernel extends AopKernel
{
protected ?string $cacheDir = Util::CACHE_DIR;

protected array $aspects = [
DatabaseModifierAspect::class,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Include\Target;

class SecureDatabaseService
{
private ?array $data = null;

public function load(): self
{
if ($this->data === null) {
$this->data = require dirname(__DIR__, 3) . '/AdviceBehavior/Include/Database/data.php';
}

return $this;
}

public function getData(): array
{
if ($this->data === null) {
$this->load();
}

return $this->data;
}
}
4 changes: 4 additions & 0 deletions tests/Functional/AdviceBehavior/Readonly/ReadonlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly;

use Okapi\Aop\Tests\ClassLoaderMockTrait;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly\Aspect\ReadonlyAspect;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly\Target\ReadonlyClass;
use Okapi\Aop\Tests\Functional\AdviceBehavior\Readonly\Target\ReadonlyPromotedProperties;
use Okapi\Aop\Tests\Util;
Expand All @@ -14,6 +15,9 @@ class ReadonlyTest extends TestCase
{
use ClassLoaderMockTrait;

/**
* @see ReadonlyAspect::doNothing()
*/
public function testReadonlyClass(): void
{
if (PHP_VERSION_ID < 80200) {
Expand Down

0 comments on commit 8fb5fcb

Please sign in to comment.