Completed
Push — master ( 8612ee...47efba )
by Peter
06:03
created

CountOfSpec::let()   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\Specification;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\QueryBuilder;
7
use Happyr\DoctrineSpecification\Filter\Equals;
8
use Happyr\DoctrineSpecification\Query\GroupBy;
9
use Happyr\DoctrineSpecification\Specification\CountOf;
10
use PhpSpec\ObjectBehavior;
11
12
/**
13
 * @mixin CountOf
14
 */
15
class CountOfSpec extends ObjectBehavior
16
{
17
    public function let()
18
    {
19
        $this->beConstructedWith(null);
20
    }
21
22
    public function it_is_a_specification()
23
    {
24
        $this->shouldHaveType('Happyr\DoctrineSpecification\Specification\Specification');
25
    }
26
27
    public function it_count_of_all(QueryBuilder $qb)
28
    {
29
        $dqlAlias = 'a';
30
31
        $qb->select(sprintf('COUNT(%s)', $dqlAlias))->shouldBeCalled();
32
33
        $this->getFilter($qb, $dqlAlias)->shouldBe('');
34
        $this->modify($qb, $dqlAlias);
35
    }
36
37
    public function it_count_of_all_grouped_by_id(QueryBuilder $qb)
38
    {
39
        $field = 'id';
40
        $dqlAlias = 'a';
41
42
        $this->beConstructedWith(new GroupBy($field, $dqlAlias));
43
44
        $qb->select(sprintf('COUNT(%s)', $dqlAlias))->shouldBeCalled();
45
        $qb->addGroupBy(sprintf('%s.%s', $dqlAlias, $field))->shouldBeCalled();
46
47
        $this->getFilter($qb, $dqlAlias)->shouldBe('');
48
        $this->modify($qb, $dqlAlias);
49
    }
50
51
    public function it_count_of_all_with_group_is_foo(QueryBuilder $qb)
52
    {
53
        $field = 'group';
54
        $value = 'foo';
55
        $dqlAlias = 'a';
56
        $parameters_count = 0;
57
        $paramName = 'comparison_'.$parameters_count;
58
59
        $this->beConstructedWith(new Equals($field, $value, $dqlAlias));
60
61
        $qb->select(sprintf('COUNT(%s)', $dqlAlias))->shouldBeCalled();
62
        $qb->getParameters()->willReturn(new ArrayCollection());
63
        $qb->setParameter($paramName, $value)->shouldBeCalled();
64
65
        $this->getFilter($qb, $dqlAlias)->shouldBe(sprintf('%s.%s = :%s', $dqlAlias, $field, $paramName));
66
        $this->modify($qb, $dqlAlias);
67
    }
68
}
69