-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add AuthorHydrator * Add tests * fix test * add covers * add test * add test * upd readme
- Loading branch information
Showing
12 changed files
with
169 additions
and
47 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
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,17 @@ | ||
<?php | ||
|
||
namespace ArtARTs36\GitHandler\Contracts; | ||
|
||
use ArtARTs36\GitHandler\Data\Author; | ||
use JetBrains\PhpStorm\ArrayShape; | ||
|
||
interface AuthorHydrator | ||
{ | ||
/** | ||
* Hydrate raw array to Author object. | ||
*/ | ||
public function hydrate( | ||
#[ArrayShape(['name' => 'string', 'email' => 'string'])] | ||
array $raw | ||
): Author; | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace ArtARTs36\GitHandler\Data\Author; | ||
|
||
use ArtARTs36\GitHandler\Contracts\AuthorHydrator; | ||
use ArtARTs36\GitHandler\Data\Author; | ||
|
||
class CacheableHydrator implements AuthorHydrator | ||
{ | ||
protected $hydrator; | ||
|
||
/** @var array<string, Author> */ | ||
protected $authors = []; | ||
|
||
public function __construct(AuthorHydrator $hydrator) | ||
{ | ||
$this->hydrator = $hydrator; | ||
} | ||
|
||
public function hydrate(array $raw): Author | ||
{ | ||
if (! array_key_exists($raw['email'], $this->authors)) { | ||
$this->authors[$raw['email']] = $this->hydrator->hydrate($raw); | ||
} | ||
|
||
return $this->authors[$raw['email']]; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace ArtARTs36\GitHandler\Data\Author; | ||
|
||
use ArtARTs36\GitHandler\Contracts\AuthorHydrator; | ||
use ArtARTs36\GitHandler\Data\Author; | ||
|
||
class Hydrator implements AuthorHydrator | ||
{ | ||
public function hydrate(array $raw): Author | ||
{ | ||
return new Author($raw['name'], $raw['email']); | ||
} | ||
} |
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
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
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
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
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
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,42 @@ | ||
<?php | ||
|
||
namespace ArtARTs36\GitHandler\Tests\Unit\Data\Author; | ||
|
||
use ArtARTs36\GitHandler\Data\Author\CacheableHydrator; | ||
use ArtARTs36\GitHandler\Data\Author\Hydrator; | ||
use ArtARTs36\GitHandler\Tests\Unit\TestCase; | ||
|
||
final class CacheableHydratorTest extends TestCase | ||
{ | ||
public function providerForTestHydrate(): array | ||
{ | ||
return [ | ||
[ | ||
[ | ||
['name' => 'ArtARTs36', 'email' => 'temicska99@mail.ru'], | ||
['name' => 'ArtARTs36', 'email' => 'temicska99@mail.ru'], | ||
] | ||
], | ||
[ | ||
[ | ||
['name' => 'ArtARTs36', 'email' => 'temicska99@mail.ru'], | ||
['name' => 'random-name', 'email' => 'temicska99@mail.ru'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @covers \ArtARTs36\GitHandler\Data\Author\CacheableHydrator::hydrate | ||
* @covers \ArtARTs36\GitHandler\Data\Author\CacheableHydrator::__construct | ||
* @dataProvider providerForTestHydrate | ||
*/ | ||
public function testHydrate(array $raws): void | ||
{ | ||
$hydrator = new CacheableHydrator(new Hydrator()); | ||
|
||
[$firstAuthor, $twoAuthor] = [$hydrator->hydrate($raws[0]), $hydrator->hydrate($raws[1])]; | ||
|
||
self::assertSame($firstAuthor, $twoAuthor); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace ArtARTs36\GitHandler\Tests\Unit\Data\Author; | ||
|
||
use ArtARTs36\GitHandler\Data\Author\Hydrator; | ||
use ArtARTs36\GitHandler\Tests\Unit\TestCase; | ||
|
||
final class HydratorTest extends TestCase | ||
{ | ||
/** | ||
* @covers \ArtARTs36\GitHandler\Data\Author\Hydrator::hydrate | ||
*/ | ||
public function testHydrate(): void | ||
{ | ||
$raw = [ | ||
'name' => 'ArtARTs36', | ||
'email' => 'temicska99@mail.ru', | ||
]; | ||
|
||
self::assertEquals($raw, (new Hydrator())->hydrate($raw)->toArray()); | ||
} | ||
} |
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