Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit e1e3d5a

Browse files
author
Andrey Helldar
committed
Updated tests
1 parent ef9790d commit e1e3d5a

File tree

4 files changed

+98
-58
lines changed

4 files changed

+98
-58
lines changed

tests/BaseTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Tests\Entities\Response;
7+
8+
abstract class BaseTestCase extends TestCase
9+
{
10+
protected $use_data = true;
11+
12+
protected function response($data = null, int $status_code = 200, array $with = []): Response
13+
{
14+
return new Response($data, $status_code, $with, [], $this->use_data);
15+
}
16+
}

tests/Entities/Response.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tests\Entities;
4+
5+
use function api_response;
6+
7+
final class Response
8+
{
9+
protected $content;
10+
11+
protected $status_code;
12+
13+
public function __construct($data = null, int $status_code = 200, array $with = [], array $headers = [], bool $use_data = true)
14+
{
15+
$response = api_response($data, $status_code, $with, $headers, $use_data);
16+
17+
$this->content = $response->getContent();
18+
$this->status_code = $response->getStatusCode();
19+
}
20+
21+
public function getContent()
22+
{
23+
return $this->content;
24+
}
25+
26+
public function getStatusCode(): int
27+
{
28+
return $this->status_code;
29+
}
30+
}

tests/ResponseNotUseDataTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace Tests;
44

55
use Exception;
6-
use PHPUnit\Framework\TestCase;
7-
use Symfony\Component\HttpFoundation\JsonResponse;
86
use Tests\Exceptions\BarException;
97
use Tests\Exceptions\FooException;
108

11-
class ResponseNotUseDataTest extends TestCase
9+
class ResponseNotUseDataTest extends BaseTestCase
1210
{
11+
protected $use_data = false;
12+
1313
public function testEmpty()
1414
{
1515
$this->assertJson($this->response(null)->getContent());
@@ -164,9 +164,4 @@ public function testExceptionHandlerWithStatusCode()
164164

165165
$this->assertSame(406, $r->getStatusCode());
166166
}
167-
168-
protected function response($data = null, int $status_code = 200, array $with = [], bool $use_data = false): JsonResponse
169-
{
170-
return api_response($data, $status_code, $with, [], $use_data);
171-
}
172167
}

tests/ResponseUseDataTest.php

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,56 @@
33
namespace Tests;
44

55
use Exception;
6-
use PHPUnit\Framework\TestCase;
76
use Tests\Exceptions\BarException;
87
use Tests\Exceptions\FooException;
98

10-
class ResponseUseDataTest extends TestCase
9+
class ResponseUseDataTest extends BaseTestCase
1110
{
1211
public function testEmpty()
1312
{
14-
$this->assertJson(api_response(null)->getContent());
15-
$this->assertJson(api_response(null, 300)->getContent());
16-
$this->assertJson(api_response(null, 400)->getContent());
17-
$this->assertJson(api_response(null, 500)->getContent());
18-
19-
$this->assertJson(api_response('')->getContent());
20-
$this->assertJson(api_response('', 300)->getContent());
21-
$this->assertJson(api_response('', 400)->getContent());
22-
$this->assertJson(api_response('', 500)->getContent());
23-
24-
$this->assertEquals(json_encode(['data' => null]), api_response(null)->getContent());
25-
$this->assertEquals(json_encode(['data' => null]), api_response(null, 300)->getContent());
26-
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), api_response(null, 400)->getContent());
27-
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), api_response(null, 500)->getContent());
28-
29-
$this->assertEquals(json_encode(['data' => null]), api_response('')->getContent());
30-
$this->assertEquals(json_encode(['data' => null]), api_response('', 300)->getContent());
31-
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), api_response('', 400)->getContent());
32-
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), api_response('', 500)->getContent());
13+
$this->assertJson($this->response(null)->getContent());
14+
$this->assertJson($this->response(null, 300)->getContent());
15+
$this->assertJson($this->response(null, 400)->getContent());
16+
$this->assertJson($this->response(null, 500)->getContent());
17+
18+
$this->assertJson($this->response('')->getContent());
19+
$this->assertJson($this->response('', 300)->getContent());
20+
$this->assertJson($this->response('', 400)->getContent());
21+
$this->assertJson($this->response('', 500)->getContent());
22+
23+
$this->assertEquals(json_encode(['data' => null]), $this->response(null)->getContent());
24+
$this->assertEquals(json_encode(['data' => null]), $this->response(null, 300)->getContent());
25+
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), $this->response(null, 400)->getContent());
26+
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), $this->response(null, 500)->getContent());
27+
28+
$this->assertEquals(json_encode(['data' => null]), $this->response('')->getContent());
29+
$this->assertEquals(json_encode(['data' => null]), $this->response('', 300)->getContent());
30+
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), $this->response('', 400)->getContent());
31+
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => null]]), $this->response('', 500)->getContent());
3332
}
3433

