-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathFactoryTest.php
More file actions
108 lines (81 loc) · 3.65 KB
/
FactoryTest.php
File metadata and controls
108 lines (81 loc) · 3.65 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
<?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 PHPUnit_Framework_TestCase
{
public function setUp()
{
$container = new Container;
$container['request'] = Mockery::mock('Dingo\Api\Http\Request');
$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', 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', 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', new UserTransformerStub, ['foo' => 'bar']);
$this->assertSame(['foo' => 'bar'], $binding->getParameters());
// Third parameter is callback and fourth is null.
$binding = $this->factory->register('Dingo\Api\Tests\Stubs\UserStub', 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', 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', 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', 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'));
}
}