RepositoryCreateFeatureContext   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 86
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A iCallMethodOnRepositoryWithParametersAndExpectingException() 0 8 2
A callMethodOnRepositoryWithParameters() 0 15 3
A iHaveFollowingEntitiesWithIdentifiersSet() 0 25 5
A iCallMethodOnRepositoryWithParameters() 0 3 1
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 RepositoryCreateFeatureContext extends AbstractRepositoryFeatureContext
8
{
9
    /**
10
     * @When /^I call method "(.*)" on repository with parameters:$/
11
     *
12
     * @param string $method
13
     * @param TableNode $parameters
14
     */
15
    public function iCallMethodOnRepositoryWithParameters(string $method, TableNode $parameters)
16
    {
17
        $this->callMethodOnRepositoryWithParameters($method, $parameters);
18
    }
19
    /**
20
     * @When /^I call method "(.*)" on repository with parameters and expecting exception:$/
21
     *
22
     * @param string $method
23
     * @param TableNode $parameters
24
     */
25
    public function iCallMethodOnRepositoryWithParametersAndExpectingException(string $method, TableNode $parameters)
26
    {
27
        try {
28
            $this->callMethodOnRepositoryWithParameters($method, $parameters);
29
        } catch (\Exception $e) {
30
            static::$entities = [];
31
32
            static::$exception = $e;
33
        }
34
    }
35
36
    /**
37
     * @Then I have following entities with identifiers set:
38
     *
39
     * @param TableNode $identifiers
40
     *
41
     * @throws \Exception
42
     */
43
    public function iHaveFollowingEntitiesWithIdentifiersSet(TableNode $identifiers)
44
    {
45
        if (($entitiesCount = \count(static::$entities))
46
            !== ($identifiersCount = \count($identifiers->getRows()) - 1)
47
        ) {
48
            throw new \Exception(\sprintf(
49
                'Count of entities(%d) does not match to count of identifiers(%d)!',
50
                $entitiesCount,
51
                $identifiersCount
52
            ));
53
        }
54
55
        foreach ($this->normalizeTableNode($identifiers) as $i => $identifier) {
56
            $entity = static::$entities[$i];
57
58
            foreach ($identifier as $identifierName => $identifierValue) {
59
                $getterMethod = \sprintf('get%s', \ucfirst($identifierName));
60
                if ($identifierValue != $entity->$getterMethod()) {
61
                    throw new \Exception(\sprintf(
62
                        'Identifiers does not match! %s::%s => %s != %s => %s',
63
                        \get_class($entity),
64
                        $getterMethod,
65
                        $entity->$getterMethod(),
66
                        $identifierName,
67
                        $identifierValue
68
                    ));
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param string $method
76
     * @param TableNode $parameters
77
     */
78
    private function callMethodOnRepositoryWithParameters(string $method, TableNode $parameters)
79
    {
80
        static::$entities = [];
81
82
        foreach ($this->normalizeTableNode($parameters) as $methodParameters) {
83
            $methodResult = \call_user_func_array([static::$repository, $method], $methodParameters);
84
85
            if (\is_array($methodResult)) {
86
                static::$entities = \array_merge(static::$entities, $methodResult);
87
            } else {
88
                static::$entities[] = $methodResult;
89
            }
90
        }
91
92
        \array_filter(static::$entities);
93
    }
94
}
95