Passed
Push — develop ( 67b972...742c02 )
by David
05:11
created

DomainRepositoryTest::providerGetByCards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 86
rs 9.328
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Repository;
6
7
use Application\Enum\Site;
8
use Application\Model\Domain;
9
use Application\Model\User;
10
use Application\Repository\DomainRepository;
11
12
class DomainRepositoryTest extends AbstractRepositoryTest
13
{
14
    private DomainRepository $repository;
15
16
    protected function setUp(): void
17
    {
18
        parent::setUp();
19
        $this->repository = _em()->getRepository(Domain::class);
20
    }
21
22
    public function testGetFullNames(): void
23
    {
24
        $actual = $this->repository->getFullNames(Site::Tiresias);
25
        $expected = [
26
            'Test domain 9000' => 9000,
27
            'Test domain 9001' => 9001,
28
        ];
29
        self::assertSame($expected, $actual);
30
31
        $actual = $this->repository->getFullNames(Site::Dilps);
32
        $expected = [
33
            'Test domain 9002' => 9002,
34
            'Test domain 9003' => 9003,
35
        ];
36
        self::assertSame($expected, $actual);
37
    }
38
39
    public function testGetSelfAndDescendantsSubQuery(): void
40
    {
41
        $expected = [
42
            ['id' => 9000],
43
        ];
44
45
        $sql = $this->repository->getSelfAndDescendantsSubQuery(9000);
46
        $actual = _em()->getConnection()->fetchAllAssociative($sql);
47
        self::assertSame($expected, $actual);
48
    }
49
50
    /**
51
     * @dataProvider providerGetByCards
52
     */
53
    public function testGetByCards(?string $userLogin, array $filter, array $expectedDomainIds, string $message): void
54
    {
55
        if ($userLogin) {
56
            $userRepository = _em()->getRepository(User::class);
57
            $user = $userRepository->getOneByLogin($userLogin, Site::Dilps);
58
            User::setCurrent($user);
59
        } else {
60
            User::setCurrent(null);
61
        }
62
63
        $result = $this->repository->getByCards($filter);
64
        $actualDomainIds = array_map(fn (Domain $domain) => $domain->getId(), $result);
65
66
        self::assertSame($expectedDomainIds, $actualDomainIds, $message);
67
    }
68
69
    public static function providerGetByCards(): iterable
70
    {
71
        // Test with administrator - should see all domains from accessible cards
72
        yield 'administrator sees all domains' => [
73
            'administrator',
74
            [
75
                'groups' => [
76
                    [
77
                        'groupLogic' => 'AND',
78
                        'conditionsLogic' => 'AND',
79
                        'conditions' => [
80
                            [
81
                                'site' => ['equal' => ['value' => Site::Dilps, 'not' => false]],
82
                            ],
83
                            [
84
                                'filename' => ['equal' => ['value' => '', 'not' => true]],
85
                            ],
86
                        ],
87
                    ],
88
                ],
89
            ],
90
            [9002, 9003], // Domains from cards 6001, 6002 (both have domains)
91
            'Administrator should see all domains from Dilps cards',
92
        ];
93
94
        // Test with junior user (user 1002) - has limited access
95
        yield 'junior sees accessible domains' => [
96
            'junior',
97
            [
98
                'groups' => [
99
                    [
100
                        'groupLogic' => 'AND',
101
                        'conditionsLogic' => 'AND',
102
                        'conditions' => [
103
                            [
104
                                'site' => ['equal' => ['value' => Site::Dilps, 'not' => false]],
105
                            ],
106
                            [
107
                                'filename' => ['equal' => ['value' => '', 'not' => true]],
108
                            ],
109
                        ],
110
                    ],
111
                ],
112
            ],
113
            [9002], // Junior (user 1002) owns card 6006 with domain 9002, but cannot see private cards 6001, 6002
114
            'Junior should see only domain from own card, not from private cards',
115
        ];
116
117
        // Test with search term filter (complexe query parameters)
118
        yield 'filter by search term' => [
119
            'administrator',
120
            [
121
                'groups' => [
122
                    [
123
                        'groupLogic' => 'AND',
124
                        'conditionsLogic' => 'AND',
125
                        'conditions' => [
126
                            [
127
                                'custom' => ['search' => ['value' => 'Test suggestion card 6001']],
128
                            ],
129
                        ],
130
                    ],
131
                ],
132
            ],
133
            [9002], // Card 6001 has domain 9002
134
            'Should return domains from card 6001',
135
        ];
136
137
        // Test empty result
138
        yield 'no cards match filter' => [
139
            'administrator',
140
            [
141
                'groups' => [
142
                    [
143
                        'groupLogic' => 'AND',
144
                        'conditionsLogic' => 'AND',
145
                        'conditions' => [
146
                            [
147
                                'id' => ['equal' => ['value' => '99999', 'not' => false]],
148
                            ],
149
                        ],
150
                    ],
151
                ],
152
            ],
153
            [],
154
            'Should return empty array when no cards match',
155
        ];
156
    }
157
}
158