|
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 not match a record 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
|
|
|
* """ |
|
83
|
|
|
* |
|
84
|
|
|
* Expression syntax is allowed |
|
85
|
|
|
* |
|
86
|
|
|
* Example: Then should not exist in table "post" a record matching: |
|
87
|
|
|
* """ |
|
88
|
|
|
* title: "{variables.input.title}" |
|
89
|
|
|
* body: "{variables.input.body}" |
|
90
|
|
|
* """ |
|
91
|
|
|
* |
|
92
|
|
|
* @Given /^should not exist in table "([^"]*)" a record matching:$/ |
|
93
|
|
|
*/ |
|
94
|
|
|
public function shouldNotExistInTableARecordMatching($table, YamlStringNode $criteria) |
|
95
|
|
|
{ |
|
96
|
|
|
$count = $this->countRecordsInTableMatching($table, $criteria->toArray()); |
|
97
|
|
|
Assert::assertEquals(0, $count, sprintf('Exist at least one record in the database "%s" matching given conditions', $table)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Count records in table matching criteria |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $table |
|
104
|
|
|
* @param array $criteria |
|
105
|
|
|
* |
|
106
|
|
|
* @return int |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function countRecordsInTableMatching($table, $criteria = []): int |
|
111
|
|
|
{ |
|
112
|
|
|
$where = ''; |
|
113
|
|
|
foreach ($criteria as $field => $vale) { |
|
114
|
|
|
if ($where) { |
|
115
|
|
|
$where .= ' AND '; |
|
116
|
|
|
} |
|
117
|
|
|
$where .= sprintf('%s = :%s', $field, $field); |
|
118
|
|
|
} |
|
119
|
|
|
$query = sprintf('SELECT count(*) AS records FROM %s WHERE %s', $table, $where); |
|
120
|
|
|
|
|
121
|
|
|
/** @var EntityManager $manager */ |
|
122
|
|
|
$manager = $this->client->getContainer()->get('doctrine')->getManager(); |
|
123
|
|
|
$rsm = new ResultSetMapping(); |
|
124
|
|
|
$rsm->addScalarResult('records', 'records', 'integer'); |
|
125
|
|
|
$query = $manager->createNativeQuery($query, $rsm); |
|
126
|
|
|
$query->setParameters($criteria); |
|
127
|
|
|
|
|
128
|
|
|
return (int) $query->getSingleScalarResult(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|