UnionTraitTest::testBuildUnionAllQueryPart()
last analyzed

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
nc 2
nop 0
dl 0
loc 25
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Enum\CommandEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
7
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderInterface;
8
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\BindTrait;
9
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\UnionTrait;
10
11
class UnionTraitTest extends AbstractTraitTestCase
12
{
13
    const COMMAND_INVALID = 'INVALID';
14
15
    const QUERY_BUILDER_COMMAND = CommandEnum::SELECT;
16
    const QUERY_BUILDER_QUERY = <<<MySQL
17
SELECT col1, col2, col3 FROM table1 WHERE table1.col1 = :col1 AND table1.col2 IS NOT NULL
18
MySQL;
19
    const QUERY_BUILDER_BIND_DATA = [
20
        'col1' => 'val1',
21
    ];
22
23
    /**
24
     * @var QueryBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $queryBuilder;
27
28
    /**
29
     * @var BindTrait|UnionTrait
30
     */
31
    private $unionTraitClass;
32
33
    public function setUp()
34
    {
35
        $this->queryBuilder = $this->createMock(QueryBuilderInterface::class);
36
        $this->queryBuilder->method('bindData')->willReturn(static::QUERY_BUILDER_BIND_DATA);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

36
        $this->queryBuilder->/** @scrutinizer ignore-call */ 
37
                             method('bindData')->willReturn(static::QUERY_BUILDER_BIND_DATA);

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...
37
        $this->queryBuilder->method('buildQuery')->willReturn(static::QUERY_BUILDER_QUERY);
38
39
        $this->unionTraitClass = new class ()
40
        {
41
            use BindTrait;
42
            use UnionTrait;
43
44
            /**
45
             * @return array
46
             */
47
            public function unionAllData(): array
48
            {
49
                return $this->unionAll;
50
            }
51
52
            public function clearUnionAllData()
53
            {
54
                $this->unionAll = [];
55
            }
56
57
            /**
58
             * @return null|string
59
             */
60
            public function buildUnionAllQueryPartPublic(): ?string
61
            {
62
                return $this->buildUnionAllQueryPart();
63
            }
64
        };
65
    }
66
67
    public function testUnionAll()
68
    {
69
        $this->queryBuilder->method('commandData')->willReturn(static::QUERY_BUILDER_COMMAND);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Janisbiz\LightOrm\Dms\My...r\QueryBuilderInterface. ( Ignorable by Annotation )

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

69
        $this->queryBuilder->/** @scrutinizer ignore-call */ 
70
                             method('commandData')->willReturn(static::QUERY_BUILDER_COMMAND);

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...
70
71
        $unionAllQueries = [
72
            $this->queryBuilder,
73
            $this->queryBuilder,
74
            $this->queryBuilder,
75
        ];
76
77
        foreach ($unionAllQueries as $unionAllQuery) {
78
            $this->unionTraitClass->unionAll($unionAllQuery);
0 ignored issues
show
Bug introduced by
It seems like unionAll() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

78
            $this->unionTraitClass->/** @scrutinizer ignore-call */ 
79
                                    unionAll($unionAllQuery);
Loading history...
79
        }
80
81
        $this->assertCount(\count($unionAllQueries), $this->unionTraitClass->unionAllData());
0 ignored issues
show
Bug introduced by
The method unionAllData() does not exist on Janisbiz\LightOrm\Dms\My...ilder\Traits\UnionTrait. Did you maybe mean unionAll()? ( Ignorable by Annotation )

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

81
        $this->assertCount(\count($unionAllQueries), $this->unionTraitClass->/** @scrutinizer ignore-call */ unionAllData());

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
It seems like unionAllData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

81
        $this->assertCount(\count($unionAllQueries), $this->unionTraitClass->/** @scrutinizer ignore-call */ unionAllData());
Loading history...
82
        $this->assertEquals(
83
            \array_map(
84
                function (QueryBuilderInterface $unionAllQuery) {
85
                    return \sprintf('(%s)', $unionAllQuery->buildQuery());
86
                },
87
                $unionAllQueries
88
            ),
89
            $this->unionTraitClass->unionAllData()
90
        );
91
        $this->assertCount(\count(self::QUERY_BUILDER_BIND_DATA), $this->unionTraitClass->bindData());
0 ignored issues
show
Bug introduced by
It seems like bindData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

91
        $this->assertCount(\count(self::QUERY_BUILDER_BIND_DATA), $this->unionTraitClass->/** @scrutinizer ignore-call */ bindData());
Loading history...
92
        $this->assertEquals(self::QUERY_BUILDER_BIND_DATA, $this->unionTraitClass->bindData());
93
    }
94
95
    public function testUnionAllWhenInvalidQueryBuilderPassed()
96
    {
97
        $this->expectException(QueryBuilderException::class);
98
        $this->expectExceptionMessage('$queryBuilder should be with valid command! Valid command: "SELECT"');
99
100
        $this->unionTraitClass->unionAll($this->queryBuilder);
101
    }
102
103
    public function testBuildUnionAllQueryPart()
104
    {
105
        $this->queryBuilder->method('commandData')->willReturn(static::QUERY_BUILDER_COMMAND);
106
107
        $unionAllQueries = [
108
            $this->queryBuilder,
109
            $this->queryBuilder,
110
            $this->queryBuilder,
111
        ];
112
113
        foreach ($unionAllQueries as $unionAllQuery) {
114
            $this->unionTraitClass->unionAll($unionAllQuery);
115
        }
116
117
        $this->assertEquals(
118
            \implode(
119
                ' UNION ALL ',
120
                \array_map(
121
                    function (QueryBuilderInterface $unionAllQuery) {
122
                        return \sprintf('(%s)', $unionAllQuery->buildQuery());
123
                    },
124
                    $unionAllQueries
125
                )
126
            ),
127
            $this->unionTraitClass->buildUnionAllQueryPartPublic()
0 ignored issues
show
Bug introduced by
It seems like buildUnionAllQueryPartPublic() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

127
            $this->unionTraitClass->/** @scrutinizer ignore-call */ 
128
                                    buildUnionAllQueryPartPublic()
Loading history...
Bug introduced by
The method buildUnionAllQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...ilder\Traits\UnionTrait. Did you maybe mean buildUnionAllQueryPart()? ( Ignorable by Annotation )

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

127
            $this->unionTraitClass->/** @scrutinizer ignore-call */ 
128
                                    buildUnionAllQueryPartPublic()

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...
128
        );
129
    }
130
131
    public function testBuildUnionAllQueryPartWhenEmpty()
132
    {
133
        $this->unionTraitClass->clearUnionAllData();
0 ignored issues
show
Bug introduced by
It seems like clearUnionAllData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

133
        $this->unionTraitClass->/** @scrutinizer ignore-call */ 
134
                                clearUnionAllData();
Loading history...
134
135
        $this->assertEquals(null, $this->unionTraitClass->buildUnionAllQueryPartPublic());
136
    }
137
}
138