69 lines
2.8 KiB
PHP
69 lines
2.8 KiB
PHP
<?php
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Tests\Classes\ParameterKeyTestClass;
|
|
use Tests\Aspects\ParameterKeyAspect;
|
|
|
|
final class ParameterKeyTest extends TestCase
|
|
{
|
|
private ParameterKeyTestClass $instance;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->instance = new ParameterKeyTestClass();
|
|
ParameterKeyAspect::reset();
|
|
}
|
|
|
|
public function testBeforeReceivesAssociativeArrayWithParameterNames(): void
|
|
{
|
|
$this->instance->methodWithNamedParams('John', 'Doe', 30);
|
|
|
|
// Verify that the keys are parameter names, not numeric indices
|
|
$this->assertEquals(['firstName', 'lastName', 'age'], ParameterKeyAspect::$capturedKeys);
|
|
|
|
// Verify the values are correctly mapped to parameter names
|
|
$this->assertEquals('John', ParameterKeyAspect::$capturedArgs['firstName']);
|
|
$this->assertEquals('Doe', ParameterKeyAspect::$capturedArgs['lastName']);
|
|
$this->assertEquals(30, ParameterKeyAspect::$capturedArgs['age']);
|
|
}
|
|
|
|
public function testSingleParameterHasCorrectKey(): void
|
|
{
|
|
$testData = ['key1' => 'value1', 'key2' => 'value2'];
|
|
$this->instance->methodWithSingleParam($testData);
|
|
|
|
// Verify the parameter name is used as the key
|
|
$this->assertEquals(['data'], ParameterKeyAspect::$capturedKeys);
|
|
$this->assertEquals($testData, ParameterKeyAspect::$capturedArgs['data']);
|
|
}
|
|
|
|
public function testMixedTypesHaveCorrectKeys(): void
|
|
{
|
|
$metadata = (object) ['info' => 'test'];
|
|
$this->instance->methodWithMixedTypes(42, 'TestName', true, $metadata);
|
|
|
|
// Verify all parameter names are present as keys
|
|
$this->assertEquals(['id', 'name', 'active', 'metadata'], ParameterKeyAspect::$capturedKeys);
|
|
|
|
// Verify each parameter value is accessible by its name
|
|
$this->assertEquals(42, ParameterKeyAspect::$capturedArgs['id']);
|
|
$this->assertEquals('TestName', ParameterKeyAspect::$capturedArgs['name']);
|
|
$this->assertTrue(ParameterKeyAspect::$capturedArgs['active']);
|
|
$this->assertSame($metadata, ParameterKeyAspect::$capturedArgs['metadata']);
|
|
}
|
|
|
|
public function testParametersCanBeAccessedByName(): void
|
|
{
|
|
$this->instance->methodWithNamedParams('Jane', 'Smith', 25);
|
|
|
|
// Verify we can access parameters by their name using array key syntax
|
|
$this->assertArrayHasKey('firstName', ParameterKeyAspect::$capturedArgs);
|
|
$this->assertArrayHasKey('lastName', ParameterKeyAspect::$capturedArgs);
|
|
$this->assertArrayHasKey('age', ParameterKeyAspect::$capturedArgs);
|
|
|
|
// Verify numeric keys do NOT exist
|
|
$this->assertArrayNotHasKey(0, ParameterKeyAspect::$capturedArgs);
|
|
$this->assertArrayNotHasKey(1, ParameterKeyAspect::$capturedArgs);
|
|
$this->assertArrayNotHasKey(2, ParameterKeyAspect::$capturedArgs);
|
|
}
|
|
}
|