Passed
Push — master ( 0b999e...3d12f2 )
by Baptiste
43s
created

EachInCollectionTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 28
dl 0
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A the_json_each_elements_in_collection_should_contain() 0 10 4
A the_json_each_elements_in_collection_should_be_bool() 0 10 4
A the_json_path_elements_in_collection_should_have_a_property() 0 6 3
A the_json_each_elements_in_collection_should_be_equal_to() 0 10 4
A the_json_each_elements_in_collection_should_be_equal_to_int() 0 10 4
1
<?php declare(strict_types=1);
2
namespace Behapi\Json;
3
4
use Webmozart\Assert\Assert;
5
6
trait EachInCollectionTrait
7
{
8
    abstract protected function getValue(?string $path);
9
10
    /**
11
     * @Then /^in the json, each elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/
12
     * @Then /^in the json, each elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/
13
     **/
14
    final public function the_json_path_elements_in_collection_should_have_a_property(string $path, ?string $not = null, string $property): void
15
    {
16
        $assert = [Assert::class, $not !== null ? 'allPropertyNotExists' : 'allPropertyExists'];
17
        assert(is_callable($assert));
18
19
        $assert($this->getValue(empty($path) ? null : $path), $property);
20
    }
21
22
    /**
23
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to "(?P<expected>(?:[^"]|\\")*)"$/
24
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to "(?P<expected>(?:[^"]|\\")*)"$/
25
     */
26
    final public function the_json_each_elements_in_collection_should_be_equal_to(string $path, string $property, ?string $not = null, string $expected): void
27
    {
28
        $values = $this->getValue(empty($path) ? null : $path);
29
        Assert::isIterable($values);
30
31
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
32
        assert(is_callable($assert));
33
34
        foreach ($values as $element) {
35
            $assert($this->accessor->getValue($element, $property), $expected);
36
        }
37
    }
38
39
    /**
40
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be (?P<expected>true|false)$/
41
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be (?P<expected>true|false)$/
42
     */
43
    final public function the_json_each_elements_in_collection_should_be_bool(string $path, string $property, ?string $not = null, string $expected): void
44
    {
45
        $values = $this->getValue(empty($path) ? null : $path);
46
        Assert::isIterable($values);
47
48
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
49
        assert(is_callable($assert));
50
51
        foreach ($values as $element) {
52
            $assert($this->accessor->getValue($element, $property), $expected === 'true');
53
        }
54
    }
55
56
    /**
57
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to (?P<expected>[0-9]+)$/
58
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to (?P<expected>[0-9]+)$/
59
     */
60
    final public function the_json_each_elements_in_collection_should_be_equal_to_int(string $path, string $property, ?string $not = null, int $expected): void
61
    {
62
        $values = $this->getValue(empty($path) ? null : $path);
63
        Assert::isIterable($values);
64
65
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
66
        assert(is_callable($assert));
67
68
        foreach ($values as $element) {
69
            $assert($this->accessor->getValue($element, $property), $expected);
70
        }
71
    }
72
73
    /**
74
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should contain "(?P<expected>(?:[^"]|\\")*)"$/
75
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) contain "(?P<expected>(?:[^"]|\\")*)"$/
76
     **/
77
    final public function the_json_each_elements_in_collection_should_contain(string $path, string $property, ?string $not = null, string $expected): void
78
    {
79
        $values = $this->getValue(empty($path) ? null : $path);
80
        Assert::isIterable($values);
81
82
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
83
        assert(is_callable($assert));
84
85
        foreach ($values as $element) {
86
            $assert($this->accessor->getValue($element, $property), $expected);
87
        }
88
    }
89
}
90