HavingTraitTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 9

14 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ havingData() 0 3 1
A hp$0 ➔ __construct() 0 4 1
A hp$0 ➔ testHaving() 0 12 1
A hp$0 ➔ setUp() 0 38 1
A hp$0 ➔ buildHavingQueryPartPublic() 0 3 1
testBuildHavingQueryPart() 0 11 ?
A hp$0 ➔ testBuildHavingQueryPartWhenEmpty() 0 5 1
testBuildHavingQueryPartWhenEmpty() 0 5 ?
A hp$0 ➔ clearHavingData() 0 3 1
A hp$0 ➔ testBuildHavingQueryPart() 0 11 1
A hp$0 ➔ testHavingWithEmptyCondition() 0 6 1
testHavingWithEmptyCondition() 0 6 ?
setUp() 0 38 ?
testHaving() 0 12 ?
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\BindTrait;
8
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\HavingTrait;
9
10
class HavingTraitTest extends AbstractTraitTestCase
11
{
12
    const HAVING_CONDITION_DEFAULT = [
13
        'column1 = :value1Bind',
14
    ];
15
    const HAVING_CONDITION_BIND_DEFAULT = [
16
        'value1Bind' => 'value1',
17
    ];
18
    const HAVING_CONDITION_EMPTY = '';
19
    const HAVING_CONDITION = 'column2 = :value2Bind';
20
    const HAVING_CONDITION_BIND = [
21
        'value2Bind' => 'value2',
22
    ];
23
24
    /**
25
     * @var HavingTrait|BindTrait
26
     */
27
    private $havingTraitClass;
28
29
    public function setUp()
30
    {
31
        $this->havingTraitClass = new class (
32
            HavingTraitTest::HAVING_CONDITION_BIND_DEFAULT,
33
            HavingTraitTest::HAVING_CONDITION_DEFAULT
34
        ) {
35
            use BindTrait;
36
            use HavingTrait;
37
38
            /**
39
             * @param array $bindDefaultData
40
             * @param array $havingDefaultData
41
             */
42
            public function __construct(array $bindDefaultData, array $havingDefaultData)
43
            {
44
                $this->bind = $bindDefaultData;
45
                $this->having = $havingDefaultData;
46
            }
47
48
            /**
49
             * @return array
50
             */
51
            public function havingData(): array
52
            {
53
                return $this->having;
54
            }
55
56
            public function clearHavingData()
57
            {
58
                $this->having = [];
59
            }
60
61
            /**
62
             * @return null|string
63
             */
64
            public function buildHavingQueryPartPublic(): ?string
65
            {
66
                return $this->buildHavingQueryPart();
67
            }
68
        };
69
    }
70
71
    public function testHaving()
72
    {
73
        $object = $this->havingTraitClass->having(static::HAVING_CONDITION, static::HAVING_CONDITION_BIND);
0 ignored issues
show
Bug introduced by
It seems like having() 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

73
        /** @scrutinizer ignore-call */ 
74
        $object = $this->havingTraitClass->having(static::HAVING_CONDITION, static::HAVING_CONDITION_BIND);
Loading history...
74
        $this->assertObjectUsesTrait(BindTrait::class, $object);
75
        $this->assertObjectUsesTrait(HavingTrait::class, $object);
76
        $this->assertEquals(
77
            \array_merge(static::HAVING_CONDITION_DEFAULT, [static::HAVING_CONDITION]),
78
            $this->havingTraitClass->havingData()
0 ignored issues
show
Bug introduced by
It seems like havingData() 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->havingTraitClass->/** @scrutinizer ignore-call */ 
79
                                     havingData()
Loading history...
Bug introduced by
The method havingData() does not exist on Janisbiz\LightOrm\Dms\My...lder\Traits\HavingTrait. Did you maybe mean having()? ( 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->havingTraitClass->/** @scrutinizer ignore-call */ 
79
                                     havingData()

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...
79
        );
80
        $this->assertEquals(
81
            \array_merge(static::HAVING_CONDITION_BIND_DEFAULT, static::HAVING_CONDITION_BIND),
82
            $this->havingTraitClass->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

82
            $this->havingTraitClass->/** @scrutinizer ignore-call */ 
83
                                     bindData()
Loading history...
83
        );
84
    }
85
86
    public function testHavingWithEmptyCondition()
87
    {
88
        $this->expectException(QueryBuilderException::class);
89
        $this->expectExceptionMessage('You must pass $condition to having function!');
90
91
        $this->havingTraitClass->having(static::HAVING_CONDITION_EMPTY);
92
    }
93
94
    public function testBuildHavingQueryPart()
95
    {
96
        $this->havingTraitClass->having(static::HAVING_CONDITION, static::HAVING_CONDITION_BIND);
97
98
        $this->assertEquals(
99
            \sprintf(
100
                '%s %s',
101
                ConditionEnum::HAVING,
102
                \implode(' AND ', \array_unique($this->havingTraitClass->havingData()))
103
            ),
104
            $this->havingTraitClass->buildHavingQueryPartPublic()
0 ignored issues
show
Bug introduced by
It seems like buildHavingQueryPartPublic() 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

104
            $this->havingTraitClass->/** @scrutinizer ignore-call */ 
105
                                     buildHavingQueryPartPublic()
Loading history...
Bug introduced by
The method buildHavingQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...lder\Traits\HavingTrait. Did you maybe mean buildHavingQueryPart()? ( Ignorable by Annotation )

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

104
            $this->havingTraitClass->/** @scrutinizer ignore-call */ 
105
                                     buildHavingQueryPartPublic()

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

110
        $this->havingTraitClass->/** @scrutinizer ignore-call */ 
111
                                 clearHavingData();
Loading history...
111
112
        $this->assertEquals(null, $this->havingTraitClass->buildHavingQueryPartPublic());
113
    }
114
}
115