1 | <?php |
||
11 | class AbstractDoctrineLoaderTest extends \PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @var ObjectRepository |
||
15 | */ |
||
16 | private $om; |
||
17 | |||
18 | /** |
||
19 | * @var AbstractDoctrineLoader |
||
20 | */ |
||
21 | private $loader; |
||
22 | |||
23 | public function setUp() |
||
24 | { |
||
25 | $this->om = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
||
26 | |||
27 | $this->loader = $this->getMockBuilder('Liip\ImagineBundle\Binary\Loader\AbstractDoctrineLoader')->setConstructorArgs(array($this->om))->getMockForAbstractClass(); |
||
28 | } |
||
29 | |||
30 | public function testFindWithValidObjectFirstHit() |
||
31 | { |
||
32 | $image = new \stdClass(); |
||
33 | |||
34 | $this->loader->expects($this->atLeastOnce())->method('mapPathToId')->with('/foo/bar')->will($this->returnValue(1337)); |
||
35 | $this->loader->expects($this->atLeastOnce())->method('getStreamFromImage')->with($image)->will($this->returnValue(fopen('data://text/plain,foo', 'r'))); |
||
36 | |||
37 | $this->om->expects($this->atLeastOnce())->method('find')->with(null, 1337)->will($this->returnValue($image)); |
||
38 | |||
39 | $this->assertEquals('foo', $this->loader->find('/foo/bar')); |
||
40 | } |
||
41 | |||
42 | public function testFindWithValidObjectSecondHit() |
||
43 | { |
||
44 | $image = new \stdClass(); |
||
45 | |||
46 | $this->loader->expects($this->atLeastOnce())->method('mapPathToId')->will($this->returnValueMap(array( |
||
47 | array('/foo/bar.png', 1337), |
||
48 | array('/foo/bar', 4711), |
||
49 | ))); |
||
50 | |||
51 | $this->loader->expects($this->atLeastOnce())->method('getStreamFromImage')->with($image)->will($this->returnValue(fopen('data://text/plain,foo', 'r'))); |
||
52 | |||
53 | $this->om->expects($this->atLeastOnce())->method('find')->will($this->returnValueMap(array( |
||
54 | array(null, 1337, null), |
||
55 | array(null, 4711, $image), |
||
56 | ))); |
||
57 | |||
58 | $this->assertEquals('foo', $this->loader->find('/foo/bar.png')); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException |
||
63 | */ |
||
64 | public function testFindWithInvalidObject() |
||
65 | { |
||
66 | $this->loader->expects($this->atLeastOnce())->method('mapPathToId')->with('/foo/bar')->will($this->returnValue(1337)); |
||
67 | $this->loader->expects($this->never())->method('getStreamFromImage'); |
||
68 | |||
69 | $this->om->expects($this->atLeastOnce())->method('find')->with(null, 1337)->will($this->returnValue(null)); |
||
70 | |||
71 | $this->loader->find('/foo/bar'); |
||
72 | } |
||
73 | } |
||
74 |