Failed Conditions
Branch master (116909)
by Guilherme
09:02
created

MonologDBHandlerTest::testWriteFailureWithCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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\LogBundle\Tests\Handler;
12
13
use Doctrine\Common\Cache\CacheProvider;
14
use Doctrine\ORM\EntityManagerInterface;
15
use LoginCidadao\LogBundle\Handler\MonologDBHandler;
16
17
class MonologDBHandlerTest extends \PHPUnit_Framework_TestCase
18
{
19
    private $record = [
20
        'message' => 'Some message',
21
        'level' => 100,
22
        'level_name' => 'debug',
23
        'extra' => ['this' => 'is extra'],
24
        'context' => ['that is' => 'the context'],
25
    ];
26
27
    public function testWriteSuccessWithCache()
28
    {
29
        $em = $this->getEntityManager();
30
        $em->expects($this->once())->method('flush');
31
        $em->expects($this->once())->method('persist')
32
            ->with($this->isInstanceOf('LoginCidadao\LogBundle\Entity\Log'));
33
34
        $cache = $this->getCache();
35
        $cache->expects($this->once())->method('contains')
36
            ->with(MonologDBHandler::DISABLE_LOGGING_FLAG_KEY)->willReturn(false);
37
38
        $handler = new MonologDBHandler($em);
39
        $handler->setCacheProvider($cache);
40
        $handler->handle($this->record);
41
    }
42
43
    public function testWriteIgnoredWithCache()
44
    {
45
        $em = $this->getEntityManager();
46
        $cache = $this->getCache();
47
        $cache->expects($this->once())->method('contains')
48
            ->with(MonologDBHandler::DISABLE_LOGGING_FLAG_KEY)->willReturn(true);
49
50
        $handler = new MonologDBHandler($em);
51
        $handler->setCacheProvider($cache);
52
        $handler->handle($this->record);
53
    }
54
55
    public function testWriteSuccessWithoutCache()
56
    {
57
        $em = $this->getEntityManager();
58
        $em->expects($this->once())->method('flush');
59
        $em->expects($this->once())->method('persist')
60
            ->with($this->isInstanceOf('LoginCidadao\LogBundle\Entity\Log'));
61
62
        $handler = new MonologDBHandler($em);
63
        $handler->handle($this->record);
64
    }
65
66
    public function testWriteFailureWithCache()
67
    {
68
        $em = $this->getEntityManager();
69
        $em->expects($this->once())->method('persist')
70
            ->with($this->isInstanceOf('LoginCidadao\LogBundle\Entity\Log'))
71
            ->willThrowException(new \RuntimeException('Something failed!'));
72
        $em->expects($this->never())->method('flush');
73
74
        $cache = $this->getCache();
75
        $cache->expects($this->once())->method('contains')
76
            ->with(MonologDBHandler::DISABLE_LOGGING_FLAG_KEY)->willReturn(false);
77
        $cache->expects($this->once())->method('save')
78
            ->with(MonologDBHandler::DISABLE_LOGGING_FLAG_KEY, true, MonologDBHandler::LIFETIME);
79
80
        $handler = new MonologDBHandler($em);
81
        $handler->setCacheProvider($cache);
82
        $handler->handle($this->record);
83
    }
84
85
    public function testWriteFailureWithoutCache()
86
    {
87
        $em = $this->getEntityManager();
88
        $em->expects($this->once())->method('persist')
89
            ->with($this->isInstanceOf('LoginCidadao\LogBundle\Entity\Log'))
90
            ->willThrowException(new \RuntimeException('Something failed!'));
91
        $em->expects($this->never())->method('flush');
92
93
        $handler = new MonologDBHandler($em);
94
        $handler->handle($this->record);
95
    }
96
97
    /**
98
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
99
     */
100
    private function getEntityManager()
101
    {
102
        return $this->getMock('Doctrine\ORM\EntityManagerInterface');
103
    }
104
105
    /**
106
     * @return \PHPUnit_Framework_MockObject_MockObject|CacheProvider
107
     */
108
    private function getCache()
109
    {
110
        return $this->getMock('Doctrine\Common\Cache\CacheProvider');
111
    }
112
}
113