Passed
Push — master ( 17d96a...eee93d )
by Rafael
03:57
created

shouldExistInTableRecordsMatching()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Behat\Context;
12
13
use Behat\Behat\Context\Context;
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\Query\ResultSetMapping;
16
use PHPUnit\Framework\Assert;
17
use Ynlo\GraphQLBundle\Behat\Client\ClientAwareInterface;
18
use Ynlo\GraphQLBundle\Behat\Client\ClientAwareTrait;
19
use Ynlo\GraphQLBundle\Behat\Gherkin\YamlStringNode;
20
21
/**
22
 * Context for database integration
23
 */
24
final class DatabaseContext implements Context, ClientAwareInterface
25
{
26
    use ClientAwareTrait;
27
28
    /**
29
     * Use a YAML syntax to create a criteria to match a record in given table
30
     *
31
     * Example: Then should exist in table "post" a record matching:
32
     *   """
33
     *   title: "Welcome"
34
     *   body: "Welcome to web page"
35
     *   """
36
     *
37
     * Expression syntax is allowed
38
     *
39
     * Example: Then should exist in table "post" a record matching:
40
     *   """
41
     *   title: "{variables.input.title}"
42
     *   body: "{variables.input.body}"
43
     *   """
44
     *
45
     * @Given /^should exist in table "([^"]*)" a record matching:$/
46
     */
47
    public function shouldExistInTableARecordMatching($table, YamlStringNode $criteria)
48
    {
49
        $count = $this->countRecordsInTableMatching($table, $criteria->toArray());
50
        Assert::assertEquals(1, $count, sprintf('Does not exist any record in the database "%s" matching given conditions', $table));
51
    }
52
53
    /**
54
     * Use a YAML syntax to create a criteria to match many records in given table
55
     *
56
     * Example: Then should exist in table "post" a record matching:
57
     *   """
58
     *   - title: "Welcome"
59
     *     body: "Welcome to web page"
60
     *   - title: "Another Post"
61
     *     body: "This is another Post"
62
     *   """
63
     *
64
     *
65
     * @Given /^should exist in table "([^"]*)" records matching:$/
66
     */
67
    public function shouldExistInTableRecordsMatching($table, YamlStringNode $criteria)
68
    {
69
        foreach ($criteria->toArray() as $row) {
70
            $count = $this->countRecordsInTableMatching($table, $row);
71
            Assert::assertEquals(1, $count, sprintf('Does not exist any record in the database "%s" matching given conditions', $table));
72
        }
73
    }
74
75
    /**
76
     * Use a YAML syntax to create a criteria to match many records in given table
77
     *
78
     * Example: Then should not exist in table "post" a record matching:
79
     *   """
80
     *   - title: "Welcome"
81
     *     body: "Welcome to web page"
82
     *   - title: "Another Post"
83
     *     body: "This is another Post"
84
     *   """
85
     *
86
     *
87
     * @Given /^should not exist in table "([^"]*)" records matching:$/
88
     */
89
    public function shouldNotExistInTableRecordsMatching($table, YamlStringNode $criteria)
90
    {
91
        foreach ($criteria->toArray() as $row) {
92
            $count = $this->countRecordsInTableMatching($table, $row);
93
            Assert::assertEquals(0, $count, sprintf('Exist at least one record in the database "%s" matching given conditions', $table));
94
        }
95
    }
96
97
    /**
98
     * Use a YAML syntax to create a criteria to not match a record in given table
99
     *
100
     * Example: Then should not exist in table "post" a record matching:
101
     *   """
102
     *   title: "Welcome"
103
     *   body: "Welcome to web page"
104
     *   """
105
     *
106
     * Expression syntax is allowed
107
     *
108
     * Example: Then should not exist in table "post" a record matching:
109
     *   """
110
     *   title: "{variables.input.title}"
111
     *   body: "{variables.input.body}"
112
     *   """
113
     *
114
     * @Given /^should not exist in table "([^"]*)" a record matching:$/
115
     */
116
    public function shouldNotExistInTableARecordMatching($table, YamlStringNode $criteria)
117
    {
118
        $count = $this->countRecordsInTableMatching($table, $criteria->toArray());
119
        Assert::assertEquals(0, $count, sprintf('Exist at least one record in the database "%s" matching given conditions', $table));
120
    }
121
122
    /**
123
     * Count records in table matching criteria
124
     *
125
     * @param string $table
126
     * @param array  $criteria
127
     *
128
     * @return int
129
     *
130
     * @throws \Doctrine\ORM\NonUniqueResultException
131
     */
132
    public function countRecordsInTableMatching($table, $criteria = []): int
133
    {
134
        $where = '';
135
        foreach ($criteria as $field => $vale) {
136
            if ($where) {
137
                $where .= ' AND ';
138
            }
139
            if ($vale === null) {
140
                $where .= sprintf('%s IS NULL', $field);
141
            } else {
142
                $where .= sprintf('%s = :%s', $field, $field);
143
            }
144
        }
145
        $query = sprintf('SELECT count(*) AS records FROM %s WHERE %s', $table, $where);
146
147
        /** @var EntityManager $manager */
148
        $manager = $this->client->getContainer()->get('doctrine')->getManager();
149
        $rsm = new ResultSetMapping();
150
        $rsm->addScalarResult('records', 'records', 'integer');
151
        $query = $manager->createNativeQuery($query, $rsm);
152
        $query->setParameters($criteria);
153
154
        return (int) $query->getSingleScalarResult();
155
    }
156
}
157