OrderByTraitTest::testOrderBy()
last analyzed

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 15
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\ConditionEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\Enum\KeywordEnum;
7
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
8
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\OrderByTrait;
9
10
class OrderByTraitTest extends AbstractTraitTestCase
11
{
12
    const ORDER_BY_DEFAULT = [
13
        'col1 DESC',
14
        'col2 ASC',
15
    ];
16
    const ORDER_BY_COLUMN = 'col3';
17
    const ORDER_BY_COLUMNS = [
18
        'col4',
19
        'col5',
20
    ];
21
    const ORDER_BY_EMPTY = null;
22
    const ORDER_BY_INVALID_KEYWORD = 'INVALID';
23
24
    /**
25
     * @var OrderByTrait
26
     */
27
    private $orderByTraitClass;
28
29
    public function setUp()
30
    {
31
        $this->orderBy = static::ORDER_BY_DEFAULT;
0 ignored issues
show
Bug introduced by
The property orderBy does not exist on Janisbiz\LightOrm\Tests\...Traits\OrderByTraitTest. Did you mean orderByTraitClass?
Loading history...
32
        $this->orderByTraitClass = new class (OrderByTraitTest::ORDER_BY_DEFAULT)
33
        {
34
            use OrderByTrait;
35
36
            /**
37
             * @param array $orderByDataDefault
38
             */
39
            public function __construct(array $orderByDataDefault)
40
            {
41
                $this->orderBy = $orderByDataDefault;
42
            }
43
44
            /**
45
             * @return array
46
             */
47
            public function orderByData(): array
48
            {
49
                return $this->orderBy;
50
            }
51
52
            public function clearOrderByData()
53
            {
54
                $this->orderBy = [];
55
            }
56
57
            /**
58
             * @return null|string
59
             */
60
            public function buildOrderByQueryPartPublic(): ?string
61
            {
62
                return $this->buildOrderByQueryPart();
63
            }
64
        };
65
    }
66
67
    /**
68
     * @dataProvider orderByData
69
     *
70
     * @param array|string $columns
71
     * @param string $keyword
72
     */
73
    public function testOrderBy($columns, $keyword)
74
    {
75
        $object = $this->orderByTraitClass->orderBy($columns, $keyword);
76
        $this->assertObjectUsesTrait(OrderByTrait::class, $object);
77
        $this->assertEquals(
78
            \array_merge(
79
                static::ORDER_BY_DEFAULT,
80
                \array_map(
81
                    function ($column) use ($keyword) {
82
                        return \sprintf('%s %s', $column, $keyword);
83
                    },
84
                    \is_array($columns) ? $columns : [$columns]
85
                )
86
            ),
87
            $this->orderByTraitClass->orderByData()
0 ignored issues
show
Bug introduced by
The method orderByData() does not exist on Janisbiz\LightOrm\Dms\My...der\Traits\OrderByTrait. Did you maybe mean orderBy()? ( Ignorable by Annotation )

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

87
            $this->orderByTraitClass->/** @scrutinizer ignore-call */ 
88
                                      orderByData()

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...
88
        );
89
    }
90
91
    /**
92
     * @dataProvider orderByData
93
     *
94
     * @param array|string $columns
95
     * @param string $keyword
96
     */
97
    public function testBuildOrderByQueryPart($columns, $keyword)
98
    {
99
        $this->orderByTraitClass->orderBy($columns, $keyword);
100
101
        $this->assertEquals(
102
            \sprintf('%s %s', ConditionEnum::ORDER_BY, \implode(', ', $this->orderByTraitClass->orderByData())),
103
            $this->orderByTraitClass->buildOrderByQueryPartPublic()
0 ignored issues
show
Bug introduced by
The method buildOrderByQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...der\Traits\OrderByTrait. Did you maybe mean buildOrderByQueryPart()? ( Ignorable by Annotation )

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

103
            $this->orderByTraitClass->/** @scrutinizer ignore-call */ 
104
                                      buildOrderByQueryPartPublic()

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...
104
        );
105
    }
106
107
    public function testBuildOrderByQueryPartWhenEmpty()
108
    {
109
        $this->orderByTraitClass->clearOrderByData();
0 ignored issues
show
Bug introduced by
It seems like clearOrderByData() 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

109
        $this->orderByTraitClass->/** @scrutinizer ignore-call */ 
110
                                  clearOrderByData();
Loading history...
110
111
        $this->assertEquals(null, $this->orderByTraitClass->buildOrderByQueryPartPublic());
112
    }
113
114
    /**
115
     *
116
     * @return array
117
     */
118
    public function orderByData()
119
    {
120
        return [
121
            [
122
                static::ORDER_BY_COLUMN,
123
                KeywordEnum::ASC,
124
            ],
125
            [
126
                static::ORDER_BY_COLUMN,
127
                KeywordEnum::DESC,
128
            ],
129
            [
130
                static::ORDER_BY_COLUMNS,
131
                KeywordEnum::ASC,
132
            ],
133
            [
134
                static::ORDER_BY_COLUMNS,
135
                KeywordEnum::DESC,
136
            ],
137
        ];
138
    }
139
140
    public function testOrderByWithEmptyOrderBy()
141
    {
142
        $this->expectException(QueryBuilderException::class);
143
        $this->expectExceptionMessage('You must pass $orderBy to orderBy method!');
144
145
        $this->orderByTraitClass->orderBy(static::ORDER_BY_EMPTY);
146
    }
147
148
    public function testOrderByWithInvalidKeyword()
149
    {
150
        $this->expectException(QueryBuilderException::class);
151
        $this->expectExceptionMessage('Invalid $keyword "INVALID" for orderBy!');
152
153
        $this->orderByTraitClass->orderBy(static::ORDER_BY_COLUMN, static::ORDER_BY_INVALID_KEYWORD);
154
    }
155
}
156