Completed
Push — master ( 53b35c...4f4192 )
by Peter
06:07
created

PlatformFunctionSpec::it_is_a_operand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace tests\Happyr\DoctrineSpecification\Operand;
4
5
use Doctrine\ORM\Configuration;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\QueryBuilder;
8
use Happyr\DoctrineSpecification\Exception\InvalidArgumentException;
9
use Happyr\DoctrineSpecification\Exception\NotConvertibleException;
10
use Happyr\DoctrineSpecification\Operand\Field;
11
use Happyr\DoctrineSpecification\Operand\Operand;
12
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
13
use PhpSpec\ObjectBehavior;
14
15
/**
16
 * @mixin PlatformFunction
17
 */
18
class PlatformFunctionSpec extends ObjectBehavior
19
{
20
    private $functionName = 'UPPER';
21
22
    private $arguments = 'foo';
23
24
    public function let()
25
    {
26
        $this->beConstructedWith($this->functionName, $this->arguments);
27
    }
28
29
    public function it_is_a_platform_function()
30
    {
31
        $this->shouldBeAnInstanceOf(PlatformFunction::class);
32
    }
33
34
    public function it_is_a_operand()
35
    {
36
        $this->shouldBeAnInstanceOf(Operand::class);
37
    }
38
39
    public function it_is_transformable_doctrine_function(QueryBuilder $qb)
40
    {
41
        $dqlAlias = 'a';
42
        $expression = 'UPPER(a.foo)';
43
44
        $this->transform($qb, $dqlAlias)->shouldReturn($expression);
45
    }
46
47
    public function it_is_transformable_many_arguments(QueryBuilder $qb)
48
    {
49
        $dqlAlias = 'a';
50
        $expression = 'concat(a.foo, a.bar)';
51
52
        $this->beConstructedWith('concat', new Field('foo'), new Field('bar'));
53
54
        $this->transform($qb, $dqlAlias)->shouldReturn($expression);
55
    }
56
57 View Code Duplication
    public function it_is_transformable_custom_string_function(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
        QueryBuilder $qb,
59
        EntityManagerInterface $em,
60
        Configuration $configuration
61
    ) {
62
        $dqlAlias = 'a';
63
        $functionName = 'foo';
64
        $expression = 'foo(a.foo)';
65
66
        $qb->getEntityManager()->willReturn($em);
67
        $em->getConfiguration()->willReturn($configuration);
68
        $configuration->getCustomStringFunction($functionName)->willReturn('ToStringClass');
69
        $configuration->getCustomNumericFunction($functionName)->willReturn(null);
70
        $configuration->getCustomDatetimeFunction($functionName)->willReturn(null);
71
72
        $this->beConstructedWith($functionName, 'foo');
73
74
        $this->transform($qb, $dqlAlias)->shouldReturn($expression);
75
    }
76
77 View Code Duplication
    public function it_is_transformable_custom_numeric_function(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
        QueryBuilder $qb,
79
        EntityManagerInterface $em,
80
        Configuration $configuration
81
    ) {
82
        $dqlAlias = 'a';
83
        $functionName = 'foo';
84
        $expression = 'foo(a.foo)';
85
86
        $qb->getEntityManager()->willReturn($em);
87
        $em->getConfiguration()->willReturn($configuration);
88
        $configuration->getCustomStringFunction($functionName)->willReturn(null);
89
        $configuration->getCustomNumericFunction($functionName)->willReturn('ToNumericClass');
90
        $configuration->getCustomDatetimeFunction($functionName)->willReturn(null);
91
92
        $this->beConstructedWith($functionName, 'foo');
93
94
        $this->transform($qb, $dqlAlias)->shouldReturn($expression);
95
    }
96
97 View Code Duplication
    public function it_is_transformable_custom_datetime_function(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
        QueryBuilder $qb,
99
        EntityManagerInterface $em,
100
        Configuration $configuration
101
    ) {
102
        $dqlAlias = 'a';
103
        $functionName = 'foo';
104
        $expression = 'foo(a.foo)';
105
106
        $qb->getEntityManager()->willReturn($em);
107
        $em->getConfiguration()->willReturn($configuration);
108
        $configuration->getCustomStringFunction($functionName)->willReturn(null);
109
        $configuration->getCustomNumericFunction($functionName)->willReturn(null);
110
        $configuration->getCustomDatetimeFunction($functionName)->willReturn('ToDatetimeClass');
111
112
        $this->beConstructedWith($functionName, 'foo');
113
114
        $this->transform($qb, $dqlAlias)->shouldReturn($expression);
115
    }
116
117
    public function it_is_transformable_undefined_function(
118
        QueryBuilder $qb,
119
        EntityManagerInterface $em,
120
        Configuration $configuration
121
    ) {
122
        $functionName = 'foo';
123
124
        $qb->getEntityManager()->willReturn($em);
125
        $em->getConfiguration()->willReturn($configuration);
126
        $configuration->getCustomStringFunction($functionName)->willReturn(null);
127
        $configuration->getCustomNumericFunction($functionName)->willReturn(null);
128
        $configuration->getCustomDatetimeFunction($functionName)->willReturn(null);
129
130
        $this->beConstructedWith($functionName, 'foo');
131
        $this->shouldThrow(InvalidArgumentException::class)->during('transform', array($qb, 'a'));
132
    }
133
134
    public function it_is_transformable_not_convertible(QueryBuilder $qb)
135
    {
136
        $this->beConstructedWith('concat', ['foo', 'bar', 'baz']);
137
138
        $this->shouldThrow(NotConvertibleException::class)->during('transform', array($qb, 'a'));
139
    }
140
}
141