testNoTablesArePopulatedIfTheyAreAlreadyProvided()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/sprout/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/sprout
12
 */
13
14
namespace Graze\Sprout\Test\Unit\Db;
15
16
use Graze\Sprout\Config\ConnectionConfigInterface;
17
use Graze\Sprout\Config\SchemaConfigInterface;
18
use Graze\Sprout\Db\DbTablePopulator;
19
use Graze\Sprout\Db\PdoFactory;
20
use Graze\Sprout\Parser\ParsedSchema;
21
use Graze\Sprout\Parser\TableFilterer;
22
use Graze\Sprout\Test\TestCase;
23
use Mockery;
24
use PDO;
25
26
/**
27
 * @runTestsInSeparateProcesses
28
 * @preserveGlobalState disabled
29
 */
30
class DbTablePopulatorTest extends TestCase
31
{
32
    /** @var DbTablePopulator */
33
    private $tablePopulator;
34
    /** @var mixed */
35
    private $tableFilterer;
36
    /** @var mixed */
37
    private $pdoFactory;
38
39
    public function setUp()
40
    {
41
        $this->pdoFactory = Mockery::mock(PdoFactory::class);
42
        $this->tableFilterer = Mockery::mock(TableFilterer::class);
43
        $this->tablePopulator = new DbTablePopulator($this->pdoFactory, $this->tableFilterer);
0 ignored issues
show
Bug introduced by
$this->tableFilterer of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Parser\TableFilterer|null expected by parameter $tableFilterer of Graze\Sprout\Db\DbTablePopulator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $this->tablePopulator = new DbTablePopulator($this->pdoFactory, /** @scrutinizer ignore-type */ $this->tableFilterer);
Loading history...
Bug introduced by
$this->pdoFactory of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Db\PdoFactory|null expected by parameter $pdoFactory of Graze\Sprout\Db\DbTablePopulator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $this->tablePopulator = new DbTablePopulator(/** @scrutinizer ignore-type */ $this->pdoFactory, $this->tableFilterer);
Loading history...
44
    }
45
46
    public function testTablePopulation()
47
    {
48
        $config = Mockery::mock(SchemaConfigInterface::class);
49
        $parsedSchema = new ParsedSchema($config, '/a/path', []);
0 ignored issues
show
Bug introduced by
$config of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Config\SchemaConfigInterface expected by parameter $schemaConfig of Graze\Sprout\Parser\ParsedSchema::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $parsedSchema = new ParsedSchema(/** @scrutinizer ignore-type */ $config, '/a/path', []);
Loading history...
50
        $connection = Mockery::mock(ConnectionConfigInterface::class);
51
        $config->allows([
52
            'getExcludes'   => [],
53
            'getSchema'     => 'schema1',
54
            'getConnection' => $connection,
55
        ]);
56
57
        $pdo = Mockery::mock(PDO::class);
58
        $this->pdoFactory->allows()
59
                         ->getPdo($connection)
60
                         ->andReturns($pdo);
61
        $statement = Mockery::mock(\PDOStatement::class);
62
        $pdo->allows()
63
            ->prepare(
0 ignored issues
show
Bug introduced by
The method prepare() does not exist on Mockery\ExpectationInterface. It seems like you code against a sub-type of Mockery\ExpectationInterface such as Mockery\CompositeExpectation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            ->/** @scrutinizer ignore-call */ prepare(
Loading history...
Bug introduced by
The method prepare() does not exist on Mockery\Expectation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            ->/** @scrutinizer ignore-call */ prepare(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
                'SELECT table_name
65
                FROM INFORMATION_SCHEMA.TABLES 
66
                WHERE table_schema = :schema
67
                AND table_type = "BASE TABLE"'
68
            )
69
            ->andReturns($statement);
70
        $statement->allows()
71
                  ->execute(['schema' => 'schema1'])
0 ignored issues
show
Bug introduced by
The method execute() does not exist on Mockery\Expectation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                  ->/** @scrutinizer ignore-call */ execute(['schema' => 'schema1'])

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method execute() does not exist on Mockery\ExpectationInterface. It seems like you code against a sub-type of Mockery\ExpectationInterface such as Mockery\CompositeExpectation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                  ->/** @scrutinizer ignore-call */ execute(['schema' => 'schema1'])
Loading history...
72
                  ->andReturns($statement);
73
        $statement->allows()
74
                  ->fetchAll(PDO::FETCH_COLUMN)
0 ignored issues
show
Bug introduced by
The method fetchAll() does not exist on Mockery\Expectation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
                  ->/** @scrutinizer ignore-call */ fetchAll(PDO::FETCH_COLUMN)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method fetchAll() does not exist on Mockery\ExpectationInterface. It seems like you code against a sub-type of Mockery\ExpectationInterface such as Mockery\CompositeExpectation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
                  ->/** @scrutinizer ignore-call */ fetchAll(PDO::FETCH_COLUMN)
Loading history...
75
                  ->andReturns(['table1', 'table2']);
76
77
        $output = $this->tablePopulator->populateTables($parsedSchema);
78
79
        $this->assertSame($parsedSchema, $output);
80
81
        $this->assertEquals(['table1', 'table2'], $output->getTables());
82
    }
83
84
    public function testNoTablesArePopulatedIfTheyAreAlreadyProvided()
85
    {
86
        $config = Mockery::mock(SchemaConfigInterface::class);
87
        $parsedSchema = new ParsedSchema($config, '/a/path', ['table1', 'table2']);
0 ignored issues
show
Bug introduced by
$config of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Config\SchemaConfigInterface expected by parameter $schemaConfig of Graze\Sprout\Parser\ParsedSchema::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        $parsedSchema = new ParsedSchema(/** @scrutinizer ignore-type */ $config, '/a/path', ['table1', 'table2']);
Loading history...
88
        $config->allows(['getExcludes' => []]);
89
90
        $output = $this->tablePopulator->populateTables($parsedSchema);
91
92
        $this->assertSame($parsedSchema, $output);
93
94
        $this->assertEquals(['table1', 'table2'], $output->getTables());
95
    }
96
97
    public function testTablesAreNotExcludedIfIncludedInExcludeList()
98
    {
99
        $config = Mockery::mock(SchemaConfigInterface::class);
100
        $parsedSchema = new ParsedSchema($config, '/a/path', ['table1', 'table2']);
0 ignored issues
show
Bug introduced by
$config of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Config\SchemaConfigInterface expected by parameter $schemaConfig of Graze\Sprout\Parser\ParsedSchema::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        $parsedSchema = new ParsedSchema(/** @scrutinizer ignore-type */ $config, '/a/path', ['table1', 'table2']);
Loading history...
101
        $config->allows(['getExcludes' => ['table1']]);
102
103
        $output = $this->tablePopulator->populateTables($parsedSchema);
104
105
        $this->assertSame($parsedSchema, $output);
106
107
        $this->assertEquals(['table1', 'table2'], $output->getTables());
108
    }
109
110
    public function testTablesAreFilteredWhenPopulatedFromADatabase()
111
    {
112
        $config = Mockery::mock(SchemaConfigInterface::class);
113
        $parsedSchema = new ParsedSchema($config, '/a/path', []);
0 ignored issues
show
Bug introduced by
$config of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Config\SchemaConfigInterface expected by parameter $schemaConfig of Graze\Sprout\Parser\ParsedSchema::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $parsedSchema = new ParsedSchema(/** @scrutinizer ignore-type */ $config, '/a/path', []);
Loading history...
114
        $connection = Mockery::mock(ConnectionConfigInterface::class);
115
        $config->allows([
116
            'getExcludes'   => ['table1'],
117
            'getSchema'     => 'schema1',
118
            'getConnection' => $connection,
119
        ]);
120
121
        $pdo = Mockery::mock(PDO::class);
122
        $this->pdoFactory->allows()
123
                         ->getPdo($connection)
124
                         ->andReturns($pdo);
125
        $statement = Mockery::mock(\PDOStatement::class);
126
        $pdo->allows()
127
            ->prepare(
128
                'SELECT table_name
129
                FROM INFORMATION_SCHEMA.TABLES 
130
                WHERE table_schema = :schema
131
                AND table_type = "BASE TABLE"'
132
            )
133
            ->andReturns($statement);
134
        $statement->allows()
135
                  ->execute(['schema' => 'schema1'])
136
                  ->andReturns($statement);
137
        $statement->allows()
138
                  ->fetchAll(PDO::FETCH_COLUMN)
139
                  ->andReturns(['table1', 'table2']);
140
141
        $this->tableFilterer->allows()
142
                            ->filter(['table1', 'table2'], ['table1'])
143
                            ->andReturns(['table2']);
144
145
        $output = $this->tablePopulator->populateTables($parsedSchema);
146
147
        $this->assertSame($parsedSchema, $output);
148
149
        $this->assertEquals(['table2'], $output->getTables());
150
    }
151
}
152