GraphQL::seeRecords()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Codeception\Helper;
5
6
use Codeception\Exception\ModuleException;
7
use Codeception\Module;
8
use Codeception\Module\Laravel5;
9
use Illuminate\Support\Arr;
10
use function strpos;
11
12
13
/**
14
 * Class GraphQL
15
 *
16
 * @package SmartWeb\ModuleTesting\Codeception\Helper
17
 */
18
class GraphQL extends Module
19
{
20
    
21
    /**
22
     * @param string $table
23
     * @param array  $records
24
     */
25
    public function seeRecords(string $table, array $records)
26
    {
27
        $laravel5 = $this->getLaravel5Module();
28
        
29
        foreach ($records as $record) {
30
            $laravel5->seeRecord($table, $record);
31
        }
32
    }
33
    
34
    /**
35
     * @return Laravel5
36
     */
37
    protected function getLaravel5Module() : Laravel5
38
    {
39
        return $this->getModule('Laravel5');
40
    }
41
    
42
    /**
43
     * @param string[] ...$keys
44
     */
45
    public function assertResponseHasKeys(string... $keys)
46
    {
47
        $this->assertArrayHasDotKeys("Response should have key.", $this->getResponseContentAsArray(), ...$keys);
48
    }
49
    
50
    /**
51
     * @param string   $message
52
     * @param array    $array
53
     * @param string[] ...$keys
54
     */
55
    public function assertArrayHasDotKeys(string $message, array $array, string... $keys)
56
    {
57
        foreach ($keys as $key) {
58
            if (strpos($key, '.') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing \strpos($key, '.') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
59
                $this->assertArrayHasKey($key, $array, $message);
60
                continue;
61
            }
62
            
63
            $subKeyArray = $array;
64
            
65
            foreach (explode('.', $key) as $segment) {
66
                if (Arr::accessible($subKeyArray)) {
67
                    $subKeyArray = $subKeyArray[$segment];
68
                } else {
69
                    $this->assertArrayHasKey($segment, $subKeyArray, $message);
70
                    continue;
71
                }
72
            }
73
        }
74
    }
75
    
76
    /**
77
     * @param $text
78
     */
79
    public function seeResponseContains($text)
80
    {
81
        $this->assertContains($text, $this->getResponseContent(), "response contains");
82
    }
83
    
84
    /**
85
     * @return array
86
     */
87
    public function getResponseContentAsArray() : array
88
    {
89
        $jsonResponse = $this->getResponseContent();
90
        
91
        return json_decode($jsonResponse, true) ?? [];
92
    }
93
    
94
    /**
95
     * @return mixed
96
     */
97
    public function getResponseContent()
98
    {
99
        try {
100
            return $this->getLaravel5Module()->_getResponseContent();
101
        } catch (ModuleException $e) {
102
            $this->fail($e->getMessage());
103
            
104
            return null;
105
        }
106
    }
107
    
108
    /**
109
     * @param string $key
110
     *
111
     * @return mixed
112
     */
113
    public function getResponseKey(string $key)
114
    {
115
        return Arr::get($this->getResponseContentAsArray(), $key);
116
    }
117
    
118
}
119