Passed
Pull Request — master (#10)
by Anatoly
02:48
created

EntryListControllerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 145
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testList() 0 22 1
A invalidLimitProvider() 0 8 1
A invalidOffsetProvider() 0 7 1
A testListWithInvalidOffset() 0 15 1
A testListWithEmptyLimitAndOffset() 0 22 1
A testListWithInvalidLimit() 0 15 1
1
<?php declare(strict_types=1);
2
3
namespace App\Bundle\Example\Tests\Service;
4
5
/**
6
 * Import classes
7
 */
8
use App\Tests\ContainerAwareTrait;
9
use App\Tests\DatabaseSchemaToolTrait;
10
use PHPUnit\Framework\TestCase;
11
use Sunrise\Http\Router\Exception\BadRequestException;
12
use Sunrise\Http\Router\OpenApi\Test\OpenApiAssertKitTrait;
13
use Sunrise\Http\ServerRequest\ServerRequestFactory;
14
15
/**
16
 * EntryListControllerTest
17
 */
18
class EntryListControllerTest extends TestCase
19
{
20
    use ContainerAwareTrait;
21
    use DatabaseSchemaToolTrait;
22
    use OpenApiAssertKitTrait;
23
24
    /**
25
     * @var string
26
     */
27
    private const ROUTE_NAME = 'api_v1_entry_list';
28
29
    /**
30
     * @return void
31
     *
32
     * @runInSeparateProcess
33
     */
34
    public function testList() : void
35
    {
36
        $container = $this->getContainer();
37
        $doctrine = $container->get('doctrine');
38
39
        $entityManager = $doctrine->getManager('master');
40
        $this->createDatabaseSchema($entityManager);
41
42
        $entryManager = $container->get('entryManager');
43
        $this->assertSame(0, $entryManager->countAll());
44
45
        $entryManager->create(['name' => 'foo', 'slug' => 'foo']);
46
        $entryManager->create(['name' => 'bar', 'slug' => 'bar']);
47
48
        $request = (new ServerRequestFactory)
49
            ->createServerRequest('GET', '/api/v1/entry');
50
51
        $route = $container->get('router')->getRoute(self::ROUTE_NAME);
52
        $response = $route->handle($request);
53
54
        $this->assertSame(200, $response->getStatusCode());
55
        $this->assertResponseBodyMatchesDescription($route, $response);
56
    }
57
58
    /**
59
     * @return void
60
     *
61
     * @runInSeparateProcess
62
     */
63
    public function testListWithEmptyLimitAndOffset() : void
64
    {
65
        $container = $this->getContainer();
66
        $doctrine = $container->get('doctrine');
67
68
        $entityManager = $doctrine->getManager('master');
69
        $this->createDatabaseSchema($entityManager);
70
71
        $entryManager = $container->get('entryManager');
72
        $this->assertSame(0, $entryManager->countAll());
73
74
        $request = (new ServerRequestFactory)
75
            ->createServerRequest('GET', '/api/v1/entry')
76
            ->withQueryParams([
77
                'limit' => '',
78
                'offset' => '',
79
            ]);
80
81
        $route = $container->get('router')->getRoute(self::ROUTE_NAME);
82
        $response = $route->handle($request);
83
84
        $this->assertSame(200, $response->getStatusCode());
85
    }
86
87
    /**
88
     * @param mixed $limit
89
     *
90
     * @return void
91
     *
92
     * @dataProvider invalidLimitProvider
93
     *
94
     * @runInSeparateProcess
95
     */
96
    public function testListWithInvalidLimit($limit) : void
97
    {
98
        $container = $this->getContainer();
99
100
        $request = (new ServerRequestFactory)
101
            ->createServerRequest('GET', '/api/v1/entry')
102
            ->withQueryParams([
103
                'limit' => $limit,
104
            ]);
105
106
        $this->expectException(BadRequestException::class);
107
108
        $container->get('router')
109
            ->getRoute(self::ROUTE_NAME)
110
            ->handle($request);
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function invalidLimitProvider() : array
117
    {
118
        return [
119
            [null],
120
            [[]],
121
            ['-1'],
122
            ['0'],
123
            ['foo'],
124
        ];
125
    }
126
127
    /**
128
     * @param mixed $offset
129
     *
130
     * @return void
131
     *
132
     * @dataProvider invalidOffsetProvider
133
     *
134
     * @runInSeparateProcess
135
     */
136
    public function testListWithInvalidOffset($offset) : void
137
    {
138
        $container = $this->getContainer();
139
140
        $request = (new ServerRequestFactory)
141
            ->createServerRequest('GET', '/api/v1/entry')
142
            ->withQueryParams([
143
                'offset' => $offset,
144
            ]);
145
146
        $this->expectException(BadRequestException::class);
147
148
        $container->get('router')
149
            ->getRoute(self::ROUTE_NAME)
150
            ->handle($request);
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function invalidOffsetProvider() : array
157
    {
158
        return [
159
            [null],
160
            [[]],
161
            ['-1'],
162
            ['foo'],
163
        ];
164
    }
165
}
166