GroupByTraitTest::testGroupByWhenEmpty()
last analyzed

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 0
dl 0
loc 6
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\ConditionEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
7
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\GroupByTrait;
8
9
class GroupByTraitTest extends AbstractTraitTestCase
10
{
11
    const GROUP_BY_DEFAULT = [
12
        'group_by1',
13
        'group_by2',
14
    ];
15
    const GROUP_BY_ARRAY = [
16
        'group_by3',
17
        'group_by4',
18
    ];
19
    const GROUP_BY_EMPTY = '';
20
    const GROUP_BY = 'group_by5';
21
22
    /**
23
     * @var GroupByTrait
24
     */
25
    private $groupByTraitClass;
26
27
    public function setUp()
28
    {
29
        $this->groupByTraitClass = new class (GroupByTraitTest::GROUP_BY_DEFAULT)
30
        {
31
            use GroupByTrait;
32
33
            /**
34
             * @param array $groupByDefault
35
             */
36
            public function __construct(array $groupByDefault)
37
            {
38
                $this->groupBy = $groupByDefault;
39
            }
40
41
            /**
42
             * @return array
43
             */
44
            public function groupByData(): array
45
            {
46
                return $this->groupBy;
47
            }
48
49
            public function clearGroupByData()
50
            {
51
                $this->groupBy = [];
52
            }
53
54
            /**
55
             * @return null|string
56
             */
57
            public function buildGroupByQueryPartPublic(): ?string
58
            {
59
                return $this->buildGroupByQueryPart();
60
            }
61
        };
62
    }
63
64
    public function testGroupBy()
65
    {
66
        $this->assertEquals(static::GROUP_BY_DEFAULT, $this->groupByTraitClass->groupByData());
0 ignored issues
show
Bug introduced by
The method groupByData() does not exist on Janisbiz\LightOrm\Dms\My...der\Traits\GroupByTrait. Did you maybe mean groupBy()? ( Ignorable by Annotation )

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

66
        $this->assertEquals(static::GROUP_BY_DEFAULT, $this->groupByTraitClass->/** @scrutinizer ignore-call */ groupByData());

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...
67
68
        $object = $this->groupByTraitClass->groupBy(static::GROUP_BY_ARRAY);
69
        $this->assertObjectUsesTrait(GroupByTrait::class, $object);
70
        $this->assertEquals(
71
            \array_merge(static::GROUP_BY_DEFAULT, static::GROUP_BY_ARRAY),
72
            $this->groupByTraitClass->groupByData()
73
        );
74
75
        $object = $this->groupByTraitClass->groupBy(static::GROUP_BY);
76
        $this->assertObjectUsesTrait(GroupByTrait::class, $object);
77
        $this->assertEquals(
78
            \array_merge(static::GROUP_BY_DEFAULT, static::GROUP_BY_ARRAY, [static::GROUP_BY]),
79
            $this->groupByTraitClass->groupByData()
80
        );
81
    }
82
83
    public function testGroupByWhenEmpty()
84
    {
85
        $this->expectException(QueryBuilderException::class);
86
        $this->expectExceptionMessage('You must pass $groupBy to groupBy method!');
87
88
        $this->groupByTraitClass->groupBy(static::GROUP_BY_EMPTY);
89
    }
90
91
    public function testBuildGroupByQueryPart()
92
    {
93
        $this
94
            ->groupByTraitClass
95
            ->groupBy(static::GROUP_BY_ARRAY)
96
            ->groupBy(static::GROUP_BY)
97
        ;
98
99
        $this->assertEquals(
100
            \sprintf('%s %s', ConditionEnum::GROUP_BY, \implode(', ', $this->groupByTraitClass->groupByData())),
101
            $this->groupByTraitClass->buildGroupByQueryPartPublic()
0 ignored issues
show
Bug introduced by
The method buildGroupByQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...der\Traits\GroupByTrait. Did you maybe mean buildGroupByQueryPart()? ( Ignorable by Annotation )

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

101
            $this->groupByTraitClass->/** @scrutinizer ignore-call */ 
102
                                      buildGroupByQueryPartPublic()

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...
102
        );
103
    }
104
105
    public function testBuildGroupByQueryPartWhenEmpty()
106
    {
107
        $this->groupByTraitClass->clearGroupByData();
0 ignored issues
show
Bug introduced by
It seems like clearGroupByData() 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

107
        $this->groupByTraitClass->/** @scrutinizer ignore-call */ 
108
                                  clearGroupByData();
Loading history...
108
109
        $this->assertEquals(null, $this->groupByTraitClass->buildGroupByQueryPartPublic());
110
    }
111
}
112