Completed
Push — master ( 0136c1...94aca9 )
by Lukas Kahwe
03:29
created

GridFSLoaderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 56
rs 10
1
<?php
2
3
namespace Liip\ImagineBundle\Tests\Binary\Loader;
4
5
use Doctrine\MongoDB\GridFSFile;
6
use Doctrine\ODM\MongoDB\DocumentRepository;
7
use Liip\ImagineBundle\Binary\Loader\GridFSLoader;
8
9
/**
10
 * @covers Liip\ImagineBundle\Binary\Loader\GridFSLoader<extended>
11
 */
12
class GridFSLoaderTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var DocumentRepository
16
     */
17
    private $repo;
18
19
    /**
20
     * @var GridFSLoader
21
     */
22
    private $loader;
23
24
    public function setUp()
25
    {
26
        if (!extension_loaded('mongodb')) {
27
            $this->markTestSkipped('ext/mongodb is not installed');
28
        }
29
        if (!class_exists('Doctrine\MongoDB\GridFSFile')) {
30
            $this->markTestSkipped('doctrine mongo odm is not installed');
31
        }
32
        $this->repo = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentRepository')->disableOriginalConstructor()->getMock();
33
34
        $dm = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentManager')->disableOriginalConstructor()->getMock();
35
        $dm->expects($this->any())->method('getRepository')->with('\Foo\Bar')->will($this->returnValue($this->repo));
36
37
        $this->loader = new GridFSLoader($dm, '\Foo\Bar');
38
    }
39
40
    public function testFindWithValidDocument()
41
    {
42
        $image = new GridFSFile();
43
        $image->setBytes('01010101');
44
45
        $imageDocument = $this->getMock('ImageDocument', array('getFile'));
46
        $imageDocument
47
            ->expects($this->any())
48
            ->method('getFile')
49
            ->with()
50
            ->will($this->returnValue($image))
51
        ;
52
53
        $this->repo->expects($this->atLeastOnce())->method('find')->with($this->isInstanceOf('\MongoId'))->will($this->returnValue($imageDocument));
54
55
        $this->assertEquals('01010101', $this->loader->find('0123456789abcdef01234567'));
56
    }
57
58
    /**
59
     * @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
60
     */
61
    public function testFindWithInValidDocument()
62
    {
63
        $this->repo->expects($this->atLeastOnce())->method('find')->with($this->isInstanceOf('\MongoId'))->will($this->returnValue(null));
64
65
        $this->loader->find('0123456789abcdef01234567');
66
    }
67
}
68