FindResponseTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 94
rs 10
c 1
b 0
f 0
wmc 7

6 Methods

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