-
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 tests for the normalizer and model
- Loading branch information
Showing
6 changed files
with
229 additions
and
1 deletion.
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,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')) | ||
); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Sweikenb\Library\Dirty\Tests\Testdata\Classes; | ||
|
||
class Person | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly int $age | ||
) { | ||
} | ||
} |
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 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; | ||
} | ||
} |
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,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; | ||
} | ||
} |