Passed
Push — main ( bc7697...6bcb5a )
by Thierry
09:28 queued 02:16
created

FeatureContext::getTheDatabaseList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
use Lagdo\DbAdmin\Driver\PgSql\Tests\Driver;
4
5
use Behat\Behat\Context\Context;
6
use PHPUnit\Framework\Assert;
7
8
use function count;
9
10
class FeatureContext implements Context
11
{
12
    /**
13
     * @var Driver
14
     */
15
    protected $driver;
16
17
    /**
18
     * @var int
19
     */
20
    protected $dbSize;
21
22
    /**
23
     * The constructor
24
     */
25
    public function __construct()
26
    {
27
        $this->driver = new Driver();
28
    }
29
30
    /**
31
     * @Given The default server is connected
32
     */
33
    public function connectToTheDefaultServer()
34
    {
35
        // Nothing to do
36
    }
37
38
    /**
39
     * @When I read the database list
40
     */
41
    public function getTheDatabaseList()
42
    {
43
        $this->driver->databases(true);
44
    }
45
46
    /**
47
     * @Then The read database list query is executed
48
     */
49
    public function checkIfTheReadDatabaseListQueryIsExecuted()
50
    {
51
        $queries = $this->driver->queries();
52
        Assert::assertGreaterThan(0, count($queries));
53
        $query = "SELECT datname FROM pg_database WHERE has_database_privilege(datname, 'CONNECT') ORDER BY datname";
54
        Assert::assertEquals($queries[0]['query'], $query);
55
    }
56
57
    /**
58
     * @Given The next request returns :status
59
     */
60
    public function setTheNextDatabaseRequestStatus(bool $status)
61
    {
62
        $this->driver->connection()->setNextResultStatus($status);
63
    }
64
65
    /**
66
     * @Given The next request returns database size of :size
67
     */
68
    public function setTheNextDatabaseRequestValueOfSize(int $size)
69
    {
70
        $this->driver->connection()->setNextResultValues([['size' => $size]]);
71
    }
72
73
    /**
74
     * @When I read the database :database size
75
     */
76
    public function getTheDatabaseSize(string $database)
77
    {
78
        $this->dbSize = $this->driver->databaseSize($database);
79
    }
80
81
    /**
82
     * @Then The size of the database is :size
83
     */
84
    public function checkTheDatabaseSize(int $size)
85
    {
86
        Assert::assertEquals($size, $this->dbSize);
87
    }
88
89
    /**
90
     * @Then The get database size query is executed on :database
91
     */
92
    public function checkIfTheGetDatabaseSizeQueryIsExecuted(string $database)
93
    {
94
        $queries = $this->driver->queries();
95
        $count = count($queries);
96
        Assert::assertGreaterThan(0, $count);
97
        $query = "SELECT pg_database_size($database)";
98
        Assert::assertEquals($queries[$count - 1]['query'], $query);
99
    }
100
}
101