Completed
Push — master ( 3e44d5...49b4bd )
by Rafael
06:52
created

shouldNotExistInTableARecordMatching()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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 not match a record in given table
55
     *
56
     * Example: Then should not exist in table "post" a record matching:
57
     *   """
58
     *   title: "Welcome"
59
     *   body: "Welcome to web page"
60
     *   """
61
     *
62
     * Expression syntax is allowed
63
     *
64
     * Example: Then should not exist in table "post" a record matching:
65
     *   """
66
     *   title: "{variables.input.title}"
67
     *   body: "{variables.input.body}"
68
     *   """
69
     *
70
     * @Given /^should not exist in table "([^"]*)" a record matching:$/
71
     */
72
    public function shouldNotExistInTableARecordMatching($table, YamlStringNode $criteria)
73
    {
74
        $count = $this->countRecordsInTableMatching($table, $criteria->toArray());
75
        Assert::assertEquals(0, $count, sprintf('Exist at least one record in the database "%s" matching given conditions', $table));
76
    }
77
78
    /**
79
     * Count records in table matching criteria
80
     *
81
     * @param string $table
82
     * @param array  $criteria
83
     *
84
     * @return int
85
     *
86
     * @throws \Doctrine\ORM\NonUniqueResultException
87
     */
88
    public function countRecordsInTableMatching($table, $criteria = []): int
89
    {
90
        $where = '';
91
        foreach ($criteria as $field => $vale) {
92
            if ($where) {
93
                $where .= ' AND ';
94
            }
95
            $where .= sprintf('%s = :%s', $field, $field);
96
        }
97
        $query = sprintf('SELECT count(*) AS records FROM %s WHERE %s', $table, $where);
98
99
        /** @var EntityManager $manager */
100
        $manager = $this->client->getContainer()->get('doctrine')->getManager();
101
        $rsm = new ResultSetMapping();
102
        $rsm->addScalarResult('records', 'records', 'integer');
103
        $query = $manager->createNativeQuery($query, $rsm);
104
        $query->setParameters($criteria);
105
106
        return (int) $query->getSingleScalarResult();
107
    }
108
}
109