Passed
Push — master ( c40e05...eb14ea )
by Tim
02:17
created

ColumnSanitizerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Dbal\Utils\ColumnSanitizerTest
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 7
13
 *
14
 * @author    Team CSI <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Dbal\Utils;
22
23
use PHPUnit\Framework\TestCase;
24
25
/**
26
 * Test class for the column sanitizer utility.
27
 *
28
 * @author    Team CSI <[email protected]>
29
 * @copyright 2020 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class ColumnSanitizerTest extends TestCase
35
{
36
37
    /**
38
     * The utility we want to test.
39
     *
40
     * @var \TechDivision\Import\Utils\SanitizerInterface
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Utils\SanitizerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    protected $columnSanitizer;
43
44
    /**
45
     * Sets up the fixture, for example, open a network connection.
46
     * This method is called before a test is executed.
47
     *
48
     * @return void
49
     * @see \PHPUnit\Framework\TestCase::setUp()
50
     */
51
    protected function setUp()
52
    {
53
        // initialize the utility we want to test
54
        $this->columnSanitizer = new ColumnSanitizer();
0 ignored issues
show
Documentation Bug introduced by
It seems like new TechDivision\Import\...Utils\ColumnSanitizer() of type TechDivision\Import\Dbal\Utils\ColumnSanitizer is incompatible with the declared type TechDivision\Import\Utils\SanitizerInterface of property $columnSanitizer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
    }
56
57
    public function testExecuteGivenEmptyRowReturnsEmptyRow()
58
    {
59
        $query = 'UPDATE some_table SET column_1=:column_1, column_2=:column_2 WHERE condition=:value';
60
        $row = [];
61
        $expected = [];
62
63
        $actual = $this->columnSanitizer->execute($row, $query);
64
65
        $this->assertEquals($expected, $actual);
66
    }
67
68
    public function testExecuteGivenEmptyStatementReturnsEmptyRow()
69
    {
70
        $query = '';
71
        $row = ['column_1' => 'foo', 'value' => 'bar'];
72
        $expected = [];
73
74
        $actual = $this->columnSanitizer->execute($row, $query);
75
76
        $this->assertEquals($expected, $actual);
77
    }
78
79
    public function testExecuteGivenLessColumnsThanAllowedReturnsOnlyAllowedGivenColumns()
80
    {
81
        $query = 'UPDATE some_table SET column_1=:column_1, column_2=:column_2 WHERE condition=:value';
82
        $row = ['column_1' => 'foo', 'value' => 'bar'];
83
        $expected = ['column_1' => 'foo', 'value' => 'bar'];
84
85
        $actual = $this->columnSanitizer->execute($row, $query);
86
87
        $this->assertEquals($expected, $actual);
88
    }
89
90
    public function testExecuteGivenAllAllowedColumnsReturnsAllColumns()
91
    {
92
        $query = 'UPDATE some_table SET column_1=:column_1, column_2=:column_2 WHERE condition=:value';
93
        $row = ['column_1' => 'foo', 'column_2' => 'baz', 'value' => 'bar'];
94
        $expected = ['column_1' => 'foo', 'column_2' => 'baz', 'value' => 'bar'];
95
96
        $actual = $this->columnSanitizer->execute($row, $query);
97
98
        $this->assertEquals($expected, $actual);
99
    }
100
101
    public function testExecuteGivenMoreThanAllowedColumnsReturnsOnlAllowedColumns()
102
    {
103
        $query = 'UPDATE some_table SET column_1=:column_1, column_2=:column_2 WHERE condition=:value';
104
        $row = ['column_1' => 'foo', 'column_2' => 'baz', 'value' => 'bar', 'additional' => 'another value'];
105
        $expected = ['column_1' => 'foo', 'column_2' => 'baz', 'value' => 'bar'];
106
107
        $actual = $this->columnSanitizer->execute($row, $query);
108
109
        $this->assertEquals($expected, $actual);
110
    }
111
112
    public function testExecuteGivenSpecialColumnReturnsDataIncludingSpecialColumn()
113
    {
114
        $query = 'UPDATE some_table SET column_1=:column_1, column_2=:column_2 WHERE condition=:value';
115
        $row = [
116
            'column_1' => 'foo',
117
            'column_2' => 'baz',
118
            'value' => 'bar',
119
            'techdivision_import_utils_entityStatus_memberName' => 'update',
120
        ];
121
        $expected = [
122
            'column_1' => 'foo',
123
            'column_2' => 'baz',
124
            'value' => 'bar',
125
            'techdivision_import_utils_entityStatus_memberName' => 'update',
126
        ];
127
128
        $actual = $this->columnSanitizer->execute($row, $query);
129
130
        $this->assertEquals($expected, $actual);
131
    }
132
}
133