Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Condense assertions #232

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 60 additions & 120 deletions tests/ArrayTest.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/Array_PHP74_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/Array_PHP80_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
17 changes: 8 additions & 9 deletions tests/ClassMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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
Expand All @@ -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')
);
Expand Down Expand Up @@ -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]);
}
}
?>
4 changes: 2 additions & 2 deletions tests/Enums_PHP81_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -53,7 +53,7 @@ public function testDeserializePostEventArguments()
new JsonMapperTest_EventObject()
);
$this->assertIsString($sn->pStr);
$this->assertEquals('barbarbar', $sn->pStr);
$this->assertSame('barbarbar', $sn->pStr);
}
}
?>
6 changes: 3 additions & 3 deletions tests/MixedType_PHP80_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -44,6 +44,6 @@ public function testStrictTypesMapping_SimpleValue()
new \namespacetest\PhpMixedType()
);

$this->assertEquals(123, $sn->data);
$this->assertSame(123, $sn->data);
}
}
7 changes: 2 additions & 5 deletions tests/NameMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
43 changes: 21 additions & 22 deletions tests/NamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ 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()
{
$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()
{
$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);
}
Expand All @@ -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);
}

Expand All @@ -46,17 +46,17 @@ 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()
{
$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()
Expand All @@ -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()
Expand All @@ -84,20 +84,19 @@ 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()
{
$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]);
}

/**
Expand All @@ -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
);
}
Expand Down
50 changes: 40 additions & 10 deletions tests/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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')
);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
);
}

}
?>
53 changes: 0 additions & 53 deletions tests/Object_PHP71_Test.php

This file was deleted.

Loading