ResponseFactoryTest::testCreateWithItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Functional;
4
5
use Happyr\JsonApiResponseFactory\ResponseFactory;
6
use Happyr\JsonApiResponseFactory\Transformer\AbstractTransformer;
7
use League\Fractal\Manager;
8
use League\Fractal\ScopeFactory;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * @author Radoje Albijanic <[email protected]>
13
 */
14
class ResponseFactoryTest extends TestCase
15
{
16
    /**
17
     * @var ResponseFactory
18
     */
19
    private $factory;
20
21
    protected function setUp(): void
22
    {
23
        $fractal = new Manager(new ScopeFactory());
24
        $this->factory = new ResponseFactory(
25
            $fractal
26
        );
27
        parent::setUp();
28
    }
29
30
    public function testCreateWithItem(): void
31
    {
32
        $item = new DummyItem('id');
33
        $response = $this->factory->createWithItem($item, new DummyItemTransformer());
34
35
        self::assertEquals(
36
            json_encode(['data' => [
37
                'id' => 'id',
38
            ]]),
39
            $response->getContent()
40
        );
41
    }
42
43
    public function testCreateWithCollection(): void
44
    {
45
        $firstItem = new DummyItem('firstId');
46
        $secondItem = new DummyItem('secondId');
47
        $response = $this->factory->createWithCollection([$firstItem, $secondItem], new DummyItemTransformer());
48
49
        self::assertEquals(
50
            json_encode(['data' => [
51
                ['id' => 'firstId'],
52
                ['id' => 'secondId'],
53
            ]]),
54
            $response->getContent()
55
        );
56
    }
57
}
58
59
class DummyItem
60
{
61
    public $id;
62
63
    public function __construct($id)
64
    {
65
        $this->id = $id;
66
    }
67
}
68
69
class DummyItemTransformer extends AbstractTransformer
70
{
71
    public function getResourceName(): string
72
    {
73
        return 'dummy-item';
74
    }
75
76
    public function transform(DummyItem $item): array
77
    {
78
        return [
79
            'id' => $item->id,
80
        ];
81
    }
82
}
83