Repository navigation
Expand file tree
/
Copy pathFactoryTest.php
More file actions
113 lines (85 loc) · 3.76 KB
/
Copy pathFactoryTest.php
File metadata and controls
113 lines (85 loc) · 3.76 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Dingo\Api\Tests\Transformer;
use Mockery;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Transformer\Factory;
use Illuminate\Support\Collection;
use Dingo\Api\Tests\Stubs\UserStub;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\TransformerStub;
use Dingo\Api\Tests\Stubs\UserTransformerStub;
class FactoryTest extends TestCase
{
protected $factory;
public function setUp()
{
$container = new Container;
$container['request'] = Mockery::mock(\Dingo\Api\Http\Request::class);
$this->factory = new Factory($container, new TransformerStub);
}
public function tearDown()
{
Mockery::close();
}
public function testResponseIsTransformable()
{
$this->assertFalse($this->factory->transformableResponse(new UserStub('Jason'), new UserTransformerStub));
$this->factory->register(\Dingo\Api\Tests\Stubs\UserStub::class, new UserTransformerStub);
$this->assertTrue($this->factory->transformableResponse(new UserStub('Jason'), new UserTransformerStub));
}
public function testRegisterParameterOrder()
{
// Third parameter is parameters and fourth is callback.
$binding = $this->factory->register(\Dingo\Api\Tests\Stubs\UserStub::class, new UserTransformerStub,
['foo' => 'bar'], function ($foo) {
$this->assertSame('foo', $foo);
});
$binding->fireCallback('foo');
$this->assertSame(['foo' => 'bar'], $binding->getParameters());
// Third parameter is parameters and fourth is null.
$binding = $this->factory->register(\Dingo\Api\Tests\Stubs\UserStub::class, new UserTransformerStub,
['foo' => 'bar']);
$this->assertSame(['foo' => 'bar'], $binding->getParameters());
// Third parameter is an empty array and fourth is callback.
$binding = $this->factory->register(\Dingo\Api\Tests\Stubs\UserStub::class, new UserTransformerStub, [],
function ($foo) {
$this->assertSame('foo', $foo);
});
$binding->fireCallback('foo');
}
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::class, new UserTransformerStub);
$response = $this->factory->transform(new UserStub('Jason'));
$this->assertSame(['name' => 'Jason'], $response);
}
public function testTransformingCollectionResponse()
{
$this->factory->register(\Dingo\Api\Tests\Stubs\UserStub::class, new UserTransformerStub);
$response = $this->factory->transform(new Collection([new UserStub('Jason'), new UserStub('Bob')]));
$this->assertSame([['name' => 'Jason'], ['name' => 'Bob']], $response);
}
public function testTransforingWithIlluminateRequest()
{
$container = new Container;
$container['request'] = new \Illuminate\Http\Request();
$factory = new Factory($container, new TransformerStub);
$factory->register(\Dingo\Api\Tests\Stubs\UserStub::class, new UserTransformerStub);
$response = $factory->transform(new UserStub('Jason'));
$this->assertSame(['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('Jason'));
}
}