From 334b620d0a681608c7590de1f1294d27f2e69372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Schr=C3=B6er?= Date: Mon, 1 Jul 2024 17:13:32 +0200 Subject: [PATCH] add tests for the normalizer and model --- src/Model/NormalizedModel.php | 2 +- tests/Model/NormalizedModelTest.php | 34 +++++++ tests/Service/NormalizerServiceTest.php | 128 ++++++++++++++++++++++++ tests/Testdata/Classes/Person.php | 12 +++ tests/Testdata/Classes/School.php | 22 ++++ tests/Testdata/Classes/SchoolClass.php | 32 ++++++ 6 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 tests/Model/NormalizedModelTest.php create mode 100644 tests/Service/NormalizerServiceTest.php create mode 100644 tests/Testdata/Classes/Person.php create mode 100644 tests/Testdata/Classes/School.php create mode 100644 tests/Testdata/Classes/SchoolClass.php diff --git a/src/Model/NormalizedModel.php b/src/Model/NormalizedModel.php index 8d784ec..e569554 100644 --- a/src/Model/NormalizedModel.php +++ b/src/Model/NormalizedModel.php @@ -26,7 +26,7 @@ public function getDiffFieldPaths(NormalizedModel $other): array } $diffFieldPaths = array_unique($diffFieldPaths); - ksort($diffFieldPaths); + sort($diffFieldPaths); return $diffFieldPaths; } diff --git a/tests/Model/NormalizedModelTest.php b/tests/Model/NormalizedModelTest.php new file mode 100644 index 0000000..1141352 --- /dev/null +++ b/tests/Model/NormalizedModelTest.php @@ -0,0 +1,34 @@ + 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')) + ); + } +} diff --git a/tests/Service/NormalizerServiceTest.php b/tests/Service/NormalizerServiceTest.php new file mode 100644 index 0000000..b167328 --- /dev/null +++ b/tests/Service/NormalizerServiceTest.php @@ -0,0 +1,128 @@ + '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); + } +} diff --git a/tests/Testdata/Classes/Person.php b/tests/Testdata/Classes/Person.php new file mode 100644 index 0000000..3197964 --- /dev/null +++ b/tests/Testdata/Classes/Person.php @@ -0,0 +1,12 @@ +classes; + } + + public function setClasses(array $classes): void + { + $this->classes = $classes; + } +} diff --git a/tests/Testdata/Classes/SchoolClass.php b/tests/Testdata/Classes/SchoolClass.php new file mode 100644 index 0000000..21b00a7 --- /dev/null +++ b/tests/Testdata/Classes/SchoolClass.php @@ -0,0 +1,32 @@ +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; + } +}