Completed
Push — master ( a8b991...dda2b8 )
by Marco
06:05 queued 02:56
created

MongoDBCacheTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 56
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 2
A tearDown() 0 6 2
A testGetStats() 0 11 1
A testMongoCursorExceptionsDoNotBubbleUp() 0 11 1
A _getCacheDriver() 0 4 1
1
<?php
2
3
namespace Doctrine\Tests\Common\Cache;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\MongoDBCache;
7
use MongoClient;
8
use MongoCollection;
9
10
/**
11
 * @requires extension mongo
12
 */
13
class MongoDBCacheTest extends CacheTest
14
{
15
    /**
16
     * @var MongoCollection
17
     */
18
    private $collection;
19
20
    protected function setUp()
21
    {
22
        if ( ! version_compare(phpversion('mongo'), '1.3.0', '>=')) {
23
            $this->markTestSkipped('Mongo >= 1.3.0 is required.');
24
        }
25
26
        $mongo = new MongoClient();
27
        $this->collection = $mongo->selectCollection('doctrine_common_cache', 'test');
28
    }
29
30
    protected function tearDown()
31
    {
32
        if ($this->collection instanceof MongoCollection) {
33
            $this->collection->drop();
34
        }
35
    }
36
37
    public function testGetStats()
38
    {
39
        $cache = $this->_getCacheDriver();
40
        $stats = $cache->getStats();
41
42
        $this->assertNull($stats[Cache::STATS_HITS]);
43
        $this->assertNull($stats[Cache::STATS_MISSES]);
44
        $this->assertGreaterThan(0, $stats[Cache::STATS_UPTIME]);
45
        $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
46
        $this->assertNull($stats[Cache::STATS_MEMORY_AVAILABLE]);
47
    }
48
49
    /**
50
     * @group 108
51
     */
52
    public function testMongoCursorExceptionsDoNotBubbleUp()
53
    {
54
        /* @var $collection \MongoCollection|\PHPUnit_Framework_MockObject_MockObject */
55
        $collection = $this->getMock('MongoCollection', array(), array(), '', false);
56
57
        $collection->expects(self::once())->method('update')->willThrowException(new \MongoCursorException());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in MongoCollection.

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...
58
59
        $cache = new MongoDBCache($collection);
60
61
        self::assertFalse($cache->save('foo', 'bar'));
62
    }
63
64
    protected function _getCacheDriver()
65
    {
66
        return new MongoDBCache($this->collection);
67
    }
68
}
69