FileToObjectTest::testGetObjectFromFileName()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 2
eloc 31
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\Tests\File;
12
13
use Doctrine\ORM\EntityManager;
14
15
use IrishDan\ResponsiveImageBundle\File\FileToObject;
16
use IrishDan\ResponsiveImageBundle\ImageEntityNameResolver;
17
use IrishDan\ResponsiveImageBundle\ResponsiveImageInterface;
18
use IrishDan\ResponsiveImageBundle\ResponsiveImageRepositoryInterface;
19
use IrishDan\ResponsiveImageBundle\Tests\Entity\TestImage;
20
21
class FileToObjectTest extends \PHPUnit_Framework_TestCase
22
{
23
    public function testGetObjectFromFileName()
24
    {
25
        // Mock the repository so it returns the mock of the Image repository.
26
        $imageRepository = $this->getMockBuilder(ResponsiveImageRepositoryInterface::class)
27
                                ->disableOriginalConstructor()
28
                                ->getMock();
29
30
        $imageRepository->expects($this->any())
31
                        ->method('findImageFromFilename')
32
                        ->will(
33
                            $this->returnCallback(
34
                                function ($filename) {
35
                                    $image = new TestImage();
36
                                    if ($filename === $image->getPath()) {
37
                                        return $image;
38
                                    }
39
                                    else {
40
                                        return null;
41
                                    }
42
                                }
43
                            )
44
                        );
45
46
        // Mock the EntityManager to return the mock of the repository
47
        $entityManager = $this->getMockBuilder(EntityManager::class)
48
                              ->disableOriginalConstructor()
49
                              ->getMock();
50
51
        $entityManager->expects($this->any())
52
                      ->method('getRepository')
53
                      ->will($this->returnValue($imageRepository));
54
55
        // The mock the ImageEntityName
56
        $nameResolver = $this->getMockBuilder(ImageEntityNameResolver::class)
57
                             ->disableOriginalConstructor()
58
                             ->getMock();
59
60
        $nameResolver->expects($this->once())
61
                     ->method('getClassName')
62
                     ->will($this->returnValue('ResponsiveImage'));
63
64
        $fileToObject = new FileToObject($entityManager, $nameResolver);
65
66
        // test with non-existing filename.
67
        $image = $fileToObject->getObjectFromFilename('not-here.jpg');
68
        $this->assertEmpty($image);
69
70
        // Test with existing image.
71
        $image = $fileToObject->getObjectFromFilename('dummy.jpg');
72
        $this->assertInstanceOf(ResponsiveImageInterface::class, $image);
73
    }
74
}
75