SerializerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createSerializer() 0 3 1
A testSerialize() 0 10 2
A dataProviderModel() 0 11 1
A testSerializeModelErrors() 0 7 1
A dataProviderData() 0 6 1
1
<?php
2
namespace RazonYang\Yii2\JSend\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use RazonYang\JSend\PayloadFactory;
6
use RazonYang\JSend\PayloadInterface;
7
use RazonYang\JSend\Status;
8
use RazonYang\Yii2\JSend\Serializer;
9
use yii\base\Model;
10
11
class SerializerTest extends TestCase
12
{
13
    public function createSerializer(): Serializer
14
    {
15
        return new Serializer();
16
    }
17
18
    /**
19
     * @dataProvider dataProviderData
20
     */
21
    public function testSerialize($data): void
22
    {
23
        $serializer = $this->createSerializer();
24
        $payload = $serializer->serialize($data);
25
        $this->assertInstanceOf(PayloadInterface::class, $payload);
26
        if ($data instanceof PayloadInterface) {
27
            $this->assertSame($data, $payload);
28
        } else {
29
            $this->assertSame(Status::SUCCESS, $payload->getStatus());
30
            $this->assertSame($data, $payload->getData());
31
        }
32
    }
33
34
    public function dataProviderData(): array
35
    {
36
        return [
37
            ['foo'],
38
            [['foo', 'bar']],
39
            [PayloadFactory::fail($this)]
40
        ];
41
    }
42
43
    /**
44
     * @dataProvider dataProviderModel
45
     */
46
    public function testSerializeModelErrors(Model $model)
47
    {
48
        $serializer = $this->createSerializer();
49
        $method = new \ReflectionMethod(Serializer::class, 'serializeModelErrors');
50
        $method->setAccessible(true);
51
        $payload = $method->invoke($serializer, $model);
52
        $this->assertEquals($model->getFirstErrors(), $payload->getData());
53
    }
54
55
    public function dataProviderModel(): array
56
    {
57
        $model1 = new Model();
58
        $model1->addErrors(['name' => 'name is required']);
59
60
        $model2 = new Model();
61
        $model2->addErrors(['name' => 'name is required', 'age' => 'age is required']);
62
        
63
        return [
64
            [$model1],
65
            [$model2],
66
        ];
67
    }
68
}
69