Passed
Push — main ( 1a63c6...e5fba6 )
by Thierry
06:00 queued 03:56
created

ServerContext::setTheNextDatabaseRequestStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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