1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace tests\Happyr\DoctrineSpecification\Specification; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Happyr\DoctrineSpecification\Filter\Equals; |
19
|
|
|
use Happyr\DoctrineSpecification\Query\GroupBy; |
20
|
|
|
use Happyr\DoctrineSpecification\Specification\CountOf; |
21
|
|
|
use Happyr\DoctrineSpecification\Specification\Specification; |
22
|
|
|
use PhpSpec\ObjectBehavior; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @mixin CountOf |
26
|
|
|
*/ |
27
|
|
|
class CountOfSpec extends ObjectBehavior |
28
|
|
|
{ |
29
|
|
|
public function let() |
30
|
|
|
{ |
31
|
|
|
$this->beConstructedWith(null); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function it_is_a_CountOf() |
35
|
|
|
{ |
36
|
|
|
$this->shouldBeAnInstanceOf(CountOf::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function it_is_a_specification() |
40
|
|
|
{ |
41
|
|
|
$this->shouldHaveType(Specification::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function it_count_of_all(QueryBuilder $qb) |
45
|
|
|
{ |
46
|
|
|
$dqlAlias = 'a'; |
47
|
|
|
|
48
|
|
|
$qb->select(sprintf('COUNT(%s)', $dqlAlias))->shouldBeCalled(); |
49
|
|
|
|
50
|
|
|
$this->getFilter($qb, $dqlAlias)->shouldBe(''); |
51
|
|
|
$this->modify($qb, $dqlAlias); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function it_count_of_all_grouped_by_id(QueryBuilder $qb) |
55
|
|
|
{ |
56
|
|
|
$field = 'id'; |
57
|
|
|
$dqlAlias = 'a'; |
58
|
|
|
|
59
|
|
|
$this->beConstructedWith(new GroupBy($field, $dqlAlias)); |
60
|
|
|
|
61
|
|
|
$qb->select(sprintf('COUNT(%s)', $dqlAlias))->shouldBeCalled(); |
62
|
|
|
$qb->addGroupBy(sprintf('%s.%s', $dqlAlias, $field))->shouldBeCalled(); |
63
|
|
|
|
64
|
|
|
$this->getFilter($qb, $dqlAlias)->shouldBe(''); |
65
|
|
|
$this->modify($qb, $dqlAlias); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function it_count_of_all_with_group_is_foo(QueryBuilder $qb) |
69
|
|
|
{ |
70
|
|
|
$field = 'group'; |
71
|
|
|
$value = 'foo'; |
72
|
|
|
$dqlAlias = 'a'; |
73
|
|
|
$parametersCount = 0; |
74
|
|
|
$paramName = 'comparison_'.$parametersCount; |
75
|
|
|
|
76
|
|
|
$this->beConstructedWith(new Equals($field, $value, $dqlAlias)); |
77
|
|
|
|
78
|
|
|
$qb->select(sprintf('COUNT(%s)', $dqlAlias))->shouldBeCalled(); |
79
|
|
|
$qb->getParameters()->willReturn(new ArrayCollection()); |
80
|
|
|
$qb->setParameter($paramName, $value, null)->shouldBeCalled(); |
81
|
|
|
|
82
|
|
|
$this->getFilter($qb, $dqlAlias)->shouldBe(sprintf('%s.%s = :%s', $dqlAlias, $field, $paramName)); |
83
|
|
|
$this->modify($qb, $dqlAlias); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|