Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

MonologDBHandlerTest::testWriteSuccessWithCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
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');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Cache\CacheProvider.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
            ->with(MonologDBHandler::DISABLE_LOGGING_FLAG_KEY)->willReturn(false);
37
38
        $handler = new MonologDBHandler($em);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 29 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...BHandler::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39
        $handler->setCacheProvider($cache);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by $this->getCache() on line 34 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...ler::setCacheProvider() does only seem to accept null|object<Doctrine\Common\Cache\CacheProvider>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Cache\CacheProvider.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
            ->with(MonologDBHandler::DISABLE_LOGGING_FLAG_KEY)->willReturn(true);
49
50
        $handler = new MonologDBHandler($em);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 45 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...BHandler::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
        $handler->setCacheProvider($cache);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by $this->getCache() on line 46 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...ler::setCacheProvider() does only seem to accept null|object<Doctrine\Common\Cache\CacheProvider>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
        $handler->handle($this->record);
53
    }
54
55
    public function testWriteSuccessWithoutCache()
56
    {
57
        $em = $this->getEntityManager();
58
        $em->expects($this->once())->method('flush');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
        $em->expects($this->once())->method('persist')
60
            ->with($this->isInstanceOf('LoginCidadao\LogBundle\Entity\Log'));
61
62
        $handler = new MonologDBHandler($em);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 57 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...BHandler::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63
        $handler->handle($this->record);
64
    }
65
66
    public function testWriteFailureWithCache()
67
    {
68
        $em = $this->getEntityManager();
69
        $em->expects($this->once())->method('persist')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Cache\CacheProvider.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 68 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...BHandler::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
        $handler->setCacheProvider($cache);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by $this->getCache() on line 74 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...ler::setCacheProvider() does only seem to accept null|object<Doctrine\Common\Cache\CacheProvider>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82
        $handler->handle($this->record);
83
    }
84
85
    public function testWriteFailureWithoutCache()
86
    {
87
        $em = $this->getEntityManager();
88
        $em->expects($this->once())->method('persist')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 87 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\LogBundle\H...BHandler::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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