From d98f6a1c252000bc2331d4be31a80a1f2a5bcfeb Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Wed, 10 Feb 2021 14:48:53 +0100 Subject: [PATCH] Add types to tests --- tests/ExportClosureTest.php | 26 +++++++++++----------- tests/ExportObjectTest.php | 44 ++++++++++++++++++------------------- tests/VarExporterTest.php | 28 ++++++++++++----------- 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/tests/ExportClosureTest.php b/tests/ExportClosureTest.php index becc5b3..c00ddb7 100644 --- a/tests/ExportClosureTest.php +++ b/tests/ExportClosureTest.php @@ -20,7 +20,7 @@ */ class ExportClosureTest extends AbstractTestCase { - public function testExportSimpleClosure() + public function testExportSimpleClosure(): void { $var = function() { return 'Hello, world!'; @@ -35,7 +35,7 @@ function () { $this->assertExportEquals($expected, $var); } - public function testExportNestedComplexClosure() + public function testExportNestedComplexClosure(): void { $var = [ (object) [ @@ -76,7 +76,7 @@ public function testExportNestedComplexClosure() $this->assertExportEquals($expected, $var); } - public function testExportNamespacedCode() + public function testExportNamespacedCode(): void { $var = function(SetState $a) : array { return [ @@ -104,7 +104,7 @@ function (\Brick\VarExporter\Tests\Classes\SetState $a) : array { $this->assertExportEquals($expected, $var); } - public function testExportClosureWithStringsContainingLikeBreaks() + public function testExportClosureWithStringsContainingLikeBreaks(): void { $var = function() { $a = 'Hello, @@ -143,7 +143,7 @@ public function testExportClosureWithStringsContainingLikeBreaks() $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN); } - public function testExportClosureWithUse() + public function testExportClosureWithUse(): void { $foo = 'bar'; @@ -158,7 +158,7 @@ public function testExportClosureWithUse() ); } - public function testExportClosureWithUseAsVars() + public function testExportClosureWithUseAsVars(): void { $foo = 'b' . 'a' . 'r'; @@ -177,7 +177,7 @@ public function testExportClosureWithUseAsVars() $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES); } - public function testExportClosureWithUseClosure() + public function testExportClosureWithUseClosure(): void { $foo = 'b' . 'a' . 'r'; @@ -204,7 +204,7 @@ public function testExportClosureWithUseClosure() } - public function testExportArrowFunction() + public function testExportArrowFunction(): void { if (version_compare(PHP_VERSION, '7.4.0') < 0) { $this->markTestSkipped("Arrow functions aren't supported in PHP " . PHP_VERSION); @@ -224,7 +224,7 @@ function ($planet) { $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN); } - public function testExportArrowFunctionWithContext() + public function testExportArrowFunctionWithContext(): void { if (version_compare(PHP_VERSION, '7.4.0') < 0) { $this->markTestSkipped("Arrow functions aren't supported in PHP " . PHP_VERSION); @@ -241,7 +241,7 @@ public function testExportArrowFunctionWithContext() ); } - public function testExportArrowFunctionWithContextVarAsVar() + public function testExportArrowFunctionWithContextVarAsVar(): void { if (version_compare(PHP_VERSION, '7.4.0') < 0) { $this->markTestSkipped("Arrow functions aren't supported in PHP " . PHP_VERSION); @@ -264,7 +264,7 @@ function ($planet) { $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES); } - public function testExportClosureDefinedInEval() + public function testExportClosureDefinedInEval(): void { $var = eval(<<assertExportThrows("Closure defined in eval()'d code cannot be exported.", $var); } - public function testExportTwoClosuresOnSameLine() + public function testExportTwoClosuresOnSameLine(): void { $var = function() { return function() {}; }; $this->assertExportThrows("Expected exactly 1 closure in */tests/ExportClosureTest.php on line *, found 2.", $var); } - public function testExportClosureDisabled() + public function testExportClosureDisabled(): void { $var = function() { return 'Hello, world!'; diff --git a/tests/ExportObjectTest.php b/tests/ExportObjectTest.php index f487fb5..9c79f6f 100644 --- a/tests/ExportObjectTest.php +++ b/tests/ExportObjectTest.php @@ -21,7 +21,7 @@ */ class ExportObjectTest extends AbstractTestCase { - public function testExportStdClass() + public function testExportStdClass(): void { $object = new \stdClass; $object->foo = 'Hello'; @@ -42,7 +42,7 @@ public function testExportStdClass() $this->assertExportEquals($expected, $object); } - public function testExportClassWithNoProperties() + public function testExportClassWithNoProperties(): void { $object = new NoProperties; @@ -51,7 +51,7 @@ public function testExportClassWithNoProperties() $this->assertExportEquals($expected, $object); } - public function testExportClassWithDynamicPropertiesOnly() + public function testExportClassWithDynamicPropertiesOnly(): void { $object = new NoProperties; $object->x = 1.0; @@ -75,7 +75,7 @@ public function testExportClassWithDynamicPropertiesOnly() $this->assertExportEquals($expected, $object); } - public function testExportClassWithDynamicPropertiesOnly_SkipDynamicProperties() + public function testExportClassWithDynamicPropertiesOnly_SkipDynamicProperties(): void { $object = new NoProperties; $object->dynamicProp = 'Hello'; @@ -86,7 +86,7 @@ public function testExportClassWithDynamicPropertiesOnly_SkipDynamicProperties() $this->assertExportEquals($expected, $object, VarExporter::SKIP_DYNAMIC_PROPERTIES); } - public function testExportClassWithConstructorAndNoProperties() + public function testExportClassWithConstructorAndNoProperties(): void { $object = new ConstructorAndNoProperties(); @@ -102,7 +102,7 @@ public function testExportClassWithConstructorAndNoProperties() $this->assertExportEquals($expected, $object, VarExporter::SKIP_DYNAMIC_PROPERTIES); } - public function testExportClassWithPublicPropertiesOnly() + public function testExportClassWithPublicPropertiesOnly(): void { $object = new PublicPropertiesOnly; $object->foo = 'Hello'; @@ -122,7 +122,7 @@ public function testExportClassWithPublicPropertiesOnly() $this->assertExportEquals($expected, $object); } - public function testExportObjectWithPublicAndDynamicProperties() + public function testExportObjectWithPublicAndDynamicProperties(): void { $object = new PublicPropertiesOnly; $object->foo = 'Hello'; @@ -144,7 +144,7 @@ public function testExportObjectWithPublicAndDynamicProperties() $this->assertExportEquals($expected, $object); } - public function testExportObjectWithPublicAndDynamicProperties_SkipDynamicProperties() + public function testExportObjectWithPublicAndDynamicProperties_SkipDynamicProperties(): void { $object = new PublicPropertiesOnly; $object->foo = 'Hello'; @@ -165,7 +165,7 @@ public function testExportObjectWithPublicAndDynamicProperties_SkipDynamicProper $this->assertExportEquals($expected, $object, VarExporter::SKIP_DYNAMIC_PROPERTIES); } - public function testExportClassWithSetState() + public function testExportClassWithSetState(): void { $object = new SetState; $object->foo = 'Hello'; @@ -182,7 +182,7 @@ public function testExportClassWithSetState() $this->assertExportEquals($expected, $object); } - public function testExportClassWithSetStateAndUnsetProperties() + public function testExportClassWithSetStateAndUnsetProperties(): void { $object = new SetState; $object->foo = null; @@ -199,7 +199,7 @@ public function testExportClassWithSetStateAndUnsetProperties() $this->assertExportEquals($expected, $object); } - public function testExportClassWithSetStateAndDynamicProperties() + public function testExportClassWithSetStateAndDynamicProperties(): void { $object = new SetState; $object->foo = 'Hello'; @@ -220,7 +220,7 @@ public function testExportClassWithSetStateAndDynamicProperties() $this->assertExportEquals($expected, $object); } - public function testExportClassWithSetStateAndDynamicProperties_SkipDynamicProperties() + public function testExportClassWithSetStateAndDynamicProperties_SkipDynamicProperties(): void { $object = new SetState; $object->foo = 'Hello'; @@ -238,7 +238,7 @@ public function testExportClassWithSetStateAndDynamicProperties_SkipDynamicPrope $this->assertExportEquals($expected, $object, VarExporter::SKIP_DYNAMIC_PROPERTIES); } - public function testExportClassWithSetStateAndOverriddenPrivateProperties() + public function testExportClassWithSetStateAndOverriddenPrivateProperties(): void { $object = new SetStateWithOverriddenPrivateProperties; @@ -247,7 +247,7 @@ public function testExportClassWithSetStateAndOverriddenPrivateProperties() $this->assertExportThrows($expectedMessage, $object); } - public function testExportClassWithPrivateConstructor() + public function testExportClassWithPrivateConstructor(): void { $object = PrivateConstructor::create(); $object->foo = 'Foo'; @@ -268,7 +268,7 @@ public function testExportClassWithPrivateConstructor() $this->assertExportEquals($expected, $object); } - public function testExportClassWithPublicPropertiesAndConstructor() + public function testExportClassWithPublicPropertiesAndConstructor(): void { $object = new PublicPropertiesWithConstructor(); @@ -287,7 +287,7 @@ public function testExportClassWithPublicPropertiesAndConstructor() $this->assertExportEquals($expected, $object); } - public function testExportClassWithSerializeMagicMethods() + public function testExportClassWithSerializeMagicMethods(): void { $object = new SerializeMagicMethods; @@ -313,7 +313,7 @@ public function testExportClassWithSerializeMagicMethods() $this->assertExportEquals($expected, $object); } - public function testExportClassWithSerializeMagicMethodsAndConstructor() + public function testExportClassWithSerializeMagicMethodsAndConstructor(): void { $object = new SerializeMagicMethodsWithConstructor('Test', 1234); @@ -334,7 +334,7 @@ public function testExportClassWithSerializeMagicMethodsAndConstructor() $this->assertExportEquals($expected, $object); } - public function testExportClassWithSerializeMagicMethodsAndConstructor_AddTypeHints() + public function testExportClassWithSerializeMagicMethodsAndConstructor_AddTypeHints(): void { $object = new SerializeMagicMethodsWithConstructor('Test', 1234); @@ -357,7 +357,7 @@ public function testExportClassWithSerializeMagicMethodsAndConstructor_AddTypeHi $this->assertExportEquals($expected, $object, VarExporter::ADD_TYPE_HINTS); } - public function testExportClassHierarchy() + public function testExportClassHierarchy(): void { $object = Hierarchy\C::create(); $object->dynamicProperty = 'A property declared dynamically'; @@ -397,7 +397,7 @@ public function testExportClassHierarchy() $this->assertExportEquals($expected, $object); } - public function testExportClassHierarchy_AddTypeHints_SkipDynamicProperties() + public function testExportClassHierarchy_AddTypeHints_SkipDynamicProperties(): void { $object = Hierarchy\C::create(); $object->dynamicProperty = 'A property declared dynamically'; @@ -441,7 +441,7 @@ public function testExportClassHierarchy_AddTypeHints_SkipDynamicProperties() $this->assertExportEquals($expected, $object, VarExporter::ADD_TYPE_HINTS | VarExporter::SKIP_DYNAMIC_PROPERTIES); } - public function testExportClassHierarchyWithUnsetProperties() + public function testExportClassHierarchyWithUnsetProperties(): void { $object = Hierarchy\C::create(); @@ -493,7 +493,7 @@ public function testExportClassHierarchyWithUnsetProperties() $this->assertExportEquals($expected, $object); } - public function testExportObjectWithRestrictiveOptions() + public function testExportObjectWithRestrictiveOptions(): void { $object = new PublicPropertiesOnly(); diff --git a/tests/VarExporterTest.php b/tests/VarExporterTest.php index 7b5846a..fb55120 100644 --- a/tests/VarExporterTest.php +++ b/tests/VarExporterTest.php @@ -11,7 +11,7 @@ class VarExporterTest extends AbstractTestCase { - public function testMixedVar() + public function testMixedVar(): void { $myObject = new PublicPropertiesOnly(); $myObject->foo = 'hello'; @@ -102,7 +102,7 @@ public function testMixedVar() $this->assertExportEquals($expected, $var); } - public function testExportObjectPropWithSpecialChars() + public function testExportObjectPropWithSpecialChars(): void { $object = new PublicPropertiesOnly; $object->{'$ref'} = '#/components/schemas/User'; @@ -122,7 +122,7 @@ public function testExportObjectPropWithSpecialChars() $this->assertExportEquals($expected, $object); } - public function testAddReturn() + public function testAddReturn(): void { $var = []; $expected = 'return [];' . PHP_EOL; @@ -130,7 +130,7 @@ public function testAddReturn() $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN); } - public function testInlineNumericScalarArray() + public function testInlineNumericScalarArray(): void { $var = [ 'one' => ['hello', 'world', 123, true, false, null, 7.5], @@ -151,7 +151,7 @@ public function testInlineNumericScalarArray() $this->assertExportEquals($expected, $var, VarExporter::INLINE_NUMERIC_SCALAR_ARRAY); } - public function testTrailingCommaInArray() + public function testTrailingCommaInArray(): void { $var = [ 'one' => ['hello', 'world', 123, true, false, null, 7.5], @@ -172,7 +172,7 @@ public function testTrailingCommaInArray() $this->assertExportEquals($expected, $var, VarExporter::INLINE_NUMERIC_SCALAR_ARRAY | VarExporter::TRAILING_COMMA_IN_ARRAY); } - public function testExportDateTime() + public function testExportDateTime(): void { $timezone = new \DateTimeZone('Europe/Berlin'); $format = 'Y-m-d H:i:s.u'; @@ -190,7 +190,7 @@ public function testExportDateTime() $this->assertExportEquals($expected, $var); } - public function testExportDateTimeImmutable() + public function testExportDateTimeImmutable(): void { $timezone = new \DateTimeZone('Europe/Berlin'); $format = 'Y-m-d H:i:s.u'; @@ -208,7 +208,7 @@ public function testExportDateTimeImmutable() $this->assertExportEquals($expected, $var); } - public function testExportInternalClass() + public function testExportInternalClass(): void { $object = new \stdClass; $object->iterator = new \ArrayIterator(); @@ -218,7 +218,7 @@ public function testExportInternalClass() $this->assertExportThrows($expectedMessage, $object); } - public function testExportResource() + public function testExportResource(): void { $handle = fopen('php://memory', 'rb+'); @@ -235,7 +235,7 @@ public function testExportResource() VarExporter::export($object); } - public function testExportObjectTwiceWithoutCircularReference() + public function testExportObjectTwiceWithoutCircularReference(): void { $a = new PublicPropertiesOnly; $a->foo = 'Foo'; @@ -270,7 +270,7 @@ public function testExportObjectTwiceWithoutCircularReference() $this->assertExportEquals($expected, $var); } - public function testExportObjectWithCircularReference() + public function testExportObjectWithCircularReference(): void { $a = new PublicPropertiesOnly; $b = new PublicPropertiesOnly; @@ -290,8 +290,10 @@ public function testExportObjectWithCircularReference() /** * @dataProvider providerExportIndented + * + * @param mixed $var */ - public function testExportIndented($var, $expected, $options) + public function testExportIndented($var, string $expected, int $options): void { $template = <<<'TPL' public function foo() @@ -306,7 +308,7 @@ public function foo() $this->assertEquals($expected, $result); } - public function providerExportIndented() + public function providerExportIndented(): iterable { // Array $var = ['one' => ['hello', true], 'two' => 2];