Passed
Push — issue#767 ( 50feef...0909e2 )
by Guilherme
05:13
created

StatisticsSubscriberTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetSubscribedEvents() 0 3 1
B testPostPersist() 0 41 1
A testPostPersistNonAuthorization() 0 8 1
B testPostRemove() 0 39 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\StatsBundle\Tests\EventListener;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use Doctrine\ORM\Event\LifecycleEventArgs;
15
use LoginCidadao\CoreBundle\Entity\Authorization;
16
use LoginCidadao\CoreBundle\Entity\Person;
17
use LoginCidadao\OAuthBundle\Entity\Client;
18
use LoginCidadao\StatsBundle\EventListener\StatisticsSubscriber;
19
20
class StatisticsSubscriberTest extends \PHPUnit_Framework_TestCase
21
{
22
    public function testGetSubscribedEvents()
23
    {
24
        $this->assertSame(['postPersist', 'postRemove'], (new StatisticsSubscriber())->getSubscribedEvents());
25
    }
26
27
    /**
28
     * @throws \Doctrine\ORM\OptimisticLockException
29
     */
30
    public function testPostPersistNonAuthorization()
31
    {
32
        /** @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject $em */
33
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
34
        $args = new LifecycleEventArgs(new \stdClass(), $em);
35
36
        $subscriber = new StatisticsSubscriber();
37
        $subscriber->postPersist($args);
38
    }
39
40
    /**
41
     * @throws \Doctrine\ORM\OptimisticLockException
42
     */
43
    public function testPostPersist()
44
    {
45
        $person = new Person();
46
        $clientId = 'client666';
47
        $authorization = new Authorization();
48
        $authorization->setPerson($person);
49
        $authorization->setClient((new Client())->setId($clientId));
50
51
        $count = [
52
            ['qty' => 999],
53
        ];
54
55
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
56
            ->disableOriginalConstructor()->getMock();
57
        $repo->expects($this->exactly(2))
58
            ->method('getCountPerson')->with($person, $clientId)
59
            ->willReturn($count);
60
61
        $statsRepo = $this->getMockBuilder('LoginCidadao\StatsBundle\Entity\StatisticsRepository')
62
            ->disableOriginalConstructor()->getMock();
63
        $statsRepo->expects($this->once())
64
            ->method('findOneBy')->with($this->isType('array'))
65
            ->willReturnCallback(function ($where) use ($clientId) {
66
                $this->assertInstanceOf('\DateTime', $where['timestamp']);
67
                $this->assertSame('agg.client.users', $where['index']);
68
                $this->assertSame($clientId, $where['key']);
69
            });
70
71
        /** @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject $em */
72
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
73
        $em->expects($this->exactly(3))
74
            ->method('getRepository')
75
            ->willReturnMap([
76
                ['LoginCidadaoOAuthBundle:Client', $repo],
77
                ['LoginCidadaoStatsBundle:Statistics', $statsRepo],
78
            ]);
79
80
        $args = new LifecycleEventArgs($authorization, $em);
81
82
        $subscriber = new StatisticsSubscriber();
83
        $subscriber->postPersist($args);
84
    }
85
86
    /**
87
     * @throws \Doctrine\ORM\OptimisticLockException
88
     */
89
    public function testPostRemove()
90
    {
91
        $person = new Person();
92
        $clientId = 'client666';
93
        $authorization = new Authorization();
94
        $authorization->setPerson($person);
95
        $authorization->setClient((new Client())->setId($clientId));
96
97
        $count = [];
98
99
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
100
            ->disableOriginalConstructor()->getMock();
101
        $repo->expects($this->exactly(2))
102
            ->method('getCountPerson')->with($person, $clientId)
103
            ->willReturn($count);
104
105
        $statsRepo = $this->getMockBuilder('LoginCidadao\StatsBundle\Entity\StatisticsRepository')
106
            ->disableOriginalConstructor()->getMock();
107
        $statsRepo->expects($this->once())
108
            ->method('findOneBy')->with($this->isType('array'))
109
            ->willReturnCallback(function ($where) use ($clientId) {
110
                $this->assertInstanceOf('\DateTime', $where['timestamp']);
111
                $this->assertSame('agg.client.users', $where['index']);
112
                $this->assertSame($clientId, $where['key']);
113
            });
114
115
        /** @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject $em */
116
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
117
        $em->expects($this->exactly(3))
118
            ->method('getRepository')
119
            ->willReturnMap([
120
                ['LoginCidadaoOAuthBundle:Client', $repo],
121
                ['LoginCidadaoStatsBundle:Statistics', $statsRepo],
122
            ]);
123
124
        $args = new LifecycleEventArgs($authorization, $em);
125
126
        $subscriber = new StatisticsSubscriber();
127
        $subscriber->postRemove($args);
128
    }
129
}
130