Passed
Push — main ( 2a77a5...d280a1 )
by Thierry
04:03 queued 02:04
created

ServerContext   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 89
rs 10
c 1
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getTheDatabaseList() 0 3 1
A checkIfTheGetDatabaseSizeQueryIsExecuted() 0 7 1
A setTheNextDatabaseRequestStatus() 0 3 1
A connectToTheDefaultServer() 0 2 1
A checkIfTheReadDatabaseListQueryIsExecuted() 0 6 1
A setTheNextDatabaseRequestValueOfSize() 0 3 1
A __construct() 0 3 1
A getTheDatabaseSize() 0 3 1
A checkTheDatabaseSize() 0 3 1
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
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
     * @When I read the database list
38
     */
39
    public function getTheDatabaseList()
40
    {
41
        $this->driver->databases(true);
42
    }
43
44
    /**
45
     * @Then The read database list query is executed
46
     */
47
    public function checkIfTheReadDatabaseListQueryIsExecuted()
48
    {
49
        $queries = $this->driver->queries();
50
        Assert::assertGreaterThan(0, count($queries));
51
        $query = "SELECT datname FROM pg_database WHERE has_database_privilege(datname, 'CONNECT') ORDER BY datname";
52
        Assert::assertEquals($queries[0]['query'], $query);
53
    }
54
55
    /**
56
     * @Given The next request returns :status
57
     */
58
    public function setTheNextDatabaseRequestStatus(bool $status)
59
    {
60
        $this->driver->connection()->setNextResultStatus($status);
61
    }
62
63
    /**
64
     * @Given The next request returns database size of :size
65
     */
66
    public function setTheNextDatabaseRequestValueOfSize(int $size)
67
    {
68
        $this->driver->connection()->setNextResultValues([['size' => $size]]);
69
    }
70
71
    /**
72
     * @When I read the database :database size
73
     */
74
    public function getTheDatabaseSize(string $database)
75
    {
76
        $this->dbSize = $this->driver->databaseSize($database);
77
    }
78
79
    /**
80
     * @Then The size of the database is :size
81
     */
82
    public function checkTheDatabaseSize(int $size)
83
    {
84
        Assert::assertEquals($size, $this->dbSize);
85
    }
86
87
    /**
88
     * @Then The get database size query is executed on :database
89
     */
90
    public function checkIfTheGetDatabaseSizeQueryIsExecuted(string $database)
91
    {
92
        $queries = $this->driver->queries();
93
        $count = count($queries);
94
        Assert::assertGreaterThan(0, $count);
95
        $query = "SELECT pg_database_size($database)";
96
        Assert::assertEquals($queries[$count - 1]['query'], $query);
97
    }
98
}
99