|
1
|
|
|
<?php |
|
2
|
|
|
namespace RazonYang\Jsend\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use RazonYang\JSend\Payload; |
|
6
|
|
|
use RazonYang\JSend\PayloadInterface; |
|
7
|
|
|
use RazonYang\JSend\Status; |
|
8
|
|
|
|
|
9
|
|
|
class PayloadTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @dataProvider dataProviderSetUp |
|
13
|
|
|
*/ |
|
14
|
|
|
public function testSetUp(string $status, $data = null, ?string $message = null, ?int $code = null): void |
|
15
|
|
|
{ |
|
16
|
|
|
$payload = new Payload(); |
|
17
|
|
|
|
|
18
|
|
|
$this->assertInstanceOf(PayloadInterface::class, $payload->setStatus($status)); |
|
19
|
|
|
if ($data !== null) { |
|
20
|
|
|
$this->assertInstanceOf(PayloadInterface::class, $payload->setData($data)); |
|
21
|
|
|
} |
|
22
|
|
|
if ($message !== null) { |
|
23
|
|
|
$this->assertInstanceOf(PayloadInterface::class, $payload->setMessage($message)); |
|
24
|
|
|
} |
|
25
|
|
|
if ($code !== null) { |
|
26
|
|
|
$this->assertInstanceOf(PayloadInterface::class, $payload->setCode($code)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$arr = $payload->toArray(); |
|
31
|
|
|
$json = $payload->toString(); |
|
32
|
|
|
$this->assertSame($arr, json_decode($json, true)); |
|
33
|
|
|
$this->assertSame($json, strval($payload)); |
|
34
|
|
|
// status |
|
35
|
|
|
$this->assertSame($status, $payload->getStatus()); |
|
36
|
|
|
$this->assertSame($status, $arr['status']); |
|
37
|
|
|
|
|
38
|
|
|
if ($data !== null) { |
|
39
|
|
|
$this->assertSame($data, $payload->getData()); |
|
40
|
|
|
$this->assertSame($data, $arr['data']); |
|
41
|
|
|
} |
|
42
|
|
|
if ($message !== null) { |
|
43
|
|
|
$this->assertSame($message, $payload->getMessage()); |
|
44
|
|
|
$this->assertSame($message, $arr['message']); |
|
45
|
|
|
} |
|
46
|
|
|
if ($code !== null) { |
|
47
|
|
|
$this->assertSame($code, $payload->getCode()); |
|
48
|
|
|
$this->assertSame($code, $arr['code']); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function dataProviderSetUp(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
[Status::SUCCESS, ['id' => 1, 'name' => 'foo']], |
|
56
|
|
|
[Status::FAIL, ['password' => 'password is incorrect']], |
|
57
|
|
|
[Status::ERROR, null, '500 Internal Error'], |
|
58
|
|
|
[Status::ERROR, ['traces' => 'stack traces'], 'Unauthorized', 401], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|