Completed
Push — master ( f58bd9...4e13a1 )
by Razon
02:22 queued 43s
created

PayloadFactoryTest::testFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace RazonYang\Jsend\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use RazonYang\JSend\Payload;
6
use RazonYang\JSend\PayloadFactory;
7
use RazonYang\JSend\PayloadInterface;
8
use RazonYang\JSend\Status;
9
10
class PayloadFactoryTest extends TestCase
11
{
12
    /**
13
     * @dataProvider dataProviderSuccess
14
     */
15
    public function testSuccess($data): void
16
    {
17
        $payload = PayloadFactory::success($data);
18
        $this->assertSame(Status::SUCCESS, $payload->getStatus());
19
        $this->assertSame($data, $payload->getData());
20
    }
21
22
    public function dataProviderSuccess(): array
23
    {
24
        return [
25
            [null],
26
            ['foo'],
27
            [['id' => 1, 'name' => 'foo']],
28
        ];
29
    }
30
31
    /**
32
     * @dataProvider dataProviderFail
33
     */
34
    public function testFail($data): void
35
    {
36
        $payload = PayloadFactory::fail($data);
37
        $this->assertSame(Status::FAIL, $payload->getStatus());
38
        $this->assertSame($data, $payload->getData());
39
    }
40
41
    public function dataProviderFail(): array
42
    {
43
        return [
44
            [null],
45
            ['foo'],
46
            [['id' => 1, 'name' => 'foo']],
47
        ];
48
    }
49
50
    /**
51
     * @dataProvider dataProviderError
52
     */
53
    public function testError(string $message, ?int $code = null, $data = null): void
54
    {
55
        $payload = PayloadFactory::error($message, $code, $data);
56
        $this->assertSame(Status::ERROR, $payload->getStatus());
57
        $this->assertSame($message, $payload->getMessage());
58
        if ($code !== null) {
59
            $this->assertSame($code, $payload->getCode());
60
        }
61
        if ($data !== null) {
62
            $this->assertSame($data, $payload->getData());
63
        }
64
    }
65
66
    public function dataProviderError(): array
67
    {
68
        return [
69
            ['error'],
70
            ['error with code', 500],
71
            ['error with code and data', 401, 'data'],
72
        ];
73
    }
74
}
75