Skip to content

Commit

Permalink
add tests for the normalizer and model
Browse files Browse the repository at this point in the history
  • Loading branch information
sweikenb committed Jul 1, 2024
1 parent 5b8699a commit 334b620
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Model/NormalizedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getDiffFieldPaths(NormalizedModel $other): array
}

$diffFieldPaths = array_unique($diffFieldPaths);
ksort($diffFieldPaths);
sort($diffFieldPaths);

return $diffFieldPaths;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Model/NormalizedModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Sweikenb\Library\Dirty\Tests\Model;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Sweikenb\Library\Dirty\Model\NormalizedModel;

#[CoversClass(NormalizedModel::class)]
class NormalizedModelTest extends TestCase
{
public function testGetDiffFieldPaths(): void
{
$paths = ['foo' => 1, 'foo.bar.baz' => 1, 'bar.baz' => 1];
$model = new NormalizedModel('234', $paths, '345');

$this->assertEquals(
[],
$model->getDiffFieldPaths(new NormalizedModel('123', $paths, '987'))
);

$paths = ['new' => 1, 'foo' => 1, 'foo.bar.baz' => 2, 'bar.baz' => 1];
$this->assertEquals(
['foo.bar.baz', 'new'],
$model->getDiffFieldPaths(new NormalizedModel('123', $paths, '987'))
);

$paths = ['foo' => 1];
$this->assertEquals(
['bar.baz', 'foo.bar.baz'],
$model->getDiffFieldPaths(new NormalizedModel('123', $paths, '987'))
);
}
}
128 changes: 128 additions & 0 deletions tests/Service/NormalizerServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Sweikenb\Library\Dirty\Tests\Service;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sweikenb\Library\Dirty\Model\ConfigModel;
use Sweikenb\Library\Dirty\Service\NormalizerService;
use Sweikenb\Library\Dirty\Service\NormalizerService as NormalizerServiceAlias;
use Sweikenb\Library\Dirty\Tests\Testdata\Classes\Person;
use Sweikenb\Library\Dirty\Tests\Testdata\Classes\School;
use Sweikenb\Library\Dirty\Tests\Testdata\Classes\SchoolClass;

#[CoversClass(NormalizerServiceAlias::class)]
class NormalizerServiceTest extends TestCase
{
public static function dataProvider(): array
{
$array = [
'name' => 'ACME School',
'classes' => [
'A1' => [
'teacher' => ['name' => 'Mr. Mustermann', 'age' => 44],
'students' => [
['name' => 'Peter', 'age' => 11],
['name' => 'Anna', 'age' => 12],
['name' => 'Ludger', 'age' => 12],
],
],
],
];

$object = new School(
'ACME School',
[
'A1' => new SchoolClass(
new Person('Mr. Mustermann', 44),
[
new Person('Peter', 11),
new Person('Anna', 12),
new Person('Ludger', 12),
]
),
]
);

return [
[$array],
[$object],
];
}

#[DataProvider('dataProvider')]
public function testNormalizeDefault(array|object $toCheck): void
{
$expected = [
'classes.A1.students.0.age' => '11',
'classes.A1.students.0.name' => 'Peter',
'classes.A1.students.1.age' => '12',
'classes.A1.students.1.name' => 'Anna',
'classes.A1.students.2.age' => '12',
'classes.A1.students.2.name' => 'Ludger',
'classes.A1.teacher.age' => '44',
'classes.A1.teacher.name' => 'Mr. Mustermann',
'name' => 'ACME School',
];

$result = (new NormalizerService())->execute('test', $toCheck);
$this->assertSame($expected, $result->fieldPaths);
}

#[DataProvider('dataProvider')]
public function testNormalizeExplicit(array|object $toCheck): void
{
$expected = [
'classes.A1.teacher.age' => '44',
'classes.A1.teacher.name' => 'Mr. Mustermann',
'name' => 'ACME School',
];
$result = (new NormalizerService())->execute(
'test',
$toCheck,
new ConfigModel(checkFieldPath: ['name', 'classes.A1.teacher'])
);
$this->assertSame($expected, $result->fieldPaths);
}

#[DataProvider('dataProvider')]
public function testNormalizeIgnore(array|object $toCheck): void
{
$expected = [
'classes.A1.students.0.age' => '11',
'classes.A1.students.0.name' => 'Peter',
'classes.A1.students.1.age' => '12',
'classes.A1.students.1.name' => 'Anna',
'classes.A1.teacher.age' => '44',
'classes.A1.teacher.name' => 'Mr. Mustermann',
];
$result = (new NormalizerService())->execute(
'test',
$toCheck,
new ConfigModel(ignoreFieldPath: ['name', 'classes.A1.students.2'])
);
$this->assertSame($expected, $result->fieldPaths);
}

#[DataProvider('dataProvider')]
public function testNormalizeBoth(array|object $toCheck): void
{
$expected = [
'classes.A1.students.0.age' => '11',
'classes.A1.students.0.name' => 'Peter',
'classes.A1.students.2.age' => '12',
'classes.A1.students.2.name' => 'Ludger',
'name' => 'ACME School',
];
$result = (new NormalizerService())->execute(
'test',
$toCheck,
new ConfigModel(
checkFieldPath: ['name', 'classes.A1.students'],
ignoreFieldPath: ['classes.A1.students.1']
)
);
$this->assertSame($expected, $result->fieldPaths);
}
}
12 changes: 12 additions & 0 deletions tests/Testdata/Classes/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Sweikenb\Library\Dirty\Tests\Testdata\Classes;

class Person
{
public function __construct(
public readonly string $name,
public readonly int $age
) {
}
}
22 changes: 22 additions & 0 deletions tests/Testdata/Classes/School.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Sweikenb\Library\Dirty\Tests\Testdata\Classes;

class School
{
public function __construct(
public readonly string $name,
private array $classes,
) {
}

public function getClasses(): array
{
return $this->classes;
}

public function setClasses(array $classes): void
{
$this->classes = $classes;
}
}
32 changes: 32 additions & 0 deletions tests/Testdata/Classes/SchoolClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Sweikenb\Library\Dirty\Tests\Testdata\Classes;

class SchoolClass
{
public function __construct(
private Person $teacher,
private array $students
) {
}

public function getTeacher(): Person
{
return $this->teacher;
}

public function setTeacher(Person $teacher): void
{
$this->teacher = $teacher;
}

public function getStudents(): array
{
return $this->students;
}

public function setStudents(Person ...$students): void
{
$this->students = $students;
}
}

0 comments on commit 334b620

Please sign in to comment.