File tree Expand file tree Collapse file tree 4 files changed +83
-0
lines changed
tests/Functional/AdviceBehavior/ModifyArgumentPassedByReference Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /** @noinspection PhpUnused */
3+ namespace Okapi \Aop \Tests \Functional \AdviceBehavior \ModifyArgumentPassedByReference \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 \ModifyArgumentPassedByReference \Target \ArrayCreator ;
9+
10+ #[Aspect]
11+ class AddMetadataToArrayAspect
12+ {
13+ #[After(
14+ class: ArrayCreator::class,
15+ method: 'createArray ' ,
16+ )]
17+ public function addMetadata (AfterMethodInvocation $ invocation ): void
18+ {
19+ /** @var array $array */
20+ $ array = $ invocation ->getArgument ('data ' );
21+ $ array ['metadata ' ] = 'metadata ' ;
22+ $ invocation ->setArgument ('data ' , $ array );
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Okapi \Aop \Tests \Functional \AdviceBehavior \ModifyArgumentPassedByReference ;
4+
5+ use Okapi \Aop \AopKernel ;
6+ use Okapi \Aop \Tests \Functional \AdviceBehavior \ModifyArgumentPassedByReference \Aspect \AddMetadataToArrayAspect ;
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+ AddMetadataToArrayAspect::class,
15+ ];
16+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Okapi \Aop \Tests \Functional \AdviceBehavior \ModifyArgumentPassedByReference ;
4+
5+ use Okapi \Aop \Tests \ClassLoaderMockTrait ;
6+ use Okapi \Aop \Tests \Functional \AdviceBehavior \ModifyArgumentPassedByReference \Target \ArrayCreator ;
7+ use Okapi \Aop \Tests \Util ;
8+ use PHPUnit \Framework \Attributes \RunTestsInSeparateProcesses ;
9+ use PHPUnit \Framework \TestCase ;
10+
11+ #[RunTestsInSeparateProcesses]
12+ class ModifyArgumentPassedByReferenceTest extends TestCase
13+ {
14+ use ClassLoaderMockTrait;
15+
16+ public function testModifyArgumentPassedByReference (): void
17+ {
18+ Util::clearCache ();
19+ Kernel::init ();
20+
21+ $ this ->assertWillBeWoven (ArrayCreator::class);
22+ $ idCreator = new ArrayCreator ();
23+
24+ $ data = 'my-awesome-data ' ;
25+ $ idCreator ->createArray ($ data );
26+ /** @var array $data */
27+
28+ $ this ->assertIsArray ($ data );
29+ $ this ->assertArrayHasKey ('metadata ' , $ data );
30+ $ this ->assertEquals ('metadata ' , $ data ['metadata ' ]);
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Okapi \Aop \Tests \Functional \AdviceBehavior \ModifyArgumentPassedByReference \Target ;
4+
5+ class ArrayCreator
6+ {
7+ public function createArray (mixed &$ data ): void
8+ {
9+ $ data = ['data ' => $ data ];
10+ }
11+ }
You can’t perform that action at this time.
0 commit comments