-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformerFactoryTest.php
86 lines (61 loc) · 2.5 KB
/
TransformerFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Dingo\Api\Tests\Transformer;
use Mockery;
use PHPUnit_Framework_TestCase;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\UserStub;
use Dingo\Api\Tests\Stubs\TransformerStub;
use Dingo\Api\Tests\Stubs\UserContractStub;
use Dingo\Api\Transformer\TransformerFactory;
use Dingo\Api\Tests\Stubs\UserTransformerStub;
class TransformerFactoryTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$container = new Container;
$container['request'] = Mockery::mock('Illuminate\Http\Request');
$this->factory = new TransformerFactory($container, new TransformerStub);
}
public function tearDown()
{
Mockery::close();
}
public function testResponseIsTransformable()
{
$this->assertFalse($this->factory->transformableResponse(new UserStub, new UserTransformerStub));
$this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub);
$this->assertTrue($this->factory->transformableResponse(new UserStub, new UserTransformerStub));
}
public function testResponseIsTransformableType()
{
$this->assertFalse($this->factory->transformableType(['foo' => 'bar']));
$this->assertTrue($this->factory->transformableType('Foo'));
$this->assertTrue($this->factory->transformableType((object) ['foo' => 'bar']));
}
public function testTransformingResponse()
{
$this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub);
$response = $this->factory->transform(new UserStub);
$this->assertEquals(['name' => 'Jason'], $response);
}
public function testTransformingCollectionResponse()
{
$this->factory->register('Dingo\Api\Tests\Stubs\UserStub', new UserTransformerStub);
$response = $this->factory->transform(new Collection([new UserStub, new UserStub]));
$this->assertEquals([['name' => 'Jason'], ['name' => 'Jason']], $response);
}
public function testTransformingResponseBoundByContract()
{
$response = $this->factory->transform(new UserContractStub);
$this->assertEquals(['name' => 'Jason'], $response);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to find bound transformer for "Dingo\Api\Tests\Stubs\UserStub" class
*/
public function testTransformingWithNoTransformerThrowsException()
{
$this->factory->transform(new UserStub);
}
}