RepositoryReadFeatureContext::iHaveFollowingRows()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 1
dl 0
loc 23
rs 9.3888
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Behat\Features\Dms\MySQL\Repository;
4
5
use Behat\Gherkin\Node\TableNode;
6
7
class RepositoryReadFeatureContext extends AbstractRepositoryFeatureContext
8
{
9
    /**
10
     * @Then /^I call method "(.*)" on repository which will return following rows:$/
11
     *
12
     * @param string $method
13
     * @param TableNode $rows
14
     */
15
    public function iCallMethodOnRepositoryWhichWillReturnFollowingRows(string $method, TableNode $rows)
16
    {
17
        static::$entities = static::$repository->$method();
18
19
        $this->iHaveFollowingRows($rows);
20
    }
21
22
    /**
23
     * @Then /^I have following rows:$/
24
     *
25
     * @param TableNode $rows
26
     *
27
     * @throws \Exception
28
     */
29
    public function iHaveFollowingRows(TableNode $rows)
30
    {
31
        if (($entitiesCount = \count(static::$entities)) !== ($expectedRowsCount = \count($rows->getRows()) - 1)) {
32
            throw new \Exception(\sprintf(
33
                'Count of expected rows(%d) does not match to count of returned rows(%d)!',
34
                $expectedRowsCount,
35
                $entitiesCount
36
            ));
37
        }
38
39
        foreach ($this->normalizeTableNode($rows) as $i => $row) {
40
            $entity = static::$entities[$i];
41
42
            foreach ($row as $column => $value) {
43
                $getterMethod = \sprintf('get%s', \ucfirst($column));
44
                if ($value !== $entity->$getterMethod()) {
45
                    throw new \Exception(\sprintf(
46
                        'Data mismatch, when reading stored row data! %s::%s => %s != %s => %s',
47
                        \get_class($entity),
48
                        $getterMethod,
49
                        $entity->$getterMethod(),
50
                        $column,
51
                        $value
52
                    ));
53
                }
54
            }
55
        }
56
    }
57
58
    /**
59
     * @Then /^I have following data columns on entities:$/
60
     *
61
     * @param TableNode $columns
62
     *
63
     * @throws \Exception
64
     */
65
    public function iHaveFollowingRowsOfDataColumnsOnEntities(TableNode $columns)
66
    {
67
        if (($entitiesCount = \count(static::$entities)) !== ($expectedRowsCount = \count($columns->getRows()) - 1)) {
68
            throw new \Exception(\sprintf(
69
                'Count of expected rows(%d) does not match to count of existing entities(%d)!',
70
                $expectedRowsCount,
71
                $entitiesCount
72
            ));
73
        }
74
75
        foreach ($this->normalizeTableNode($columns) as $i => $row) {
76
            $entity = static::$entities[$i];
77
78
            foreach ($row as $column => $value) {
79
                if ($value !== $entity->data($column)) {
80
                    throw new \Exception(\sprintf(
81
                        'Data mismatch, when reading stored row data! %s::data(%s) => %s != %s => %s',
82
                        \get_class($entity),
83
                        $column,
84
                        $entity->data($column),
0 ignored issues
show
Bug introduced by
It seems like $entity->data($column) can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                        /** @scrutinizer ignore-type */ $entity->data($column),
Loading history...
85
                        $column,
86
                        $value
87
                    ));
88
                }
89
            }
90
        }
91
    }
92
93
    /**
94
     * @Then /^I call method "(.*)" on repository which will return following integer (\d+)$/
95
     *
96
     * @param string $method
97
     * @param int $integer
98
     *
99
     * @throws \Exception
100
     */
101
    public function iCallMethodOnRepositoryWhichWillReturnFollowingInteger(string $method, int $integer)
102
    {
103
        $returnedInteger = static::$repository->$method();
104
105
        if ($returnedInteger !== (int) $integer) {
106
            throw new \Exception(\sprintf(
107
                'Expected integer(%d) does not match returned integer(%d)!',
108
                $integer,
109
                $returnedInteger
110
            ));
111
        }
112
    }
113
114
    /**
115
     * @codingStandardsIgnoreStart
116
     * @Then /^I call method "(.*)" on repository which will return paginator with page size of (\d+) and current page (\d+)$/
117
     * @codingStandardsIgnoreEnd
118
     *
119
     * @param string $method
120
     * @param int $pageSize
121
     * @param int $currentPage
122
     */
123
    public function iCallPaginatorMethodOnRepositoryWithPageSizeOfAndCurrentPage(
124
        string $method,
125
        int $pageSize,
126
        int $currentPage
127
    ) {
128
        static::$paginator = static::$repository->$method($pageSize, $currentPage);
129
    }
130
131
    /**
132
     * @Then /^I call method "(.*)" on paginator which will return entities$/
133
     *
134
     * @param string $method
135
     */
136
    public function iCallMethodOnPaginatorWhichWillReturnEntities(string $method)
137
    {
138
        static::$entities = static::$paginator->$method();
139
    }
140
141
    /**
142
     * @Then /^I call method "(.*)" on paginator which will return following integer (\d+)$/
143
     *
144
     * @param string $method
145
     * @param int $integer
146
     *
147
     * @throws \Exception
148
     */
149
    public function iCallMethodOnPaginatorWhichWillReturnFollowingInteger(string $method, int $integer)
150
    {
151
        $returnedInteger = static::$paginator->$method();
152
153
        if ($returnedInteger !== (int) $integer) {
154
            throw new \Exception(\sprintf(
155
                'Expected integer(%d) does not match returned integer(%d)!',
156
                $integer,
157
                $returnedInteger
158
            ));
159
        }
160
    }
161
162
    /**
163
     * @Then /^I get following page numbers from paginator:$/
164
     *
165
     * @param TableNode $expectedPageNumbers
166
     */
167
    public function iGetFollowingPageNumbersFromPaginator(TableNode $expectedPageNumbers)
168
    {
169
        $pageNumbers = self::$paginator->getPageNumbers();
170
171
        if (($pageNumbersCount = \count($pageNumbers))
172
            !== ($expectedPageNumbersCount = \count($expectedPageNumbers->getRows()) - 1)
173
        ) {
174
            throw new \Exception(\sprintf(
175
                'Count of expected page numbers(%d) does not match to count of existing page numbers(%d)!',
176
                $expectedPageNumbersCount,
177
                $pageNumbersCount
178
            ));
179
        }
180
181
        foreach ($this->normalizeTableNode($expectedPageNumbers) as $expectedPageNumberRow) {
182
            foreach ($expectedPageNumberRow as $value) {
183
                if (!isset($pageNumbers[$value]) || $value !== ($pageNumber = $pageNumbers[$value])) {
184
                    throw new \Exception(\sprintf(
185
                        'Data mismatch, when reading page numbers! %s != %s',
186
                        $value,
187
                        isset($pageNumber) ? $pageNumber : null
188
                    ));
189
                }
190
            }
191
        }
192
    }
193
}
194