diff --git a/tests/ArrayTest.php b/tests/ArrayTest.php index 6161dfead..eb0304699 100644 --- a/tests/ArrayTest.php +++ b/tests/ArrayTest.php @@ -35,11 +35,10 @@ public function testMapTypedArray() new JsonMapperTest_Array() ); $this->assertIsArray($sn->typedArray); - $this->assertEquals(2, count($sn->typedArray)); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn->typedArray[0]); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn->typedArray[1]); - $this->assertEquals('stringvalue', $sn->typedArray[0]->str); - $this->assertEquals(1.2, $sn->typedArray[1]->fl); + $this->assertCount(2, $sn->typedArray); + $this->assertContainsOnlyInstancesOf(JsonMapperTest_Simple::class, $sn->typedArray); + $this->assertSame('stringvalue', $sn->typedArray[0]->str); + $this->assertSame(1.2, $sn->typedArray[1]->fl); } /** @@ -54,14 +53,14 @@ public function testMapTypedSimpleArray() new JsonMapperTest_Array() ); $this->assertIsArray($sn->typedSimpleArray); - $this->assertEquals(3, count($sn->typedSimpleArray)); - $this->assertInstanceOf('DateTime', $sn->typedSimpleArray[0]); + $this->assertCount(3, $sn->typedSimpleArray); + $this->assertInstanceOf(DateTime::class, $sn->typedSimpleArray[0]); $this->assertNull($sn->typedSimpleArray[1]); - $this->assertInstanceOf('DateTime', $sn->typedSimpleArray[2]); - $this->assertEquals( + $this->assertInstanceOf(DateTime::class, $sn->typedSimpleArray[2]); + $this->assertSame( '2014-01-02', $sn->typedSimpleArray[0]->format('Y-m-d') ); - $this->assertEquals( + $this->assertSame( '2014-05-07', $sn->typedSimpleArray[2]->format('Y-m-d') ); } @@ -84,7 +83,7 @@ public function testMapArrayJsonNoTypeEnforcement() $jm = new JsonMapper(); $jm->bEnforceMapType = false; $sn = $jm->map(array(), new JsonMapperTest_Simple()); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn); + $this->assertInstanceOf(JsonMapperTest_Simple::class, $sn); } /** @@ -97,11 +96,7 @@ public function testFlArray() json_decode('{"flArray":[1.23,3.14,2.048]}'), new JsonMapperTest_Array() ); - $this->assertIsArray($sn->flArray); - $this->assertEquals(3, count($sn->flArray)); - $this->assertTrue(is_float($sn->flArray[0])); - $this->assertTrue(is_float($sn->flArray[1])); - $this->assertTrue(is_float($sn->flArray[2])); + $this->assertSame([1.23, 3.14, 2.048], $sn->flArray); } /** @@ -114,11 +109,7 @@ public function testFlArrayKeyed() json_decode('{"flArray":{"foo":1.23,"bar":3.14,"baz":2.048}}'), new JsonMapperTest_Array() ); - $this->assertIsArray($sn->flArray); - $this->assertEquals(3, count($sn->flArray)); - $this->assertTrue(is_float($sn->flArray['foo'])); - $this->assertTrue(is_float($sn->flArray['bar'])); - $this->assertTrue(is_float($sn->flArray['baz'])); + $this->assertSame(['foo' => 1.23, 'bar' => 3.14, 'baz' => 2.048], $sn->flArray); } /** @@ -131,11 +122,7 @@ public function testStrArray() json_decode('{"strArray":["str",false,2.048]}'), new JsonMapperTest_Array() ); - $this->assertIsArray($sn->strArray); - $this->assertEquals(3, count($sn->strArray)); - $this->assertIsString($sn->strArray[0]); - $this->assertIsString($sn->strArray[1]); - $this->assertIsString($sn->strArray[2]); + $this->assertSame(["str", "", "2.048"], $sn->strArray); } /** @@ -148,11 +135,7 @@ public function testStrArrayV2() json_decode('{"strArrayV2":["str",false,2.048]}'), new JsonMapperTest_Array() ); - $this->assertIsArray($sn->strArrayV2); - $this->assertEquals(3, count($sn->strArrayV2)); - $this->assertIsString($sn->strArrayV2[0]); - $this->assertIsString($sn->strArrayV2[1]); - $this->assertIsString($sn->strArrayV2[2]); + $this->assertSame(["str", "", "2.048"], $sn->strArrayV2); } /** @@ -162,15 +145,15 @@ public function testMapArrayObject() { $jm = new JsonMapper(); $sn = $jm->map( - json_decode('{"pArrayObject":[{"str":"stringvalue"},{"fl":"1.2"}]}'), + json_decode('{"pArrayObject":[{"str":"stringvalue"},{"fl":"1.2"},{"fl":1.2}]}'), new JsonMapperTest_Array() ); - $this->assertInstanceOf('ArrayObject', $sn->pArrayObject); - $this->assertEquals(2, count($sn->pArrayObject)); - $this->assertInstanceOf('\stdClass', $sn->pArrayObject[0]); - $this->assertInstanceOf('\stdClass', $sn->pArrayObject[1]); - $this->assertEquals('stringvalue', $sn->pArrayObject[0]->str); - $this->assertEquals('1.2', $sn->pArrayObject[1]->fl); + $this->assertInstanceOf(ArrayObject::class, $sn->pArrayObject); + $this->assertCount(3, $sn->pArrayObject); + $this->assertContainsOnlyInstancesOf(stdClass::class, $sn->pArrayObject); + $this->assertSame('stringvalue', $sn->pArrayObject[0]->str); + $this->assertSame("1.2", $sn->pArrayObject[1]->fl); + $this->assertSame(1.2, $sn->pArrayObject[2]->fl); } /** @@ -180,17 +163,14 @@ public function testMapTypedArrayObject() { $jm = new JsonMapper(); $sn = $jm->map( - json_decode( - '{"pTypedArrayObject":[{"str":"stringvalue"},{"fl":"1.2"}]}' - ), + json_decode('{"pTypedArrayObject":[{"str":"stringvalue"},{"fl":"1.2"}]}'), new JsonMapperTest_Array() ); - $this->assertInstanceOf('ArrayObject', $sn->pTypedArrayObject); - $this->assertEquals(2, count($sn->pTypedArrayObject)); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn->pTypedArrayObject[0]); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn->pTypedArrayObject[1]); - $this->assertEquals('stringvalue', $sn->pTypedArrayObject[0]->str); - $this->assertEquals('1.2', $sn->pTypedArrayObject[1]->fl); + $this->assertInstanceOf(ArrayObject::class, $sn->pTypedArrayObject); + $this->assertCount(2, $sn->pTypedArrayObject); + $this->assertContainsOnlyInstancesOf(JsonMapperTest_Simple::class, $sn->pTypedArrayObject); + $this->assertSame('stringvalue', $sn->pTypedArrayObject[0]->str); + $this->assertSame(1.2, $sn->pTypedArrayObject[1]->fl); } /** @@ -200,33 +180,26 @@ public function testMapSimpleArrayObject() { $jm = new JsonMapper(); $sn = $jm->map( - json_decode( - '{"pSimpleArrayObject":{"eins":"1","zwei":"1.2"}}' - ), + json_decode('{"pSimpleArrayObject":{"eins":"1","zwei":"1.2"}}'), new JsonMapperTest_Array() ); - $this->assertInstanceOf('ArrayObject', $sn->pSimpleArrayObject); - $this->assertEquals(2, count($sn->pSimpleArrayObject)); - $this->assertIsInt($sn->pSimpleArrayObject['eins']); - $this->assertIsInt($sn->pSimpleArrayObject['zwei']); - $this->assertEquals(1, $sn->pSimpleArrayObject['eins']); - $this->assertEquals(1, $sn->pSimpleArrayObject['zwei']); + $this->assertInstanceOf(ArrayObject::class, $sn->pSimpleArrayObject); + $this->assertCount(2, $sn->pSimpleArrayObject); + $this->assertContainsOnly('int', $sn->pSimpleArrayObject, true); + $this->assertSame(1, $sn->pSimpleArrayObject['eins']); + $this->assertSame(1, $sn->pSimpleArrayObject['zwei']); } public function testMapSimpleArrayAccess() { $jm = new JsonMapper(); $sn = $jm->map( - json_decode( - '{"pArrayAccessCollection":{"eins": 1,"zwei": "two"}}' - ), + json_decode('{"pArrayAccessCollection":{"eins": 1,"zwei": "two"}}'), new JsonMapperTest_Array() ); - $this->assertInstanceOf('ArrayAccess', $sn->pArrayAccessCollection); - $this->assertIsInt($sn->pArrayAccessCollection['eins']); - $this->assertIsString($sn->pArrayAccessCollection['zwei']); - $this->assertEquals(1, $sn->pArrayAccessCollection['eins']); - $this->assertEquals("two", $sn->pArrayAccessCollection['zwei']); + $this->assertInstanceOf(ArrayAccess::class, $sn->pArrayAccessCollection); + $this->assertSame(1, $sn->pArrayAccessCollection['eins']); + $this->assertSame("two", $sn->pArrayAccessCollection['zwei']); } public function testInvalidArray() @@ -313,8 +286,8 @@ public function testMapTypedArrayObjectDoesNotExist() ), new JsonMapperTest_Broken() ); - $this->assertInstanceOf('ArrayObject', $sn->pTypedArrayObjectNoClass); - $this->assertEquals(1, count($sn->pTypedArrayObjectNoClass)); + $this->assertInstanceOf(ArrayObject::class, $sn->pTypedArrayObjectNoClass); + $this->assertCount(1, $sn->pTypedArrayObjectNoClass); $this->assertInstanceOf( 'ThisClassDoesNotExist', $sn->pTypedArrayObjectNoClass[0] ); @@ -348,13 +321,12 @@ public function testArrayObjectList() json_decode('{"pArrayObjectList": [{"x":"X"},{"y":"Y"}]}'), new JsonMapperTest_Array() ); - $this->assertNotNull($sn->pArrayObjectList); $this->assertIsArray($sn->pArrayObjectList); $this->assertCount(2, $sn->pArrayObjectList); $this->assertContainsOnlyInstancesOf(\ArrayObject::class, $sn->pArrayObjectList); // test first element data $ao = $sn->pArrayObjectList[0]; - $this->assertEquals(['x' => 'X'], $ao->getArrayCopy()); + $this->assertSame(['x' => 'X'], $ao->getArrayCopy()); } /** @@ -368,13 +340,12 @@ public function testArrayObjectSubclassList() json_decode('{"pArrayObjectSubclassList": [{"x":"X"},{"y":"Y"}]}'), new JsonMapperTest_Array() ); - $this->assertNotNull($sn->pArrayObjectSubclassList); $this->assertIsArray($sn->pArrayObjectSubclassList); $this->assertCount(2, $sn->pArrayObjectSubclassList); $this->assertContainsOnlyInstancesOf(MyArrayObject::class, $sn->pArrayObjectSubclassList); // test first element data $ao = $sn->pArrayObjectSubclassList[0]; - $this->assertEquals(['x' => 'X'], $ao->getArrayCopy()); + $this->assertSame(['x' => 'X'], $ao->getArrayCopy()); } /** @@ -387,18 +358,7 @@ public function testNMatrix() json_decode('{"nMatrix":[[1,2],[3,4],[5]]}'), new JsonMapperTest_Array() ); - $this->assertIsArray($sn->nMatrix); - $this->assertEquals(3, count($sn->nMatrix)); - $this->assertIsArray($sn->nMatrix[0]); - $this->assertIsArray($sn->nMatrix[1]); - $this->assertIsArray($sn->nMatrix[2]); - - $this->assertEquals(2, count($sn->nMatrix[0])); - $this->assertIsInt($sn->nMatrix[0][0]); - $this->assertIsInt($sn->nMatrix[0][1]); - - $this->assertEquals(2, count($sn->nMatrix[1])); - $this->assertEquals(1, count($sn->nMatrix[2])); + $this->assertSame([[1,2],[3,4],[5]], $sn->nMatrix); } /** @@ -413,17 +373,18 @@ public function testObjectMultiverse() new JsonMapperTest_Array() ); $this->assertIsArray($sn->pMultiverse); - $this->assertEquals(1, count($sn->pMultiverse)); + $this->assertCount(1, $sn->pMultiverse); $this->assertIsArray($sn->pMultiverse[0]); - $this->assertEquals(1, count($sn->pMultiverse[0])); + $this->assertCount(1, $sn->pMultiverse[0]); $this->assertIsArray($sn->pMultiverse[0][0]); - $this->assertEquals(1, count($sn->pMultiverse[0][0])); + $this->assertCount(1, $sn->pMultiverse[0][0]); $this->assertInstanceOf( - 'JsonMapperTest_Simple', $sn->pMultiverse[0][0][0] + JsonMapperTest_Simple::class, $sn->pMultiverse[0][0][0] ); + $this->assertSame(23, $sn->pMultiverse[0][0][0]->pint); } /** @@ -436,7 +397,7 @@ public function testMapArray() json_decode('[1,2,3]'), [] ); - $this->assertEquals([1, 2, 3], $mapped); + $this->assertSame([1, 2, 3], $mapped); } /** @@ -450,7 +411,7 @@ public function testMapArrayStrangeKeys() ['en-US' => 'foo', 'de-DE' => 'bar'], [] ); - $this->assertEquals(['en-US' => 'foo', 'de-DE' => 'bar'], $mapped); + $this->assertSame(['en-US' => 'foo', 'de-DE' => 'bar'], $mapped); } /** @@ -464,10 +425,10 @@ public function testMapTypedSimpleArrayFromObject() new JsonMapperTest_Array() ); $this->assertIsArray($sn->typedSimpleArray); - $this->assertEquals(1, count($sn->typedSimpleArray)); + $this->assertCount(1, $sn->typedSimpleArray); $this->assertArrayHasKey('en-US', $sn->typedSimpleArray); - $this->assertInstanceOf('DateTime', $sn->typedSimpleArray['en-US']); - $this->assertEquals( + $this->assertInstanceOf(DateTime::class, $sn->typedSimpleArray['en-US']); + $this->assertSame( '2014-01-02', $sn->typedSimpleArray['en-US']->format('Y-m-d') ); } @@ -484,8 +445,6 @@ public function testObjectInsteadOfString() json_decode('{"strArray":[{}]}'), new JsonMapperTest_Array() ); - $this->assertIsArray($sn->strArray); - $this->assertNotEmpty($sn->strArray); } public function testPolymorphicArray() @@ -511,13 +470,13 @@ public function testPolymorphicArray() }; $zoo = $jm->map(json_decode($zooJson), new Zoo()); - $this->assertEquals(2, count($zoo->animals)); + $this->assertCount(2, $zoo->animals); $this->assertInstanceOf(Cat::class, $zoo->animals[0]); - $this->assertEquals('Lion', $zoo->animals[0]->name); + $this->assertSame('Lion', $zoo->animals[0]->name); $this->assertInstanceOf(Fish::class, $zoo->animals[1]); - $this->assertEquals('Clown Fish', $zoo->animals[1]->name); + $this->assertSame('Clown Fish', $zoo->animals[1]->name); } public function testMapArrayFromVariadicFunctionWithSimpleType() @@ -530,20 +489,7 @@ public function testMapArrayFromVariadicFunctionWithSimpleType() ); $variadicArray = $sn->getVariadicInt(); - $this->assertIsArray($variadicArray); - $this->assertEquals(3, count($variadicArray)); - $this->assertIsInt($variadicArray[0]); - $this->assertIsInt($variadicArray[1]); - $this->assertIsInt($variadicArray[2]); - $this->assertEquals( - 1, $variadicArray[0] - ); - $this->assertEquals( - 2, $variadicArray[1] - ); - $this->assertEquals( - 3, $variadicArray[2] - ); + $this->assertSame([1,2,3], $variadicArray); } public function testMapArrayFromVariadicFunctionWithObjectType() @@ -556,16 +502,10 @@ public function testMapArrayFromVariadicFunctionWithObjectType() ); $variadicArray = $sn->getVariadicDateTime(); - $this->assertIsArray($variadicArray); - $this->assertEquals(2, count($variadicArray)); - $this->assertInstanceOf('DateTime', $variadicArray[0]); - $this->assertInstanceOf('DateTime', $variadicArray[1]); - $this->assertEquals( - '2014-01-02', $variadicArray[0]->format('Y-m-d') - ); - $this->assertEquals( - '2014-05-07', $variadicArray[1]->format('Y-m-d') - ); + $this->assertCount(2, $variadicArray); + $this->assertContainsOnlyInstancesOf(DateTime::class, $variadicArray); + $this->assertSame('2014-01-02', $variadicArray[0]->format('Y-m-d')); + $this->assertSame('2014-05-07', $variadicArray[1]->format('Y-m-d')); } } diff --git a/tests/Array_PHP74_Test.php b/tests/Array_PHP74_Test.php index 6b14e7ccf..52087386c 100644 --- a/tests/Array_PHP74_Test.php +++ b/tests/Array_PHP74_Test.php @@ -21,7 +21,7 @@ public function testJsonMapper() $json = json_decode('{"files": ["test.txt"]}'); $jsonMapper = new \JsonMapper(); $array = $jsonMapper->map($json, new JsonMapperTest_PHP74Array()); - self::assertCount(1, $array->files); + $this->assertCount(1, $array->files); } public function testMapArrayValueToStringProperty() diff --git a/tests/Array_PHP80_Test.php b/tests/Array_PHP80_Test.php index a8c8a3ced..c1c8829f4 100644 --- a/tests/Array_PHP80_Test.php +++ b/tests/Array_PHP80_Test.php @@ -22,7 +22,7 @@ public function testJsonMapper() $jsonMapper = new \JsonMapper(); $jsonMapper->bIgnoreVisibility = true; $array = $jsonMapper->map($json, JsonMapperTest_PHP80Array::class); - self::assertCount(1, $array->getFiles()); - self::assertInstanceOf(JsonMapperTest_ArrayValueForStringProperty::class, $array->getFiles()[0]); + $this->assertCount(1, $array->getFiles()); + $this->assertInstanceOf(JsonMapperTest_ArrayValueForStringProperty::class, $array->getFiles()[0]); } } diff --git a/tests/ClassMapTest.php b/tests/ClassMapTest.php index b14e12289..e4e47496a 100644 --- a/tests/ClassMapTest.php +++ b/tests/ClassMapTest.php @@ -30,8 +30,8 @@ public function __invoke($class, $jvalue) $testCase = $this; // the class/interface to be mapped - $testCase->assertEquals($testCase::CLASS_MAP_CLASS, $class); - $testCase->assertEquals($testCase::CLASS_MAP_DATA, $jvalue); + $testCase->assertSame($testCase::CLASS_MAP_CLASS, $class); + $testCase->assertSame($testCase::CLASS_MAP_DATA, $jvalue); return 'DateTime'; } @@ -48,8 +48,8 @@ public function classMapTestData() 'name' => ['DateTime'], 'function' => [function ($class, $jvalue) use ($testCase) { // the class/interface to be mapped - $testCase->assertEquals($testCase::CLASS_MAP_CLASS, $class); - $testCase->assertEquals($testCase::CLASS_MAP_DATA, $jvalue); + $testCase->assertSame($testCase::CLASS_MAP_CLASS, $class); + $testCase->assertSame($testCase::CLASS_MAP_DATA, $jvalue); return 'DateTime'; }], 'invoke' => [$this], // __invoke @@ -68,9 +68,8 @@ public function testClassMap($classMapValue) new JsonMapperTest_Object() ); - $this->assertIsObject($sn->pPlainObject); - $this->assertInstanceOf('DateTime', $sn->pPlainObject); - $this->assertEquals( + $this->assertInstanceOf(DateTime::class, $sn->pPlainObject); + $this->assertSame( self::CLASS_MAP_DATA, $sn->pPlainObject->format('c') ); @@ -123,8 +122,8 @@ public function testMapArraySubtype() new JsonMapperTest_Array() ); $this->assertIsArray($data->typedSimpleArray); - $this->assertEquals(1, count($data->typedSimpleArray)); - $this->assertIsString($data->typedSimpleArray[0]); + $this->assertCount(1, $data->typedSimpleArray); + $this->assertSame("2019-03-23", $data->typedSimpleArray[0]); } } ?> diff --git a/tests/Enums_PHP81_Test.php b/tests/Enums_PHP81_Test.php index addd2beea..2f15d79ea 100644 --- a/tests/Enums_PHP81_Test.php +++ b/tests/Enums_PHP81_Test.php @@ -28,7 +28,7 @@ public function testEnumMapping() new \Enums\ObjectWithEnum() ); - $this->assertEquals(\Enums\StringBackedEnum::FOO, $sn->stringBackedEnum); - $this->assertEquals(\Enums\IntBackedEnum::BAR, $sn->intBackedEnum); + $this->assertSame(\Enums\StringBackedEnum::FOO, $sn->stringBackedEnum); + $this->assertSame(\Enums\IntBackedEnum::BAR, $sn->intBackedEnum); } } diff --git a/tests/EventTest.php b/tests/EventTest.php index 8089b67d0..0c69bb559 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -39,7 +39,7 @@ public function testDeserializePostEvent() new JsonMapperTest_EventObject() ); $this->assertIsString($sn->pStr); - $this->assertEquals('two', $sn->pStr); + $this->assertSame('two', $sn->pStr); } public function testDeserializePostEventArguments() @@ -53,7 +53,7 @@ public function testDeserializePostEventArguments() new JsonMapperTest_EventObject() ); $this->assertIsString($sn->pStr); - $this->assertEquals('barbarbar', $sn->pStr); + $this->assertSame('barbarbar', $sn->pStr); } } ?> diff --git a/tests/MixedType_PHP80_Test.php b/tests/MixedType_PHP80_Test.php index 8e6cdbfb9..c7d4e354d 100644 --- a/tests/MixedType_PHP80_Test.php +++ b/tests/MixedType_PHP80_Test.php @@ -28,8 +28,8 @@ public function testStrictTypesMapping_ComplexValue() ); $this->assertInstanceOf(stdClass::class, $sn->data); - $this->assertEquals(123, $sn->data->id); - $this->assertEquals('Test User', $sn->data->name); + $this->assertSame(123, $sn->data->id); + $this->assertSame('Test User', $sn->data->name); } /** @@ -44,6 +44,6 @@ public function testStrictTypesMapping_SimpleValue() new \namespacetest\PhpMixedType() ); - $this->assertEquals(123, $sn->data); + $this->assertSame(123, $sn->data); } } diff --git a/tests/NameMappingTest.php b/tests/NameMappingTest.php index 50f1a45e7..e2656578f 100644 --- a/tests/NameMappingTest.php +++ b/tests/NameMappingTest.php @@ -14,11 +14,8 @@ public function testItSetKeysIfReturnedByUndefinedPropertyHandler(): void string $key, $value ): string { - return lcfirst( - str_replace( - ' ', '', ucwords(str_replace(array('_', '-'), ' ', $key)) - ) - ); + $this->assertSame('hyphen_value', $key); + return "hyphenValue"; }; /** @var JsonMapperTest_Simple $sn */ diff --git a/tests/NamespaceTest.php b/tests/NamespaceTest.php index d80aadbc0..f6bc5d2a2 100644 --- a/tests/NamespaceTest.php +++ b/tests/NamespaceTest.php @@ -9,8 +9,8 @@ public function testMapArrayNamespace() $mapper = new \JsonMapper(); $json = '{"data":[{"value":"1.2"}]}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); - $this->assertInstanceOf('\namespacetest\Unit', $res->data[0]); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); + $this->assertInstanceOf(\namespacetest\Unit::class, $res->data[0]); } public function testMapSimpleArrayNamespace() @@ -18,8 +18,8 @@ public function testMapSimpleArrayNamespace() $mapper = new \JsonMapper(); $json = '{"units":[{"value":"1.2"}]}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); - $this->assertInstanceOf('\namespacetest\Unit', $res->units[0]); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); + $this->assertInstanceOf(\namespacetest\Unit::class, $res->units[0]); } public function testMapSimpleStringArrayNamespace() @@ -27,7 +27,7 @@ public function testMapSimpleStringArrayNamespace() $mapper = new \JsonMapper(); $json = '{"messages":["message 1", "message 2"]}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); $this->assertNotNull($res->messages); $this->assertCount(2, $res->messages); } @@ -37,7 +37,7 @@ public function testMapMixed() $mapper = new \JsonMapper(); $json = '{"mixed":true}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); $this->assertTrue($res->mixed); } @@ -46,8 +46,8 @@ public function testMapChildClassNamespace() $mapper = new \JsonMapper(); $json = '{"user":{"name": "John Smith"}}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); - $this->assertInstanceOf('\namespacetest\model\User', $res->user); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); + $this->assertInstanceOf(\namespacetest\model\User::class, $res->user); } public function testMapChildClassConstructorNamespace() @@ -55,8 +55,8 @@ public function testMapChildClassConstructorNamespace() $mapper = new \JsonMapper(); $json = '{"user":"John Smith"}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); - $this->assertInstanceOf('\namespacetest\model\User', $res->user); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); + $this->assertInstanceOf(\namespacetest\model\User::class, $res->user); } public function testMapChildObjectArrayNamespace() @@ -65,8 +65,8 @@ public function testMapChildObjectArrayNamespace() $json = '{"data":[],"user":{"name": "John Smith"}}'; /* @var \namespacetest\UnitData $res */ $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\\ArrayObject', $res->data); - $this->assertInstanceOf('\namespacetest\model\User', $res->user); + $this->assertInstanceOf(\ArrayObject::class, $res->data); + $this->assertInstanceOf(\namespacetest\model\User::class, $res->user); } public function testMapEmpty() @@ -84,9 +84,9 @@ public function testMapCustomArrayObjectWithChildType() $mapper = new \JsonMapper(); $json = '{"users":[{"user":"John Smith"}]}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); - $this->assertInstanceOf('\namespacetest\model\UserList', $res->users); - $this->assertInstanceOf('\namespacetest\model\User', $res->users[0]); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); + $this->assertInstanceOf(\namespacetest\model\UserList::class, $res->users); + $this->assertInstanceOf(\namespacetest\model\User::class, $res->users[0]); } public function testMapCustomArrayObject() @@ -94,10 +94,9 @@ public function testMapCustomArrayObject() $mapper = new \JsonMapper(); $json = '{"aodata":["foo"]}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); - $this->assertInstanceOf('\namespacetest\model\MyArrayObject', $res->aodata); - $this->assertIsString($res->aodata[0]); - $this->assertEquals('foo', $res->aodata[0]); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); + $this->assertInstanceOf(\namespacetest\model\MyArrayObject::class, $res->aodata); + $this->assertSame('foo', $res->aodata[0]); } /** @@ -109,11 +108,11 @@ public function testSetterNamespacedTypeHint() $mapper = new \JsonMapper(); $json = '{"namespacedTypeHint":"Foo"}'; $res = $mapper->map(json_decode($json), new UnitData()); - $this->assertInstanceOf('\namespacetest\UnitData', $res); + $this->assertInstanceOf(\namespacetest\UnitData::class, $res); $this->assertInstanceOf( - '\othernamespace\Foo', $res->internalData['namespacedTypeHint'] + \othernamespace\Foo::class, $res->internalData['namespacedTypeHint'] ); - $this->assertEquals( + $this->assertSame( 'Foo', $res->internalData['namespacedTypeHint']->name ); } diff --git a/tests/ObjectTest.php b/tests/ObjectTest.php index 5fcdb5eaf..6d730b27e 100644 --- a/tests/ObjectTest.php +++ b/tests/ObjectTest.php @@ -33,8 +33,8 @@ public function testMapObject() new JsonMapperTest_Simple() ); $this->assertIsObject($sn->simple); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn->simple); - $this->assertEquals('stringvalue', $sn->simple->str); + $this->assertInstanceOf(JsonMapperTest_Simple::class, $sn->simple); + $this->assertSame('stringvalue', $sn->simple->str); } public function testMapObjectByClassName() @@ -45,8 +45,8 @@ public function testMapObjectByClassName() JsonMapperTest_Simple::class ); $this->assertIsObject($sn->simple); - $this->assertInstanceOf('JsonMapperTest_Simple', $sn->simple); - $this->assertEquals('stringvalue', $sn->simple->str); + $this->assertInstanceOf(JsonMapperTest_Simple::class, $sn->simple); + $this->assertSame('stringvalue', $sn->simple->str); } public function testMapDateTime() @@ -56,8 +56,8 @@ public function testMapDateTime() json_decode('{"datetime":"2014-04-01T00:00:00+02:00"}'), new JsonMapperTest_Object() ); - $this->assertInstanceOf('DateTime', $sn->datetime); - $this->assertEquals( + $this->assertInstanceOf(DateTime::class, $sn->datetime); + $this->assertSame( '2014-04-01T00:00:00+02:00', $sn->datetime->format('c') ); @@ -111,8 +111,8 @@ public function testStrictTypeCheckingObject() ); $this->assertIsObject($sn->pPlainObject); - $this->assertInstanceOf('JsonMapperTest_PlainObject', $sn->pPlainObject); - $this->assertEquals('abc', $sn->pPlainObject->pStr); + $this->assertInstanceOf(JsonMapperTest_PlainObject::class, $sn->pPlainObject); + $this->assertSame('abc', $sn->pPlainObject->pStr); } public function testStrictTypeCheckingObjectError() @@ -206,7 +206,8 @@ public function testConstructorWithoutParams() JsonMapperTest_ObjectConstructor::class ); - $this->assertEquals('bar', $objs[0]->foo); + $this->assertSame('bar', $objs[0]->foo); + $this->assertSame(1, $objs[0]->id); } public function testConstructorWithOptionalParams() @@ -219,7 +220,36 @@ public function testConstructorWithOptionalParams() JsonMapperTest_ObjectConstructorOptional::class ); - $this->assertEquals('optional', $objs[0]->foo); + $this->assertSame('optional', $objs[0]->foo); + $this->assertSame(1, $objs[0]->id); + } + + /** + * Test for PHP7 nullable types like "?Object" + */ + public function testObjectSetterTypeNullable() + { + $jm = new JsonMapper(); + $sn = $jm->map( + json_decode('{"typeNullableObject":null}'), + new JsonMapperTest_PHP7Object() + ); + $this->assertNull($sn->typeNullableObject); } + + /** + * Test for non-nullable types like "@param object" with null value + */ + public function testObjectSetterDocblockInvalidNull() + { + $this->expectException(JsonMapper_Exception::class); + $this->expectExceptionMessage('JSON property "nonNullableObject" in class "JsonMapperTest_PHP7Object" must not be NULL'); + $jm = new JsonMapper(); + $sn = $jm->map( + json_decode('{"nonNullableObject":null}'), + new JsonMapperTest_PHP7Object() + ); + } + } ?> diff --git a/tests/Object_PHP71_Test.php b/tests/Object_PHP71_Test.php deleted file mode 100644 index c552b01f4..000000000 --- a/tests/Object_PHP71_Test.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @license OSL-3.0 http://opensource.org/licenses/osl-3.0 - * @link https://github.com/cweiske/jsonmapper - */ - -/** - * Unit tests for JsonMapper's object handling using PHP 7.1 syntax - * - * @category Tools - * @package JsonMapper - * @author Christian Weiske - * @license OSL-3.0 http://opensource.org/licenses/osl-3.0 - * @link https://github.com/cweiske/jsonmapper - * @requires PHP 7.1 - */ -class Object_PHP71_Test extends \PHPUnit\Framework\TestCase -{ - /** - * Test for PHP7 nullable types like "?Object" - */ - public function testObjectSetterTypeNullable() - { - $jm = new JsonMapper(); - $sn = $jm->map( - json_decode('{"typeNullableObject":null}'), - new JsonMapperTest_PHP7Object() - ); - $this->assertNull($sn->typeNullableObject); - } - - /** - * Test for non-nullable types like "@param object" with null value - */ - public function testObjectSetterDocblockInvalidNull() - { - $this->expectException(JsonMapper_Exception::class); - $this->expectExceptionMessage('JSON property "nonNullableObject" in class "JsonMapperTest_PHP7Object" must not be NULL'); - $jm = new JsonMapper(); - $sn = $jm->map( - json_decode('{"nonNullableObject":null}'), - new JsonMapperTest_PHP7Object() - ); - } -} -?> diff --git a/tests/OtherTest.php b/tests/OtherTest.php index 3c4f265ce..e1cd2fd3c 100644 --- a/tests/OtherTest.php +++ b/tests/OtherTest.php @@ -66,9 +66,9 @@ public function testMapOnlySetterTypeHint() $this->assertIsObject($sn->internalData['typehint']); $this->assertInstanceOf( - 'JsonMapperTest_Simple', $sn->internalData['typehint'] + JsonMapperTest_Simple::class, $sn->internalData['typehint'] ); - $this->assertEquals( + $this->assertSame( 'stringvalue', $sn->internalData['typehint']->str ); } @@ -86,9 +86,9 @@ public function testMapOnlySetterDocblock() ); $this->assertIsObject($sn->internalData['docblock']); $this->assertInstanceOf( - 'JsonMapperTest_Simple', $sn->internalData['docblock'] + JsonMapperTest_Simple::class, $sn->internalData['docblock'] ); - $this->assertEquals( + $this->assertSame( 'stringvalue', $sn->internalData['docblock']->str ); } @@ -105,9 +105,9 @@ public function testMapOnlySetterNoType() ); $this->assertIsObject($sn->internalData['notype']); $this->assertInstanceOf( - 'stdClass', $sn->internalData['notype'] + stdClass::class, $sn->internalData['notype'] ); - $this->assertEquals( + $this->assertSame( 'stringvalue', $sn->internalData['notype']->str ); } @@ -125,14 +125,14 @@ public function testMapProtectedWithoutSetterMethod() new JsonMapperTest_Simple() ); $this->assertNull($sn->getProtectedStrNoSetter()); - $this->assertEquals( + $this->assertSame( array( array( 'info', 'Property {property} has no public setter method in {class}', array( + 'property' => 'protectedStrNoSetter', 'class' => 'JsonMapperTest_Simple', - 'property' => 'protectedStrNoSetter' ) ) ), @@ -187,7 +187,7 @@ public function testUndefinedPropertyHandler() new JsonMapperTest_Broken() ); - $this->assertEquals(123, $sn->ADDundefinedProperty); + $this->assertSame(123, $sn->ADDundefinedProperty); } public function setUnknownProperty($object, $propName, $jsonValue) @@ -205,8 +205,8 @@ public function testPrivatePropertyWithPublicSetter() $json = '{"privateProperty" : 1}'; $result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter()); - $this->assertEquals(1, $result->getPrivateProperty()); - $this->assertTrue(empty($logger->log)); + $this->assertSame(1, $result->getPrivateProperty()); + $this->assertEmpty($logger->log); } public function testPrivatePropertyWithNoSetter() @@ -220,9 +220,6 @@ public function testPrivatePropertyWithNoSetter() $json = '{"privateNoSetter" : 1}'; $result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter()); - - $this->assertEquals(1, $result->getPrivateProperty()); - $this->assertTrue(empty($logger->log)); } public function testPrivatePropertyWithNoSetterButAllowed() @@ -233,7 +230,7 @@ public function testPrivatePropertyWithNoSetterButAllowed() $json = '{"privateNoSetter" : 1}'; $result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter()); - $this->assertEquals(1, $result->getPrivateNoSetter()); + $this->assertSame(1, $result->getPrivateNoSetter()); } public function testPrivatePropertyInParentClassWithNoSetterButAllowed() @@ -244,7 +241,7 @@ public function testPrivatePropertyInParentClassWithNoSetterButAllowed() $json = '{"privateNoSetter" : 1}'; $result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetterSub()); - $this->assertEquals(1, $result->getPrivateNoSetter()); + $this->assertSame(1, $result->getPrivateNoSetter()); } public function testPrivatePropertyWithPrivateSetter() @@ -262,27 +259,17 @@ public function testPrivatePropertyWithPrivateSetter() public function testPrivatePropertySetterWithoutDoc() { - if (PHP_MAJOR_VERSION < 7) { - $this->markTestSkipped("This test is for PHP >= 7"); - } - $jm = new JsonMapper(); $jm->bExceptionOnUndefinedProperty = true; - $jm->setLogger(new JsonMapperTest_Logger()); $result = $jm->map(json_decode('{"privatePropertySetterWithoutDoc" : 1}'), new JsonMapperTest_PrivateWithSetter()); - $this->assertEquals(1, $result->getPrivatePropertySetterWithoutDoc()); + $this->assertSame(1, $result->getPrivatePropertySetterWithoutDoc()); } public function testPrivatePropertyNullableNotNullSetterWithoutDoc() { - if (PHP_MAJOR_VERSION < 7) { - $this->markTestSkipped("This test is for PHP >= 7"); - } - $jm = new JsonMapper(); $jm->bExceptionOnUndefinedProperty = true; - $jm->setLogger(new JsonMapperTest_Logger()); $result = $jm->map(json_decode('{"privatePropertyNullableSetterWithoutDoc" : 1}'), new JsonMapperTest_PrivateWithSetter()); $this->assertSame(1, $result->getPrivatePropertyNullableSetterWithoutDoc()); @@ -290,13 +277,8 @@ public function testPrivatePropertyNullableNotNullSetterWithoutDoc() public function testPrivatePropertyNullableNullSetterWithoutDoc() { - if (PHP_MAJOR_VERSION < 7) { - $this->markTestSkipped("This test is for PHP >= 7"); - } - $jm = new JsonMapper(); $jm->bExceptionOnUndefinedProperty = true; - $jm->setLogger(new JsonMapperTest_Logger()); $result = $jm->map(json_decode('{"privatePropertyNullableSetterWithoutDoc" : null}'), new JsonMapperTest_PrivateWithSetter()); $this->assertNull($result->getPrivatePropertyNullableSetterWithoutDoc()); @@ -306,7 +288,6 @@ public function testPrivateArrayOfSimple() { $jm = new JsonMapper(); $jm->bExceptionOnUndefinedProperty = true; - $jm->setLogger(new JsonMapperTest_Logger()); $result = $jm->map( json_decode( @@ -338,7 +319,7 @@ public function testPrivateSetterButAllowed() $json = '{"privateSetter" : 1}'; $result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter()); - $this->assertEquals(1, $result->getPrivateSetter()); + $this->assertSame(1, $result->getPrivateSetter()); } public function testSetterIsPreferredOverProperty() @@ -348,8 +329,7 @@ public function testSetterIsPreferredOverProperty() json_decode('{"setterPreferredOverProperty":"foo"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->setterPreferredOverProperty); - $this->assertEquals( + $this->assertSame( 'set via setter: foo', $sn->setterPreferredOverProperty ); } @@ -379,11 +359,11 @@ public function testDependencyInjection() $jm->createInstance('JsonMapperTest_Simple') ); - $this->assertEquals('first level', $sn->str); - $this->assertEquals('database', $sn->db); + $this->assertSame('first level', $sn->str); + $this->assertSame('database', $sn->db); - $this->assertEquals('second level', $sn->simple->str); - $this->assertEquals('database', $sn->simple->db); + $this->assertSame('second level', $sn->simple->str); + $this->assertSame('database', $sn->simple->db); } } ?> diff --git a/tests/SimpleTest.php b/tests/SimpleTest.php index 271b5e4f0..ce9eb2f26 100644 --- a/tests/SimpleTest.php +++ b/tests/SimpleTest.php @@ -32,8 +32,7 @@ public function testMapSimpleString() json_decode('{"str":"stringvalue"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->str); - $this->assertEquals('stringvalue', $sn->str); + $this->assertSame('stringvalue', $sn->str); } /** @@ -46,8 +45,7 @@ public function testMapSimpleFloat() json_decode('{"fl":"1.2"}'), new JsonMapperTest_Simple() ); - $this->assertIsFloat($sn->fl); - $this->assertEquals(1.2, $sn->fl); + $this->assertSame(1.2, $sn->fl); } /** @@ -60,8 +58,7 @@ public function testMapSimpleBool() json_decode('{"pbool":"1"}'), new JsonMapperTest_Simple() ); - $this->assertIsBool($sn->pbool); - $this->assertEquals(true, $sn->pbool); + $this->assertSame(true, $sn->pbool); } /** @@ -74,8 +71,7 @@ public function testMapSimpleBoolean() json_decode('{"pboolean":"0"}'), new JsonMapperTest_Simple() ); - $this->assertIsBool($sn->pboolean); - $this->assertEquals(false, $sn->pboolean); + $this->assertSame(false, $sn->pboolean); } /** @@ -88,8 +84,7 @@ public function testMapSimpleInt() json_decode('{"pint":"123"}'), new JsonMapperTest_Simple() ); - $this->assertIsInt($sn->pint); - $this->assertEquals(123, $sn->pint); + $this->assertSame(123, $sn->pint); } /** @@ -102,8 +97,7 @@ public function testMapSimpleInteger() json_decode('{"pinteger":"12345"}'), new JsonMapperTest_Simple() ); - $this->assertIsInt($sn->pinteger); - $this->assertEquals(12345, $sn->pinteger); + $this->assertSame(12345, $sn->pinteger); } /** @@ -116,15 +110,13 @@ public function testMapSimpleMixed() json_decode('{"mixed":12345}'), new JsonMapperTest_Simple() ); - $this->assertIsInt($sn->mixed); - $this->assertEquals('12345', $sn->mixed); + $this->assertSame(12345, $sn->mixed); $sn = $jm->map( json_decode('{"mixed":"12345"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->mixed); - $this->assertEquals(12345, $sn->mixed); + $this->assertSame('12345', $sn->mixed); } /** @@ -137,8 +129,7 @@ public function testMapSimpleNullableInt() json_decode('{"pnullable":0}'), new JsonMapperTest_Simple() ); - $this->assertIsInt($sn->pnullable); - $this->assertEquals(0, $sn->pnullable); + $this->assertSame(0, $sn->pnullable); } /** @@ -152,7 +143,6 @@ public function testMapSimpleNullableNull() new JsonMapperTest_Simple() ); $this->assertNull($sn->pnullable); - $this->assertEquals(null, $sn->pnullable); } /** @@ -165,8 +155,7 @@ public function testMapSimpleNullableWrong() json_decode('{"pnullable":"12345"}'), new JsonMapperTest_Simple() ); - $this->assertIsInt($sn->pnullable); - $this->assertEquals(12345, $sn->pnullable); + $this->assertSame(12345, $sn->pnullable); } /** @@ -180,7 +169,7 @@ public function testMapSimpleNoType() new JsonMapperTest_Simple() ); $this->assertIsObject($sn->notype); - $this->assertEquals((object) array('k' => 'v'), $sn->notype); + $this->assertSame(['k' => 'v'], (array) $sn->notype); } /** @@ -193,8 +182,7 @@ public function testMapSimpleUnderscore() json_decode('{"under_score":"f"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->under_score); - $this->assertEquals('f', $sn->under_score); + $this->assertSame('f', $sn->under_score); } @@ -208,8 +196,7 @@ public function testMapSimpleUnderscoreSetter() json_decode('{"under_score_setter":"blubb"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->internalData['under_score_setter']); - $this->assertEquals( + $this->assertSame( 'blubb', $sn->internalData['under_score_setter'] ); } @@ -225,8 +212,7 @@ public function testMapSimpleHyphen() new JsonMapperTest_Simple() ); - $this->assertIsString($sn->hyphenValue); - $this->assertEquals('test', $sn->hyphenValue); + $this->assertSame('test', $sn->hyphenValue); } @@ -240,8 +226,7 @@ public function testMapSimpleHyphenSetter() json_decode('{"hyphen-value-setter":"blubb"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->internalData['hyphen-value-setter']); - $this->assertEquals( + $this->assertSame( 'blubb', $sn->internalData['hyphen-value-setter'] ); @@ -257,8 +242,7 @@ public function testMapCaseMismatchNoDocblock() json_decode('{"noDocBlock":"blubb"}'), new JsonMapperTest_Simple() ); - $this->assertIsString($sn->nodocblock); - $this->assertEquals('blubb', $sn->nodocblock); + $this->assertSame('blubb', $sn->nodocblock); } } ?> diff --git a/tests/StrictTypes_PHP74_Test.php b/tests/StrictTypes_PHP74_Test.php index 6830f2462..439f70ee6 100644 --- a/tests/StrictTypes_PHP74_Test.php +++ b/tests/StrictTypes_PHP74_Test.php @@ -26,11 +26,11 @@ public function testStrictTypesMapping() new \namespacetest\PhpStrictTypes() ); - $this->assertEquals(123, $sn->id); + $this->assertSame(123, $sn->id); $this->assertInstanceOf(\namespacetest\model\User::class, $sn->importedNs); $this->assertInstanceOf(\othernamespace\Foo::class, $sn->otherNs); - $this->assertEquals('anything', $sn->withoutType); - $this->assertTrue(isset($sn->nullable)); + $this->assertSame('anything', $sn->withoutType); + $this->assertSame('value', $sn->nullable); $this->assertIsArray($sn->fooArray); $this->assertCount(2, $sn->fooArray); $this->assertInstanceOf(\othernamespace\Foo::class, $sn->fooArray[0]); diff --git a/tests/UnionTypesTest.php b/tests/UnionTypesTest.php index 9747180c8..5d0e7bdbb 100644 --- a/tests/UnionTypesTest.php +++ b/tests/UnionTypesTest.php @@ -17,10 +17,9 @@ class UnionTypesTest extends \PHPUnit\Framework\TestCase */ public function testMapUnionNative() { - $this->expectException( - JsonMapper_Exception::class, - 'Cannot decide which of the union types shall be used: \DateTime|string' - ); + $this->expectException(JsonMapper_Exception::class); + $this->expectExceptionMessage('Cannot decide which of the union types shall be used: \DateTime|string'); + $jm = new JsonMapper(); $sn = $jm->map( json_decode('{"dateOrStringNative":"stringvalue"}'), @@ -33,10 +32,9 @@ public function testMapUnionNative() */ public function testMapUnionDocblock() { - $this->expectException( - JsonMapper_Exception::class, - 'Cannot decide which of the union types shall be used: \DateTime|string' - ); + $this->expectException(JsonMapper_Exception::class); + $this->expectExceptionMessage('Cannot decide which of the union types shall be used: DateTime|string'); + $jm = new JsonMapper(); $sn = $jm->map( json_decode('{"dateOrStringDocblock":"stringvalue"}'),