forked from fzaninotto/Faker
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding trait for GeneratorAwareExtension (#165)
* Adding trait for GeneratorAwareExtension * Fixes * Fix: Remove unnecessary initialization * Fix: Add test Co-authored-by: Andreas Möller <am@localheinz.com>
- Loading branch information
1 parent
1c73fa4
commit 1dad76c
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Faker\Extension; | ||
|
||
use Faker\Generator; | ||
|
||
/** | ||
* A helper trait to be used with GeneratorAwareExtension. | ||
*/ | ||
trait GeneratorAwareExtensionTrait | ||
{ | ||
/** | ||
* @var Generator|null | ||
*/ | ||
private $generator; | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function withGenerator(Generator $generator): self | ||
{ | ||
$instance = clone $this; | ||
|
||
$instance->generator = $generator; | ||
|
||
return $instance; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Extension; | ||
|
||
use Faker\Extension\Extension; | ||
use Faker\Extension\GeneratorAwareExtensionTrait; | ||
use Faker\Generator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Faker\Extension\GeneratorAwareExtensionTrait | ||
*/ | ||
final class GeneratorAwareExtensionTest extends TestCase | ||
{ | ||
public function testWithGeneratorClonesExtensionAndSetsGenerator(): void | ||
{ | ||
$generator = new Generator(); | ||
|
||
$extension = new class() implements Extension { | ||
use GeneratorAwareExtensionTrait; | ||
|
||
public function generator(): Generator | ||
{ | ||
return $this->generator; | ||
} | ||
}; | ||
|
||
$mutated = $extension->withGenerator($generator); | ||
|
||
self::assertNotSame($mutated, $extension); | ||
self::assertSame($generator, $mutated->generator()); | ||
} | ||
} |