These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: davis |
||
5 | * Date: 7/23/17 |
||
6 | * Time: 2:12 PM |
||
7 | */ |
||
8 | |||
9 | namespace DavisPeixoto\BlogCore\Tests\Services; |
||
10 | |||
11 | use PHPUnit\Framework\TestCase; |
||
12 | use DavisPeixoto\BlogCore\Entity\Author; |
||
13 | use DavisPeixoto\BlogCore\Service\CreateAuthor; |
||
14 | use DavisPeixoto\BlogCore\Service\EditAuthor; |
||
15 | use DavisPeixoto\BlogCore\Service\DeleteAuthor; |
||
16 | use DavisPeixoto\BlogCore\Service\GetAuthor; |
||
17 | use DavisPeixoto\BlogCore\Service\ListAuthors; |
||
18 | use DavisPeixoto\BlogCore\Repository\AbstractAuthorRepository; |
||
19 | use Ramsey\Uuid\Uuid; |
||
20 | use Ramsey\Uuid\UuidInterface; |
||
21 | use Psr\Log\LoggerInterface; |
||
22 | use DateTime; |
||
23 | use Exception; |
||
24 | |||
25 | View Code Duplication | class TestAuthorServices extends TestCase |
|
0 ignored issues
–
show
|
|||
26 | { |
||
27 | /** |
||
28 | * @LoggerInterface |
||
29 | */ |
||
30 | private $logger; |
||
31 | |||
32 | /** |
||
33 | * @RepositoryInterface |
||
34 | */ |
||
35 | private $authorRepository; |
||
36 | |||
37 | /** |
||
38 | * @UuidInterface |
||
39 | */ |
||
40 | private $uuid; |
||
41 | |||
42 | /** |
||
43 | * @Author |
||
44 | */ |
||
45 | private $author; |
||
46 | |||
47 | /** |
||
48 | * @array |
||
49 | */ |
||
50 | private $filters; |
||
51 | |||
52 | public function setUp() |
||
53 | { |
||
54 | $this->logger = $this->createMock(LoggerInterface::class); |
||
55 | $this->authorRepository = $this->getMockForAbstractClass(AbstractAuthorRepository::class); |
||
56 | $this->uuid = Uuid::uuid4(); |
||
57 | $this->author = new Author($this->uuid, 'Davis', '[email protected]', 'Some string', new DateTime()); |
||
58 | $this->filters = []; |
||
59 | } |
||
60 | |||
61 | public function testCreateAuthorService() |
||
62 | { |
||
63 | $this->authorRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid)); |
||
64 | $service = new CreateAuthor($this->authorRepository, $this->author, $this->logger); |
||
65 | $this->assertEquals($this->uuid, $service->run(), 'called once!'); |
||
66 | } |
||
67 | |||
68 | public function testCreateAuthorServiceShouldReturnNullOnFailure() |
||
69 | { |
||
70 | $this->logger->expects($this->once())->method('error'); |
||
71 | $this->authorRepository->expects($this->once())->method('save')->will($this->throwException(new Exception())); |
||
72 | $service = new CreateAuthor($this->authorRepository, $this->author, $this->logger); |
||
73 | $this->assertEquals(null, $service->run(), 'Test exception'); |
||
74 | } |
||
75 | |||
76 | public function testEditAuthorService() |
||
77 | { |
||
78 | $this->authorRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid)); |
||
79 | $service = new EditAuthor($this->authorRepository, $this->author, $this->logger); |
||
80 | $this->assertEquals($this->uuid, $service->run(), 'called once!'); |
||
81 | } |
||
82 | |||
83 | public function testEditAuthorServiceShouldReturnNullOnFailure() |
||
84 | { |
||
85 | $this->logger->expects($this->once())->method('error'); |
||
86 | $this->authorRepository->expects($this->once())->method('save')->will($this->throwException(new Exception())); |
||
87 | $service = new EditAuthor($this->authorRepository, $this->author, $this->logger); |
||
88 | $this->assertEquals(null, $service->run(), 'Test exception'); |
||
89 | } |
||
90 | |||
91 | public function testDeleteAuthorService() |
||
92 | { |
||
93 | $this->authorRepository->expects($this->once())->method('delete')->will($this->returnValue(true)); |
||
94 | $service = new DeleteAuthor($this->authorRepository, $this->author, $this->logger); |
||
95 | $this->assertEquals(true, $service->run(), 'Success'); |
||
96 | } |
||
97 | |||
98 | public function testDeleteAuthorServiceReturningFalse() |
||
99 | { |
||
100 | $this->authorRepository->expects($this->once())->method('delete')->will($this->returnValue(false)); |
||
101 | $service = new DeleteAuthor($this->authorRepository, $this->author, $this->logger); |
||
102 | $this->assertEquals(false, $service->run(), 'Success'); |
||
103 | } |
||
104 | |||
105 | public function testDeleteAuthorServiceShouldReturnFalseOnFailure() |
||
106 | { |
||
107 | $this->logger->expects($this->once())->method('error'); |
||
108 | $this->authorRepository->expects($this->once())->method('delete')->will($this->throwException(new Exception())); |
||
109 | $service = new DeleteAuthor($this->authorRepository, $this->author, $this->logger); |
||
110 | $this->assertEquals(false, $service->run(), 'Test exception'); |
||
111 | } |
||
112 | |||
113 | public function testGetAuthorService() |
||
114 | { |
||
115 | $this->authorRepository->expects($this->once())->method('get')->will($this->returnValue($this->author)); |
||
116 | $service = new GetAuthor($this->authorRepository, $this->uuid, $this->logger); |
||
117 | $this->assertEquals($this->author, $service->run(), 'Success'); |
||
118 | } |
||
119 | |||
120 | public function testGetAuthorServiceReturningException() |
||
121 | { |
||
122 | $this->logger->expects($this->once())->method('error'); |
||
123 | $this->authorRepository->expects($this->once())->method('get')->will($this->throwException(new Exception())); |
||
124 | $service = new GetAuthor($this->authorRepository, $this->uuid, $this->logger); |
||
125 | $this->assertEquals(false, $service->run(), 'Exception'); |
||
126 | } |
||
127 | |||
128 | public function testGetAuthorServiceReturningFalse() |
||
129 | { |
||
130 | $this->authorRepository->expects($this->once())->method('get')->will($this->returnValue(false)); |
||
131 | $service = new GetAuthor($this->authorRepository, $this->uuid, $this->logger); |
||
132 | $this->assertEquals(false, $service->run(), 'False'); |
||
133 | } |
||
134 | |||
135 | public function testGetAuthorsListService() |
||
136 | { |
||
137 | $this->authorRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->returnValue([$this->author])); |
||
138 | $service = new ListAuthors($this->authorRepository, $this->filters, $this->logger); |
||
139 | $this->assertEquals([$this->author], $service->run(), 'Success'); |
||
140 | } |
||
141 | |||
142 | public function testGetAuthorsListServiceException() |
||
143 | { |
||
144 | $this->logger->expects($this->once())->method('error'); |
||
145 | $this->authorRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->throwException(new Exception())); |
||
146 | $service = new ListAuthors($this->authorRepository, $this->filters, $this->logger); |
||
147 | $this->assertEquals([], $service->run(), 'Exception'); |
||
148 | } |
||
149 | } |
||
150 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.