Skip to content

Commit

Permalink
Add argument passed by reference test
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterWoshid committed Sep 4, 2024
1 parent 4ca9603 commit fdc107f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
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);
}
}
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,
];
}
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']);
}
}
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];
}
}

0 comments on commit fdc107f

Please sign in to comment.