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
|
|
|
* Count records in table matching criteria |
55
|
|
|
* |
56
|
|
|
* @param string $table |
57
|
|
|
* @param array $criteria |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
* |
61
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
62
|
|
|
*/ |
63
|
|
|
public function countRecordsInTableMatching($table, $criteria = []): int |
64
|
|
|
{ |
65
|
|
|
$where = ''; |
66
|
|
|
foreach ($criteria as $field => $vale) { |
67
|
|
|
if ($where) { |
68
|
|
|
$where .= ' AND '; |
69
|
|
|
} |
70
|
|
|
$where .= sprintf('%s = :%s', $field, $field); |
71
|
|
|
} |
72
|
|
|
$query = sprintf('SELECT count(*) AS records FROM %s WHERE %s', $table, $where); |
73
|
|
|
|
74
|
|
|
/** @var EntityManager $manager */ |
75
|
|
|
$manager = $this->client->getContainer()->get('doctrine')->getManager(); |
76
|
|
|
$rsm = new ResultSetMapping(); |
77
|
|
|
$rsm->addScalarResult('records', 'records', 'integer'); |
78
|
|
|
$query = $manager->createNativeQuery($query, $rsm); |
79
|
|
|
$query->setParameters($criteria); |
80
|
|
|
|
81
|
|
|
return (int) $query->getSingleScalarResult(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|