Passed
Push — master ( 6cecf9...d32758 )
by Davis
44s
created

TestAuthorServices::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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
class TestAuthorServices extends TestCase
26
{
27
    /**
28
     * @LoggerInterface
29
     */
30
    private $logger;
31
32
    /**
33
     * @LoggerInterface
34
     */
35
    private $loggerWillBeCalled;
0 ignored issues
show
Unused Code introduced by
The property $loggerWillBeCalled is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * @RepositoryInterface
39
     */
40
    private $authorRepository;
41
42
    /**
43
     * @UuidInterface
44
     */
45
    private $uuid;
46
47
    /**
48
     * @Author
49
     */
50
    private $author;
51
52
    /**
53
     * @array
54
     */
55
    private $filters;
56
57
    public function setUp()
58
    {
59
        $this->logger = $this->createMock(LoggerInterface::class);
60
        $this->authorRepository = $this->getMockForAbstractClass(AbstractAuthorRepository::class);
61
        $this->uuid = Uuid::uuid4();
62
        $this->author = new Author($this->uuid, 'Davis', '[email protected]', 'Some string', new DateTime());
63
        $this->filters = [];
64
    }
65
66
    public function testCreateAuthorService()
67
    {
68
        $this->authorRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid));
69
        $service = new CreateAuthor($this->authorRepository, $this->author, $this->logger);
70
        $this->assertEquals($this->uuid, $service->run(), 'called once!');
71
    }
72
73 View Code Duplication
    public function testCreateAuthorServiceShouldReturnNullOnFailure()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        $this->logger->expects($this->once())->method('error');
76
        $this->authorRepository->expects($this->once())->method('save')->will($this->throwException(new Exception()));
77
        $service = new CreateAuthor($this->authorRepository, $this->author, $this->logger);
78
        $this->assertEquals(null, $service->run(), 'Test exception');
79
    }
80
81
    public function testEditAuthorService()
82
    {
83
        $this->authorRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid));
84
        $service = new EditAuthor($this->authorRepository, $this->author, $this->logger);
85
        $this->assertEquals($this->uuid, $service->run(), 'called once!');
86
    }
87
88 View Code Duplication
    public function testEditAuthorServiceShouldReturnNullOnFailure()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
89
    {
90
        $this->logger->expects($this->once())->method('error');
91
        $this->authorRepository->expects($this->once())->method('save')->will($this->throwException(new Exception()));
92
        $service = new EditAuthor($this->authorRepository, $this->author, $this->logger);
93
        $this->assertEquals(null, $service->run(), 'Test exception');
94
    }
95
96
    public function testDeleteAuthorService()
97
    {
98
        $this->authorRepository->expects($this->once())->method('delete')->will($this->returnValue(true));
99
        $service = new DeleteAuthor($this->authorRepository, $this->author, $this->logger);
100
        $this->assertEquals(true, $service->run(), 'Success');
101
    }
102
103
    public function testDeleteAuthorServiceReturningFalse()
104
    {
105
        $this->authorRepository->expects($this->once())->method('delete')->will($this->returnValue(false));
106
        $service = new DeleteAuthor($this->authorRepository, $this->author, $this->logger);
107
        $this->assertEquals(false, $service->run(), 'Success');
108
    }
109
110 View Code Duplication
    public function testDeleteAuthorServiceShouldReturnFalseOnFailure()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
111
    {
112
        $this->logger->expects($this->once())->method('error');
113
        $this->authorRepository->expects($this->once())->method('delete')->will($this->throwException(new Exception()));
114
        $service = new DeleteAuthor($this->authorRepository, $this->author, $this->logger);
115
        $this->assertEquals(false, $service->run(), 'Test exception');
116
    }
117
118
    public function testGetAuthorService()
119
    {
120
        $this->authorRepository->expects($this->once())->method('get')->will($this->returnValue($this->author));
121
        $service = new GetAuthor($this->authorRepository, $this->uuid, $this->logger);
122
        $this->assertEquals($this->author, $service->run(), 'Success');
123
    }
124
125 View Code Duplication
    public function testGetAuthorServiceReturningException()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
126
    {
127
        $this->logger->expects($this->once())->method('error');
128
        $this->authorRepository->expects($this->once())->method('get')->will($this->throwException(new Exception()));
129
        $service = new GetAuthor($this->authorRepository, $this->uuid, $this->logger);
130
        $this->assertEquals(false, $service->run(), 'Exception');
131
    }
132
133
    public function testGetAuthorServiceReturningFalse()
134
    {
135
        $this->authorRepository->expects($this->once())->method('get')->will($this->returnValue(false));
136
        $service = new GetAuthor($this->authorRepository, $this->uuid, $this->logger);
137
        $this->assertEquals(false, $service->run(), 'False');
138
    }
139
140
    public function testGetAuthorsListService()
141
    {
142
        $this->authorRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->returnValue([$this->author]));
143
        $service = new ListAuthors($this->authorRepository, $this->filters, $this->logger);
144
        $this->assertEquals([$this->author], $service->run(), 'Success');
145
    }
146
147 View Code Duplication
    public function testGetAuthorsListServiceException()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
148
    {
149
        $this->logger->expects($this->once())->method('error');
150
        $this->authorRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->throwException(new Exception()));
151
        $service = new ListAuthors($this->authorRepository, $this->filters, $this->logger);
152
        $this->assertEquals([], $service->run(), 'Exception');
153
    }
154
}
155