Skip to content

Commit

Permalink
[Feature] Diff configurator (#43)
Browse files Browse the repository at this point in the history
* Add DiffConfigurator

* Add TypeCaster::integer

* use TypeCaster::integer

* remove doc

* build docs
  • Loading branch information
ArtARTs36 authored Jan 18, 2022
1 parent c4488f0 commit 259ed25
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 8 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 | 186 | 6744 |
| Tests | 120 | 7070 |
| Source | 188 | 6795 |
| Tests | 121 | 7138 |

2 changes: 1 addition & 1 deletion src/Config/Configurators/CoreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function parse(array $raw): ConfigSubject
return new Core(
$raw['autocrlf'] ?? '',
TypeCaster::boolean($raw['ignorecase'] ?? ''),
$raw['repositoryformatversion'] ?? 0,
TypeCaster::integer($raw, 'repositoryformatversion'),
TypeCaster::boolean($raw['bare'] ?? ''),
TypeCaster::boolean($raw['logallrefupdates'] ?? ''),
TypeCaster::boolean($raw['precomposeunicode'] ?? ''),
Expand Down
21 changes: 21 additions & 0 deletions src/Config/Configurators/DiffConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace ArtARTs36\GitHandler\Config\Configurators;

use ArtARTs36\GitHandler\Config\Subjects\ConfigDiff;
use ArtARTs36\GitHandler\Contracts\Config\ConfigSubject;
use ArtARTs36\GitHandler\Contracts\Config\SubjectConfigurator;
use ArtARTs36\GitHandler\Support\TypeCaster;

class DiffConfigurator implements SubjectConfigurator
{
public function parse(array $raw): ConfigSubject
{
return new ConfigDiff(TypeCaster::string($raw, 'external'), TypeCaster::boolean($raw['renames']));
}

public function getPrefix(): string
{
return 'diff';
}
}
5 changes: 3 additions & 2 deletions src/Config/Configurators/PackConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArtARTs36\GitHandler\Config\Subjects\Pack;
use ArtARTs36\GitHandler\Contracts\Config\ConfigSubject;
use ArtARTs36\GitHandler\Contracts\Config\SubjectConfigurator;
use ArtARTs36\GitHandler\Support\TypeCaster;

class PackConfigurator implements SubjectConfigurator
{
Expand All @@ -13,10 +14,10 @@ public function parse(array $raw): ConfigSubject
return new Pack(
$raw['windowmemory'] ?? '',
$raw['packsizelimit'] ?? '',
$raw['threads'] ?? 0,
TypeCaster::integer($raw, 'threads'),
$raw['deltacachesize'] ?? '',
$raw['sizelimit'] ?? '',
$raw['window'] ?? 0
TypeCaster::integer($raw, 'window')
);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Config/Subjects/ConfigDiff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ArtARTs36\GitHandler\Config\Subjects;

use ArtARTs36\GitHandler\Attributes\ConfigKey;

class ConfigDiff extends AbstractSubject
{
#[ConfigKey('external')]
public $externalPath;

#[ConfigKey('renames')]
public $renames;

public function __construct(string $externalPath, bool $renames)
{
$this->externalPath = $externalPath;
$this->renames = $renames;
}
}
2 changes: 2 additions & 0 deletions src/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ArtARTs36\GitHandler\Command\Commands\SubmoduleCommand;
use ArtARTs36\GitHandler\Command\GitCommandBuilder;
use ArtARTs36\GitHandler\Config\Configurators\CommitConfigurator;
use ArtARTs36\GitHandler\Config\Configurators\DiffConfigurator;
use ArtARTs36\GitHandler\Config\Configurators\SubmoduleConfigurator;
use ArtARTs36\GitHandler\Contracts\Commands\GitAttributeCommand;
use ArtARTs36\GitHandler\Contracts\Commands\GitFileCommand;
Expand Down Expand Up @@ -216,6 +217,7 @@ public function config(): GitConfigCommand
new BranchConfigurator(),
new SubmoduleConfigurator(),
new CommitConfigurator(),
new DiffConfigurator(),
])
),
$this->commandBuilder,
Expand Down
3 changes: 0 additions & 3 deletions src/Support/TemporaryPathGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class TemporaryPathGenerator implements PathGenerator

private static $counter = 0;

/**
* @codeCoverageIgnore
*/
public function __construct(FileSystem $files)
{
$this->files = $files;
Expand Down
10 changes: 10 additions & 0 deletions src/Support/TypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ public static function boolean(string $raw): bool
{
return $raw === 'true';
}

public static function string(array $raw, string $key): string
{
return array_key_exists($key, $raw) ? $raw[$key] : '';
}

public static function integer(array $raw, string $key, int $default = 0): int
{
return array_key_exists($key, $raw) ? (int) $raw[$key] : $default;
}
}
37 changes: 37 additions & 0 deletions tests/Unit/Config/Configurators/DiffConfiguratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ArtARTs36\GitHandler\Tests\Unit\Config\Configurators;

use ArtARTs36\GitHandler\Config\Configurators\DiffConfigurator;
use ArtARTs36\GitHandler\Tests\Unit\TestCase;

final class DiffConfiguratorTest extends TestCase
{
public function providerForTestParse(): array
{
return [
[
[
'external' => 'external_path',
'renames' => 'true',
],
[
'externalPath' => 'external_path',
'renames' => true,
],
],
];
}

/**
* @dataProvider providerForTestParse
* @covers \ArtARTs36\GitHandler\Config\Configurators\DiffConfigurator::parse
* @covers \ArtARTs36\GitHandler\Config\Subjects\ConfigDiff::__construct
*/
public function testParse(array $raw, array $expected): void
{
$configurator = new DiffConfigurator();

self::assertEquals($expected, $configurator->parse($raw)->toArray());
}
}
1 change: 1 addition & 0 deletions tests/Unit/TemporaryPathGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class TemporaryPathGeneratorTest extends GitTestCase
/**
* @covers \ArtARTs36\GitHandler\Support\TemporaryPathGenerator::toArchive
* @covers \ArtARTs36\GitHandler\Support\TemporaryPathGenerator::buildArchiveName
* @covers \ArtARTs36\GitHandler\Support\TemporaryPathGenerator::__construct
*/
public function testToArchiveNotRepeats(): void
{
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/TypeCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,34 @@ public function testBoolean(string $raw, bool $expected): void
{
self::assertEquals($expected, TypeCaster::boolean($raw));
}


public function providerForTestInteger(): array
{
return [
[
[
'key1' => '2',
],
'key1',
2,
],
[
[
'key1' => '2',
],
'key2',
0,
],
];
}

/**
* @dataProvider providerForTestInteger
* @covers \ArtARTs36\GitHandler\Support\TypeCaster::integer
*/
public function testInteger(array $raw, string $key, int $expected): void
{
self::assertEquals($expected, TypeCaster::integer($raw, $key));
}
}

0 comments on commit 259ed25

Please sign in to comment.