3534
public function testData()
3635
{
37-
$this->assertJson(api_response('ok')->getContent());
38-
$this->assertJson(api_response('fail', 400)->getContent());
36+
$this->assertJson($this->response('ok')->getContent());
37+
$this->assertJson($this->response('fail', 400)->getContent());
3938

40-
$this->assertEquals(json_encode(['data' => 'ok']), api_response('ok')->getContent());
39+
$this->assertEquals(json_encode(['data' => 'ok']), $this->response('ok')->getContent());
4140
}
4241

4342
public function testStructure()
4443
{
45-
$this->assertJsonStringEqualsJsonString(json_encode(['data' => 'ok']), api_response('ok')->getContent());
46-
$this->assertJsonStringEqualsJsonString(json_encode(['error' => ['type' => Exception::class, 'data' => 'fail']]), api_response('fail', 400)->getContent());
44+
$this->assertJsonStringEqualsJsonString(json_encode(['data' => 'ok']), $this->response('ok')->getContent());
45+
$this->assertJsonStringEqualsJsonString(json_encode(['error' => ['type' => Exception::class, 'data' => 'fail']]), $this->response('fail', 400)->getContent());
4746

48-
$this->assertJsonStringNotEqualsJsonString(json_encode(['data' => 'ok']), api_response('fail', 400)->getContent());
47+
$this->assertJsonStringNotEqualsJsonString(json_encode(['data' => 'ok']), $this->response('fail', 400)->getContent());
4948
}
5049

5150
public function testDataWith()
5251
{
53-
$this->assertJson(api_response('ok', 200, ['foo' => 'bar'])->getContent());
54-
$this->assertJson(api_response('fail', 400, ['foo' => 'bar'])->getContent());
52+
$this->assertJson($this->response('ok', 200, ['foo' => 'bar'])->getContent());
53+
$this->assertJson($this->response('fail', 400, ['foo' => 'bar'])->getContent());
5554

56-
$this->assertEquals(json_encode(['data' => 'ok', 'foo' => 'bar']), api_response('ok', 200, ['foo' => 'bar'])->getContent());
55+
$this->assertEquals(json_encode(['data' => 'ok', 'foo' => 'bar']), $this->response('ok', 200, ['foo' => 'bar'])->getContent());
5756

5857
$this->assertEquals(
5958
json_encode([
@@ -63,64 +62,64 @@ public function testDataWith()
6362
],
6463
'foo' => 'bar',
6564
]),
66-
api_response('ok', 400, ['foo' => 'bar'])->getContent()
65+
$this->response('ok', 400, ['foo' => 'bar'])->getContent()
6766
);
6867

6968
$this->assertEquals(
7069
json_encode(['data' => [], 'foo' => 'bar', 'baz' => 'baq']),
71-
api_response([], 200, ['foo' => 'bar', 'baz' => 'baq'])->getContent()
70+
$this->response([], 200, ['foo' => 'bar', 'baz' => 'baq'])->getContent()
7271
);
7372

7473
$this->assertEquals(
7574
json_encode(['data' => ['foo' => 'foo', 'bar' => 'bar'], 'foo' => 'bar', 'baz' => 'baq']),
76-
api_response([], 200, ['data' => ['foo' => 'foo', 'bar' => 'bar'], 'foo' => 'bar', 'baz' => 'baq'])->getContent()
75+
$this->response([], 200, ['data' => ['foo' => 'foo', 'bar' => 'bar'], 'foo' => 'bar', 'baz' => 'baq'])->getContent()
7776
);
7877

7978
$this->assertEquals(
8079
json_encode(['error' => ['type' => Exception::class, 'data' => []], 'foo' => 'bar', 'baz' => 'baq']),
81-
api_response([], 400, ['foo' => 'bar', 'baz' => 'baq'])->getContent()
80+
$this->response([], 400, ['foo' => 'bar', 'baz' => 'baq'])->getContent()
8281
);
8382

8483
$this->assertEquals(
8584
json_encode(['error' => ['type' => Exception::class, 'data' => []], 'data' => ['foo' => 'foo', 'bar' => 'bar'], 'foo' => 'bar', 'baz' => 'baq']),
86-
api_response([], 400, ['data' => ['foo' => 'foo', 'bar' => 'bar'], 'foo' => 'bar', 'baz' => 'baq'])->getContent()
85+
$this->response([], 400, ['data' => ['foo' => 'foo', 'bar' => 'bar'], 'foo' => 'bar', 'baz' => 'baq'])->getContent()
8786
);
8887
}
8988

