Passed
Push — main ( 64ae32...ac06bb )
by Thierry
02:16
created

FeatureContext::__construct()   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\MySql\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
     * @Given The driver version is :version
40
     */
41
    public function setTheDriverVersion(string $version)
42
    {
43
        $this->driver->setVersion($version);
44
    }
45
46
    /**
47
     * @When I read the database list
48
     */
49
    public function getTheDatabaseList()
50
    {
51
        $this->driver->databases(true);
52
    }
53
54
    /**
55
     * @Then The select schema name query is executed
56
     */
57
    public function checkTheSelectSchemaNameQueryIsExecuted()
58
    {
59
        $queries = $this->driver->queries();
60
        Assert::assertGreaterThan(0, count($queries));
61
        Assert::assertEquals($queries[0]['query'], 'SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME');
62
    }
63
64
    /**
65
     * @Then The show databases query is executed
66
     */
67
    public function checkTheShowDatabasesQueryIsExecuted()
68
    {
69
        $queries = $this->driver->queries();
70
        Assert::assertGreaterThan(0, count($queries));
71
        Assert::assertEquals($queries[0]['query'], 'SHOW DATABASES');
72
    }
73
74
    /**
75
     * @Given The next request returns :status
76
     */
77
    public function setTheNextDatabaseRequestStatus(bool $status)
78
    {
79
        $this->driver->connection()->setNextResultStatus($status);
80
    }
81
82
    /**
83
     * @Given The next request returns database size of :size
84
     */
85
    public function setTheNextDatabaseRequestValueOfSize(int $size)
86
    {
87
        $this->driver->connection()->setNextResultValues([['size' => $size]]);
88
    }
89
90
    /**
91
     * @When I read the database :database size
92
     */
93
    public function getTheDatabaseSize(string $database)
94
    {
95
        $this->dbSize = $this->driver->databaseSize($database);
96
    }
97
98
    /**
99
     * @Then The size of the database is :size
100
     */
101
    public function checkTheDatabaseSize(int $size)
102
    {
103
        Assert::assertEquals($size, $this->dbSize);
104
    }
105
106
    /**
107
     * @Then The get database size query is executed on :database
108
     */
109
    public function checkIfTheGetDatabaseSizeQueryIsExecuted(string $database)
110
    {
111
        $queries = $this->driver->queries();
112
        $count = count($queries);
113
        Assert::assertGreaterThan(0, $count);
114
        $query = "SELECT SUM(data_length + index_length) " .
115
            "FROM information_schema.tables where table_schema=$database";
116
        Assert::assertEquals($queries[$count - 1]['query'], $query);
117
    }
118
}
119