hasProperty($propertyName)) { throw new ReflectionException("Unknown property {$propertyName}"); } $property = $reflection->getProperty($propertyName); $property->setAccessible(true); return $property->getValue($object); } /** * Surcharge la méthode assertClassHasAttribute qui disparaitra dans PHPUnit 10 */ public static function assertClassHasAttribute(string $attributeName, string $className, string $message = ''): void { $reflection = new \ReflectionClass($className); self::assertTrue($reflection->hasProperty($attributeName), 'Failed asserting that class "' . $className . '" has attribute "' . $attributeName . '".'); } /** * Teste que la classe possède les propriétés attendues */ public function testExpectedProperties() { $webPage = new SectionWebPage(); foreach ($this->expectedProperties as $expectedProperty) { $this->assertClassHasAttribute($expectedProperty, get_class($webPage)); } } /** * Teste que la classe ne possède que les propriétés attendues */ public function testUnexpectedProperties() { $webPage = new SectionWebPage(); // Other properties ? $reflection = new \ReflectionClass(get_class($webPage)); foreach ($reflection->getProperties() as $property) { $this->assertTrue( in_array($property->getName(), $this->expectedProperties), "Property '{$property->getName()}' should not exist" ); } } /** * Teste la méthode slugify(). * * Utilise les jeux de données fournis par la méthode providerSlugify() * * @dataProvider providerSlugify */ public function testSlugify(string $subject, string $expected): void { $this->assertSame($expected, SectionWebPage::slugify($subject), "«{$subject}»\nshould be slugified to\n«{$expected}»"); } /** * Retourne les jeux de données de test pour la méthode slugify(). * * @return string[][] */ public function providerSlugify(): array { return [ 'identity' => [ 'abcdef', 'abcdef', ], 'case conversion' => [ 'aBcDEf', 'abcdef', ], 'simple spaces' => [ 'a b c d e f', 'a-b-c-d-e-f', ], 'leading and trailing separators removal' => [ "\n \t abcdef \t\n ", 'abcdef', ], 'simple spaces with leading and trailing separators' => [ " \t\n a b c d e f \t \n ", 'a-b-c-d-e-f', ], 'multiple separators in various positions' => [ " \n a \t b \t\n c d e f", 'a-b-c-d-e-f', ], 'french characters transliteration' => [ 'àbçdéf', 'abcdef', ], 'emoji conversion' => [ '😠 ab - 😓 - cd 😕 ef', 'ab-cd-ef', ], 'special characters conversion' => [ 'a b~|`c-d$e%:.,f', 'a-b-c-d-e-f', ], 'leading and trailing special characters removal' => [ '#_{a b~|`c-d$e%:.,f*^', 'a-b-c-d-e-f', ], 'html tags stripping' => [ 'abcdef', 'abcdef', ], 'more html tags stripping' => [ 'abcdef', 'abcdef', ], 'html entities conversion' => [ 'a «b»c def', 'a-b-c-def', ], 'html entities conversion and tags removal' => [ 'a «b»c def', 'a-b-c-def', ], 'full example' => [ "😠#[ A «b\n \t\t»^"'^Ç dÉ
f %*
", 'a-b-c-def', ], ]; } /** * Teste la méthode appendSection() appelée une fois */ public function testAppendSectionOnce() { $fakeTitle = 'fakeTitle'; $fakeContent = 'fakeContent'; $webPage = new SectionWebPage(); $webPage->appendSection($fakeTitle, $fakeContent); $propertyName = 'sections'; $this->assertEquals([$fakeTitle => $fakeContent], self::getPropertyValue($webPage, $propertyName), "The property '{$propertyName}' should contain the value of the section added with appendSection"); } /** * Teste la méthode appendSection() appelée deux fois */ public function testAppendContentTwice() { $fakeTitle1 = 'fakeTitle'; $fakeContent1 = 'fakeBody first section'; $fakeTitle2 = 'fake Title'; $fakeContent2 = 'fakeBody second section'; $webPage = new SectionWebPage(); $webPage->appendSection($fakeTitle1, $fakeContent1); $webPage->appendSection($fakeTitle2, $fakeContent2); $propertyName = 'sections'; $this->assertEquals( [ $fakeTitle1 => $fakeContent1, $fakeTitle2 => $fakeContent2, ], self::getPropertyValue($webPage, $propertyName), "The property '{$propertyName}' should contain the value of the section added with appendSection" ); } }