Skip to content

Commit

Permalink
Logger refactor (#42)
Browse files Browse the repository at this point in the history
* add AuthorHydrator

* Add tests

* fix test

* add covers

* add test

* add test

* upd readme
  • Loading branch information
ArtARTs36 authored Jan 12, 2022
1 parent e8bc297 commit c4488f0
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 47 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ Tool for work with Git in PHP

| Type | Files' count | Code lines' count |
| ------------ | ------------ | ------------ |
| Source | 183 | 6680 |
| Tests | 118 | 7012 |
| Source | 186 | 6744 |
| Tests | 120 | 7070 |

17 changes: 17 additions & 0 deletions src/Contracts/AuthorHydrator.php
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;
}
28 changes: 28 additions & 0 deletions src/Data/Author/CacheableHydrator.php
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']];
}
}
14 changes: 14 additions & 0 deletions src/Data/Author/Hydrator.php
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']);
}
}
8 changes: 7 additions & 1 deletion src/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
use ArtARTs36\GitHandler\Contracts\Handler\GitHandler;
use ArtARTs36\GitHandler\Contracts\Transaction\GitTransaction;
use ArtARTs36\GitHandler\Contracts\Backup\GitBackup;
use ArtARTs36\GitHandler\Data\Author\CacheableHydrator;
use ArtARTs36\GitHandler\Data\Author\Hydrator;
use ArtARTs36\GitHandler\Data\GitContext;
use ArtARTs36\GitHandler\Data\Version;
use ArtARTs36\GitHandler\Support\Logger;
Expand Down Expand Up @@ -144,7 +146,11 @@ public function archives(): GitArchiveCommand

public function logs(): GitLogCommand
{
return new LogCommand(new Logger(), $this->commandBuilder, $this->executor);
return new LogCommand(
new Logger(new CacheableHydrator(new Hydrator())),
$this->commandBuilder,
$this->executor
);
}

public function greps(): GitGrepCommand
Expand Down
27 changes: 13 additions & 14 deletions src/Support/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ArtARTs36\GitHandler\Support;

use ArtARTs36\GitHandler\Contracts\AuthorHydrator;
use ArtARTs36\GitHandler\Contracts\LogParser;
use ArtARTs36\GitHandler\Data\Author;
use ArtARTs36\GitHandler\Data\Commit;
Expand All @@ -13,8 +14,12 @@ class Logger implements LogParser
{
protected $regex = '/\|log-entry\|(.*?)\|(.*?)\|(.*?)\|(.*?)\|(.*?)\|/m';

/** @var array<string, Author> $authors */
protected $authors = [];
protected $authorHydrator;

public function __construct(AuthorHydrator $authorHydrator)
{
$this->authorHydrator = $authorHydrator;
}

public function parse(Str $raw): ?LogCollection
{
Expand All @@ -24,7 +29,7 @@ public function parse(Str $raw): ?LogCollection
$logs[] = new Log(
new Commit(trim($match[1])),
new \DateTime($match[2]),
$this->getOrCreateAuthor($match[3], $match[4]),
$this->createAuthor($match),
trim($match[5])
);
}
Expand All @@ -36,17 +41,11 @@ public function parse(Str $raw): ?LogCollection
return new LogCollection($logs);
}

protected function hasAuthor(string $name): bool
{
return array_key_exists($name, $this->authors);
}

protected function getOrCreateAuthor(string $name, string $email): Author
protected function createAuthor(array $raw): Author
{
if (! $this->hasAuthor($name)) {
$this->authors[$name] = new Author($name, $email);
}

return $this->authors[$name];
return $this->authorHydrator->hydrate([
'name' => $raw[3],
'email' => $raw[4],
]);
}
}
4 changes: 4 additions & 0 deletions tests/Unit/Commands/AttributeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public function providerForTestAdd(): array
// .gitattributes has requested pattern and equals attribute
"my-pattern value\n", 'my-pattern', ['value'], "my-pattern value\n",
],
[
// .gitattributes has requested pattern
"my-pattern value\n", 'my-pattern', ['value2'], "my-pattern value value2\n",
],
];
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Commands/FileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public function testCreateFileWithFolder(): void
self::assertTrue($this->mockFileSystem->exists($this->mockGitContext->getRootDir() . '/folder1/file.txt'));
}

/**
* @covers \ArtARTs36\GitHandler\Command\Commands\FileCommand::createFolder
*/
public function testCreateFolder(): void
{
$fileCommand = $this->makeFileCommand();

$fileCommand->createFolder('my-folder');

self::assertTrue($this->mockFileSystem->exists($fileCommand->createPathTo('my-folder')));
}

/**
* @covers \ArtARTs36\GitHandler\Command\Commands\FileCommand::getContent
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Commands/LogCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ArtARTs36\GitHandler\Tests\Unit\Commands;

use ArtARTs36\GitHandler\Command\Commands\LogCommand;
use ArtARTs36\GitHandler\Data\Author\Hydrator;
use ArtARTs36\GitHandler\Exceptions\BranchDoesNotHaveCommits;
use ArtARTs36\GitHandler\Support\Logger;
use ArtARTs36\GitHandler\Tests\Unit\GitTestCase;
Expand All @@ -16,7 +17,7 @@ public function testLogOnBranchDoesNotHaveCommits(): void
{
$this->mockCommandExecutor->addFail('fatal: your current branch \'master\' does not have any commits yet');

$git = new LogCommand(new Logger(), $this->mockCommandBuilder, $this->mockCommandExecutor);
$git = new LogCommand(new Logger(new Hydrator()), $this->mockCommandBuilder, $this->mockCommandExecutor);

self::expectException(BranchDoesNotHaveCommits::class);

Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Data/Author/CacheableHydratorTest.php
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);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Data/Author/HydratorTest.php
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());
}
}
35 changes: 6 additions & 29 deletions tests/Unit/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ final class LoggerTest extends TestCase
*/
public function testParseOnEmptyRaw(): void
{
$logger = new Logger();

self::assertNull($logger->parse(new Str('')));
self::assertNull($this->makeLogger()->parse(new Str('')));
}

/**
* @covers \ArtARTs36\GitHandler\Support\Logger::parse
* @covers \ArtARTs36\GitHandler\Support\Logger::hasAuthor
* @covers \ArtARTs36\GitHandler\Support\Logger::createAuthor
* @covers \ArtARTs36\GitHandler\Support\Logger::__construct
*/
public function testParse(): void
{
$logger = new Logger();
$logger = $this->makeLogger();

//

Expand All @@ -48,32 +47,10 @@ public function testParse(): void
],
'message' => 'update readme.md',
], $result->first()->toArray());
self::assertTrue($this->callMethodFromObject($logger, 'hasAuthor', 'ArtARTs36'));
}

/**
* @covers \ArtARTs36\GitHandler\Support\Logger::getOrCreateAuthor
*/
public function testGetOrCreateAuthor(): void
private function makeLogger(): Logger
{
$logger = new Logger();

//

[$name, $email] = ['Dev', 'test@mail.ru'];

//

/** @var Author $author */
$author = $this->callMethodFromObject($logger, 'getOrCreateAuthor', $name, $email);

self::assertEquals($name, $author->name);
self::assertEquals($email, $author->email);

//

$authorTwo = $this->callMethodFromObject($logger, 'getOrCreateAuthor', $name, $email);

self::assertSame($author, $authorTwo);
return new Logger(new Author\Hydrator());
}
}

0 comments on commit c4488f0

Please sign in to comment.