Skip to content

Commit

Permalink
Condense unit test assertions
Browse files Browse the repository at this point in the history
Resolves: #232
  • Loading branch information
cweiske committed May 16, 2024
2 parents 0b5b559 + b94c4a1 commit 10aceb9
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 300 deletions.
161 changes: 55 additions & 106 deletions tests/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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')
);
}
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -165,12 +148,11 @@ public function testMapArrayObject()
json_decode('{"pArrayObject":[{"str":"stringvalue"},{"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(2, $sn->pArrayObject);
$this->assertContainsOnlyInstancesOf(stdClass::class, $sn->pArrayObject);
$this->assertSame('stringvalue', $sn->pArrayObject[0]->str);
$this->assertSame('1.2', $sn->pArrayObject[1]->fl);
}

/**
Expand All @@ -185,12 +167,11 @@ public function testMapTypedArrayObject()
),
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);
}

/**
Expand All @@ -205,12 +186,11 @@ public function testMapSimpleArrayObject()
),
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()
Expand All @@ -222,11 +202,9 @@ public function testMapSimpleArrayAccess()
),
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()
Expand Down Expand Up @@ -313,8 +291,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]
);
Expand Down Expand Up @@ -354,7 +332,7 @@ public function testArrayObjectList()
$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());
}

/**
Expand All @@ -374,7 +352,7 @@ public function testArrayObjectSubclassList()
$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());
}

/**
Expand All @@ -387,18 +365,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);
}

/**
Expand All @@ -413,17 +380,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);
}

/**
Expand All @@ -436,7 +404,7 @@ public function testMapArray()
json_decode('[1,2,3]'),
[]
);
$this->assertEquals([1, 2, 3], $mapped);
$this->assertSame([1, 2, 3], $mapped);
}

/**
Expand All @@ -450,7 +418,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);
}

/**
Expand All @@ -464,10 +432,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')
);
}
Expand Down Expand Up @@ -511,13 +479,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()
Expand All @@ -530,20 +498,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()
Expand All @@ -556,16 +511,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'));
}
}

Expand Down
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]);
}
}
?>
Loading

0 comments on commit 10aceb9

Please sign in to comment.