-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on value object to represent property hooks
- Loading branch information
1 parent
d99567b
commit 7cfd5df
Showing
5 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Framework/MockObject/Runtime/PropertyHook/PropertyGetHook.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,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
60
src/Framework/MockObject/Runtime/PropertyHook/PropertyHook.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,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
31
src/Framework/MockObject/Runtime/PropertyHook/PropertySetHook.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,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
40
tests/unit/Framework/MockObject/Runtime/PropertyGetHookTest.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,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
40
tests/unit/Framework/MockObject/Runtime/PropertySetHookTest.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,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()); | ||
} | ||
} |