Skip to content

Commit

Permalink
Initial work on value object to represent property hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 14, 2024
1 parent d99567b commit 7cfd5df
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Framework/MockObject/Runtime/PropertyHook/PropertyGetHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject\Runtime;

use function sprintf;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final readonly class PropertyGetHook extends PropertyHook
{
/**
* @return non-empty-string
*
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
public function asString(): string
{
return sprintf(
'$%s::get',
$this->propertyName(),
);
}
}
60 changes: 60 additions & 0 deletions src/Framework/MockObject/Runtime/PropertyHook/PropertyHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject\Runtime;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
abstract readonly class PropertyHook
{
/**
* @var non-empty-string
*/
private string $propertyName;

/**
* @param non-empty-string $propertyName
*/
public static function get(string $propertyName): PropertyGetHook
{
return new PropertyGetHook($propertyName);
}

/**
* @param non-empty-string $propertyName
*/
public static function set(string $propertyName): PropertySetHook
{
return new PropertySetHook($propertyName);
}

/**
* @param non-empty-string $propertyName
*/
protected function __construct(string $propertyName)
{
$this->propertyName = $propertyName;
}

/**
* @return non-empty-string
*/
public function propertyName(): string
{
return $this->propertyName;
}

/**
* @return non-empty-string
*
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
abstract public function asString(): string;
}
31 changes: 31 additions & 0 deletions src/Framework/MockObject/Runtime/PropertyHook/PropertySetHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject\Runtime;

use function sprintf;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final readonly class PropertySetHook extends PropertyHook
{
/**
* @return non-empty-string
*
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
public function asString(): string
{
return sprintf(
'$%s::set',
$this->propertyName(),
);
}
}
40 changes: 40 additions & 0 deletions tests/unit/Framework/MockObject/Runtime/PropertyGetHookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject\Runtime;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;

#[CoversClass(PropertyHook::class)]
#[CoversClass(PropertyGetHook::class)]
#[Small]
#[Group('test-doubles')]
final class PropertyGetHookTest extends TestCase
{
public function testHasPropertyName(): void
{
$propertyName = 'property';

$propertyHook = PropertyHook::get($propertyName);

$this->assertSame($propertyName, $propertyHook->propertyName());
}

public function testCanBeRepresentedAsString(): void
{
$propertyName = 'property';

$propertyHook = PropertyHook::get($propertyName);

$this->assertSame('$' . $propertyName . '::get', $propertyHook->asString());
}
}
40 changes: 40 additions & 0 deletions tests/unit/Framework/MockObject/Runtime/PropertySetHookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject\Runtime;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;

#[CoversClass(PropertyHook::class)]
#[CoversClass(PropertySetHook::class)]
#[Small]
#[Group('test-doubles')]
final class PropertySetHookTest extends TestCase
{
public function testHasPropertyName(): void
{
$propertyName = 'property';

$propertyHook = PropertyHook::set($propertyName);

$this->assertSame($propertyName, $propertyHook->propertyName());
}

public function testCanBeRepresentedAsString(): void
{
$propertyName = 'property';

$propertyHook = PropertyHook::set($propertyName);

$this->assertSame('$' . $propertyName . '::set', $propertyHook->asString());
}
}

0 comments on commit 7cfd5df

Please sign in to comment.