Skip to content

Commit a3ae4e6

Browse files
author
Nil Portugués
committed
Better test cases
1 parent b209041 commit a3ae4e6

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

src/Mapping/MappingFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ class MappingFactory
4040
public static function fromClass($className)
4141
{
4242
/* @var ApiMapping|HalJsonMapping|JsonApiMapping $instance */
43-
$className = (string) $className;
43+
$className = '\\'.ltrim($className, '\\');
44+
if (!class_exists($className, true)) {
45+
throw new MappingException(
46+
sprintf('Provided class %s could not be loaded.', $className)
47+
);
48+
}
4449
$instance = new $className();
4550

4651
if (!in_array(ApiMapping::class, class_implements($instance, true))) {
4752
throw new MappingException(
48-
sprintf('Class %s must implement %s.', get_class($instance), ApiMapping::class)
53+
sprintf('Class %s must implement %s.', ltrim($className, '\\'), ApiMapping::class)
4954
);
5055
}
5156

tests/Dummy/PostApiMapping.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace NilPortugues\Tests\Api\Dummy;
44

5-
use NilPortugues\Api\Mappings\ApiMapping;
65
use NilPortugues\Api\Mappings\HalJsonMapping;
76
use NilPortugues\Api\Mappings\JsonApiMapping;
87
use NilPortugues\Tests\Api\Dummy\ComplexObject\Post;
98

10-
class PostApiMapping implements ApiMapping, JsonApiMapping, HalJsonMapping
9+
class PostApiMapping implements JsonApiMapping, HalJsonMapping
1110
{
1211
/**
1312
* {@inheritdoc}

tests/Mapping/MapperTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
use NilPortugues\Api\Mapping\Mapper;
1414
use NilPortugues\Api\Mapping\MappingException;
1515
use NilPortugues\Tests\Api\Dummy\ComplexObject\Post;
16+
use NilPortugues\Tests\Api\Dummy\PostApiMapping;
1617

1718
class MapperTest extends \PHPUnit_Framework_TestCase
1819
{
19-
public function testItCanConstruct()
20+
public function testItCanConstructWithClassArray()
21+
{
22+
$mapping = [
23+
PostApiMapping::class,
24+
];
25+
26+
$mapper = new Mapper($mapping);
27+
$this->assertNotEmpty($mapper->getClassMap());
28+
}
29+
30+
public function testItCanConstructWithArray()
2031
{
2132
$mapping = [
2233
[

tests/Mapping/MappingFactoryTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public function testItCanBuildMappingsFromClass()
3131
$this->assertEquals('http://example.com/posts/{postId}/relationships/author', $mapping->getRelationshipSelfUrl('author'));
3232
}
3333

34+
public function testItCanBuildMappingsFromClassWillThrowExceptionIfAClassIsNotProvided()
35+
{
36+
$this->setExpectedException(MappingException::class);
37+
MappingFactory::fromClass('NotAClass');
38+
}
39+
40+
public function testItCanBuildMappingsFromClassWillThrowExceptionIfClassDoesImplementApiMappingInterface()
41+
{
42+
$this->setExpectedException(MappingException::class);
43+
MappingFactory::fromClass('\DateTime');
44+
}
45+
3446
public function testItCanBuildMappingsFromArray()
3547
{
3648
$mappedClass = [
@@ -169,7 +181,7 @@ public function testItWillThrowExceptionIfArrayHasNoClassKey()
169181
public function testItWillThrowExceptionIfArrayHasNoSelfUrlKey()
170182
{
171183
$this->setExpectedException(MappingException::class);
172-
$mappedClass = ['class' => 'Post', 'id_properties' => ['postId'], 'urls' => []];
184+
$mappedClass = ['class' => Post::class, 'id_properties' => ['postId'], 'urls' => []];
173185
MappingFactory::fromArray($mappedClass);
174186
}
175187
}

tests/Mapping/MappingTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public function testAliasedProperties()
5959
$this->assertEquals(['oldName' => 'newName', 'oldName2' => 'newName2'], $this->mapping->getAliasedProperties());
6060
}
6161

62+
/**
63+
*
64+
*/
65+
public function testAliasedPropertiesRenamedIdProperties()
66+
{
67+
$this->mapping->setPropertyNameAliases(['postId' => 'newId']);
68+
$this->assertEquals(
69+
['postId' => 'newId'],
70+
$this->mapping->getAliasedProperties()
71+
);
72+
$this->assertEquals(['newId'], $this->mapping->getIdProperties());
73+
}
6274
/**
6375
*
6476
*/

0 commit comments

Comments
 (0)