9089
public function testNumber()
9190
{
92-
$this->assertEquals(json_encode(['data' => 304]), api_response(304)->getContent());
93-
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => 304]]), api_response(304, 400)->getContent());
91+
$this->assertEquals(json_encode(['data' => 304]), $this->response(304)->getContent());
92+
$this->assertEquals(json_encode(['error' => ['type' => Exception::class, 'data' => 304]]), $this->response(304, 400)->getContent());
9493
}
9594

9695
public function testStatusCode()
9796
{
98-
$this->assertEquals(200, api_response('ok')->getStatusCode());
99-
$this->assertEquals(301, api_response('ok', 301)->getStatusCode());
100-
$this->assertEquals(401, api_response('ok', 401)->getStatusCode());
101-
$this->assertEquals(500, api_response('ok', 500)->getStatusCode());
97+
$this->assertEquals(200, $this->response('ok')->getStatusCode());
98+
$this->assertEquals(301, $this->response('ok', 301)->getStatusCode());
99+
$this->assertEquals(401, $this->response('ok', 401)->getStatusCode());
100+
$this->assertEquals(500, $this->response('ok', 500)->getStatusCode());
102101
}
103102

104103
public function testKeyCollisionTesting()
105104
{
106-
$this->assertEquals(json_encode(['data' => 'example', 'foo' => 'bar']), api_response(['data' => 'example', 'foo' => 'bar'])->getContent());
107-
$this->assertEquals(json_encode(['data' => ['example', 'foo' => 'bar']]), api_response(['data' => ['example', 'foo' => 'bar']])->getContent());
105+
$this->assertEquals(json_encode(['data' => 'example', 'foo' => 'bar']), $this->response(['data' => 'example', 'foo' => 'bar'])->getContent());
106+
$this->assertEquals(json_encode(['data' => ['example', 'foo' => 'bar']]), $this->response(['data' => ['example', 'foo' => 'bar']])->getContent());
108107

109108
$this->assertEquals(
110109
json_encode(['error' => ['type' => Exception::class, 'data' => 'example'], 'foo' => 'bar']),
111-
api_response(['data' => 'example', 'foo' => 'bar'], 400)->getContent()
110+
$this->response(['data' => 'example', 'foo' => 'bar'], 400)->getContent()
112111
);
113112

114113
$this->assertEquals(
115114
json_encode(['error' => ['type' => Exception::class, 'data' => ['foo' => 'bar', 'baz' => 'baq']]]),
116-
api_response(['data' => ['foo' => 'bar', 'baz' => 'baq']], 400)->getContent()
115+
$this->response(['data' => ['foo' => 'bar', 'baz' => 'baq']], 400)->getContent()
117116
);
118117
}
119118

120119
public function testWithExceptionHandler()
121120
{
122121
$e = new FooException('Foo');
123-
$r = api_response($e);
122+
$r = $this->response($e);
124123

125124
$this->assertEquals(
126125
json_encode(['error' => ['type' => 'FooException', 'data' => 'Foo']]),
@@ -133,7 +132,7 @@ public function testWithExceptionHandler()
133132
public function testExceptionHandlerWithAdditionalData()
134133
{
135134
$e = new FooException('Foo');
136-
$r = api_response($e, 200, ['foo' => 'Bar']);
135+
$r = $this->response($e, 200, ['foo' => 'Bar']);
137136

138137
$this->assertEquals(
139138
json_encode(['error' => ['type' => 'FooException', 'data' => 'Foo'], 'foo' => 'Bar']),
@@ -146,7 +145,7 @@ public function testExceptionHandlerWithAdditionalData()
146145
public function testExceptionHandlerWithReplaceStatusCode()
147146
{
148147
$e = new FooException('Foo');
149-
$r = api_response($e, 408);
148+
$r = $this->response($e, 408);
150149

151150
$this->assertEquals(
152151
json_encode(['error' => ['type' => 'FooException', 'data' => 'Foo']]),
@@ -159,7 +158,7 @@ public function testExceptionHandlerWithReplaceStatusCode()
159158
public function testExceptionHandlerWithDefaultStatusCode()
160159
{
161160
$e = new BarException('Bar');
162-
$r = api_response($e);
161+
$r = $this->response($e);
163162

164163
$this->assertEquals(
165164
json_encode(['error' => ['type' => 'BarException', 'data' => 'Bar']]),
@@ -172,7 +171,7 @@ public function testExceptionHandlerWithDefaultStatusCode()
172171
public function testExceptionHandlerWithStatusCode()
173172
{
174173
$e = new BarException('Bar');
175-
$r = api_response($e, 406);
174+
$r = $this->response($e, 406);
176175

177176
$this->assertEquals(
178177
json_encode(['error' => ['type' => 'BarException', 'data' => 'Bar']]),

0 commit comments

Comments
 (0)