Passed
Push — master ( 727475...c2e18b )
by Igor
04:07
created

FindResponseTest::testAddLawyer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SomeWork\Minjust\Tests\Unit;
4
5
use Generator;
6
use PHPUnit\Framework\TestCase;
7
use ReflectionObject;
8
use SomeWork\Minjust\Entity\Lawyer;
9
use SomeWork\Minjust\FindResponse;
10
11
/**
12
 * @covers \SomeWork\Minjust\FindResponse
13
 * @coversDefaultClass \SomeWork\Minjust\FindResponse
14
 */
15
class FindResponseTest extends TestCase
16
{
17
    public function testEmpty(): FindResponse
18
    {
19
        $response = new FindResponse();
20
21
        $this->assertIsArray($this->getPropertyValue($response, 'lawyers'));
22
        $this->assertNull($this->getPropertyValue($response, 'detailLawyers'));
23
        $this->assertIsInt($this->getPropertyValue($response, 'page'));
24
        $this->assertIsInt($this->getPropertyValue($response, 'totalPage'));
25
        $this->assertIsInt($this->getPropertyValue($response, 'total'));
26
27
        return $response;
28
    }
29
30
    protected function getPropertyValue(object $object, string $property)
31
    {
32
        $ref = new ReflectionObject($object);
33
        /** @noinspection PhpUnhandledExceptionInspection */
34
        $property = $ref->getProperty($property);
35
        $property->setAccessible(true);
36
37
        return $property->getValue($object);
38
    }
39
40
    /**
41
     * @depends testEmpty
42
     *
43
     * @param \SomeWork\Minjust\FindResponse $response
44
     *
45
     * @return \SomeWork\Minjust\FindResponse
46
     */
47
    public function testSet(FindResponse $response): FindResponse
48
    {
49
        $lawyers = [
50
            (new Lawyer())->setFullName('test1'),
51
            (new Lawyer())->setFullName('test2'),
52
        ];
53
        $generator = $this->getDetailGenerator($lawyers);
54
55
        $response
56
            ->setLawyers($lawyers)
57
            ->setDetailLawyersGenerator($generator)
58
            ->setPage(2)
59
            ->setTotalPage(3)
60
            ->setTotal(4);
61
62
        $this->assertEquals($lawyers, $this->getPropertyValue($response, 'lawyers'));
63
        $this->assertEquals($generator, $this->getPropertyValue($response, 'detailLawyers'));
64
        $this->assertEquals(2, $this->getPropertyValue($response, 'page'));
65
        $this->assertEquals(3, $this->getPropertyValue($response, 'totalPage'));
66
        $this->assertEquals(4, $this->getPropertyValue($response, 'total'));
67
68
        return $response;
69
    }
70
71
    protected function getDetailGenerator(array $lawyers): ?Generator
72
    {
73
        foreach ($lawyers as $lawyer) {
74
            yield $lawyer;
75
        }
76
    }
77
78
    /**
79
     * @depends testSet
80
     *
81
     * @param \SomeWork\Minjust\FindResponse $response
82
     */
83
    public function testGet(FindResponse $response): void
84
    {
85
        $lawyers = [
86
            (new Lawyer())->setFullName('test1'),
87
            (new Lawyer())->setFullName('test2'),
88
        ];
89
        $generator = $this->getDetailGenerator($lawyers);
90
91
        $this->assertEquals($lawyers, $response->getLawyers());
92
        $this->assertEquals($generator, $response->getDetailLawyers());
93
        $this->assertEquals(2, $response->getPage());
94
        $this->assertEquals(3, $response->getTotalPage());
95
        $this->assertEquals(4, $response->getTotal());
96
    }
97
98
    /**
99
     * @covers ::addLawyer
100
     */
101
    public function testAddLawyer(): void
102
    {
103
        $lawyer = (new Lawyer())->setFullName('test1');
104
105
        $response = new FindResponse();
106
        $response->addLawyer($lawyer);
107
108
        $this->assertEquals([$lawyer], $this->getPropertyValue($response, 'lawyers'));
109
    }
110
}
111