-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add argument passed by reference test
- Loading branch information
1 parent
4ca9603
commit fdc107f
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...tional/AdviceBehavior/ModifyArgumentPassedByReference/Aspect/AddMetadataToArrayAspect.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** @noinspection PhpUnused */ | ||
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference\Aspect; | ||
|
||
use Okapi\Aop\Attributes\After; | ||
use Okapi\Aop\Attributes\Aspect; | ||
use Okapi\Aop\Invocation\AfterMethodInvocation; | ||
use Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference\Target\ArrayCreator; | ||
|
||
#[Aspect] | ||
class AddMetadataToArrayAspect | ||
{ | ||
#[After( | ||
class: ArrayCreator::class, | ||
method: 'createArray', | ||
)] | ||
public function addMetadata(AfterMethodInvocation $invocation): void | ||
{ | ||
/** @var array $array */ | ||
$array = $invocation->getArgument('data'); | ||
$array['metadata'] = 'metadata'; | ||
$invocation->setArgument('data', $array); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/Functional/AdviceBehavior/ModifyArgumentPassedByReference/Kernel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference; | ||
|
||
use Okapi\Aop\AopKernel; | ||
use Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference\Aspect\AddMetadataToArrayAspect; | ||
use Okapi\Aop\Tests\Util; | ||
|
||
class Kernel extends AopKernel | ||
{ | ||
protected ?string $cacheDir = Util::CACHE_DIR; | ||
|
||
protected array $aspects = [ | ||
AddMetadataToArrayAspect::class, | ||
]; | ||
} |
32 changes: 32 additions & 0 deletions
32
...al/AdviceBehavior/ModifyArgumentPassedByReference/ModifyArgumentPassedByReferenceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference; | ||
|
||
use Okapi\Aop\Tests\ClassLoaderMockTrait; | ||
use Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference\Target\ArrayCreator; | ||
use Okapi\Aop\Tests\Util; | ||
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[RunTestsInSeparateProcesses] | ||
class ModifyArgumentPassedByReferenceTest extends TestCase | ||
{ | ||
use ClassLoaderMockTrait; | ||
|
||
public function testModifyArgumentPassedByReference(): void | ||
{ | ||
Util::clearCache(); | ||
Kernel::init(); | ||
|
||
$this->assertWillBeWoven(ArrayCreator::class); | ||
$idCreator = new ArrayCreator(); | ||
|
||
$data = 'my-awesome-data'; | ||
$idCreator->createArray($data); | ||
/** @var array $data */ | ||
|
||
$this->assertIsArray($data); | ||
$this->assertArrayHasKey('metadata', $data); | ||
$this->assertEquals('metadata', $data['metadata']); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Functional/AdviceBehavior/ModifyArgumentPassedByReference/Target/ArrayCreator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\AdviceBehavior\ModifyArgumentPassedByReference\Target; | ||
|
||
class ArrayCreator | ||
{ | ||
public function createArray(mixed &$data): void | ||
{ | ||
$data = ['data' => $data]; | ||
} | ||
} |