Conditions | 2 |
Paths | 1 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 |