Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class MailServiceTest extends TestCase |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var \AcMailerTest\Mail\Transport\MockTransport |
||
| 27 | */ |
||
| 28 | private $transport; |
||
| 29 | /** |
||
| 30 | * @var \AcMailer\Service\MailService |
||
| 31 | */ |
||
| 32 | private $mailService; |
||
| 33 | |||
| 34 | public function setUp() |
||
| 35 | { |
||
| 36 | $this->transport = new MockTransport(); |
||
| 37 | $config = include __DIR__ . '/../../config/module.config.php'; |
||
| 38 | $renderer = new PhpRenderer(); |
||
| 39 | $renderer->setResolver(new TemplatePathStack($config['view_manager']['template_path_stack'])); |
||
| 40 | $this->mailService = new MailService(new Message(), $this->transport, $renderer); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testMimePartBodyCasting() |
||
| 57 | |||
| 58 | public function testHtmlBodyCasting() |
||
| 59 | { |
||
| 60 | $this->mailService->setBody('<div>Html body</div>'); |
||
| 61 | $this->assertTrue($this->mailService->getMessage()->getBody() instanceof Mime\Message); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function testStringBodyCasting() |
||
| 70 | |||
| 71 | public function testMimeMessageBodyRemainsUnchanged() |
||
| 81 | |||
| 82 | public function testCharsetIsRespectedWhenSettingHtmlStringBody() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @expectedException InvalidArgumentException |
||
| 98 | */ |
||
| 99 | public function testInvalidBodyThrowsException() |
||
| 100 | { |
||
| 101 | $this->mailService->setBody(new \stdClass()); |
||
|
|
|||
| 102 | } |
||
| 103 | |||
| 104 | public function testSetSubject() |
||
| 105 | { |
||
| 106 | $expected = 'This is the subject'; |
||
| 107 | |||
| 108 | $this->assertEquals($this->mailService, $this->mailService->setSubject($expected)); |
||
| 109 | $this->assertEquals($expected, $this->mailService->getMessage()->getSubject()); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function testSuccessfulSending() |
||
| 113 | { |
||
| 114 | $result = $this->mailService->send(); |
||
| 115 | |||
| 116 | $this->assertTrue($result->isValid()); |
||
| 117 | $this->assertEquals(MailResult::DEFAULT_MESSAGE, $result->getMessage()); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testSendingWithError() |
||
| 121 | { |
||
| 122 | $this->transport->setForceError(true); |
||
| 123 | $result = $this->mailService->send(); |
||
| 124 | |||
| 125 | $this->assertFalse($result->isValid()); |
||
| 126 | $this->assertEquals(MockTransport::ERROR_MESSAGE, $result->getMessage()); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @expectedException \Exception |
||
| 131 | */ |
||
| 132 | public function testWithUncatchedException() |
||
| 133 | { |
||
| 134 | $this->transport->setForceError(true, new \Exception()); |
||
| 135 | $this->mailService->send(); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function testZendMailExceptionsAreNotRethrown() |
||
| 152 | |||
| 153 | public function testSetTransport() |
||
| 154 | { |
||
| 155 | $this->assertSame($this->transport, $this->mailService->getTransport()); |
||
| 156 | $anotherTransport = new MockTransport(); |
||
| 157 | $this->assertSame($this->mailService, $this->mailService->setTransport($anotherTransport)); |
||
| 158 | $this->assertSame($anotherTransport, $this->mailService->getTransport()); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function testSetRenderer() |
||
| 162 | { |
||
| 163 | $this->assertInstanceOf('Zend\View\Renderer\PhpRenderer', $this->mailService->getRenderer()); |
||
| 164 | $anotherRenderer = new PhpRenderer(); |
||
| 165 | $this->assertSame($this->mailService, $this->mailService->setRenderer($anotherRenderer)); |
||
| 166 | $this->assertSame($anotherRenderer, $this->mailService->getRenderer()); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function testSuccesfulMailEvent() |
||
| 170 | { |
||
| 171 | $mailListener = new MailListenerMock(); |
||
| 172 | $this->mailService->attachMailListener($mailListener); |
||
| 173 | $result = $this->mailService->send(); |
||
| 174 | |||
| 175 | $this->assertTrue($result->isValid()); |
||
| 176 | $this->assertTrue($mailListener->isOnPreSendCalled()); |
||
| 177 | $this->assertTrue($mailListener->isOnPostSendCalled()); |
||
| 178 | $this->assertFalse($mailListener->isOnSendErrorCalled()); |
||
| 179 | } |
||
| 180 | |||
| 181 | View Code Duplication | public function testMailEventWithError() |
|
| 182 | { |
||
| 183 | $mailListener = new MailListenerMock(); |
||
| 184 | $this->transport->setForceError(true); |
||
| 185 | $this->mailService->attachMailListener($mailListener); |
||
| 186 | $result = $this->mailService->send(); |
||
| 187 | |||
| 188 | $this->assertFalse($result->isValid()); |
||
| 189 | $this->assertTrue($mailListener->isOnPreSendCalled()); |
||
| 190 | $this->assertFalse($mailListener->isOnPostSendCalled()); |
||
| 191 | $this->assertTrue($mailListener->isOnSendErrorCalled()); |
||
| 192 | } |
||
| 193 | |||
| 194 | View Code Duplication | public function testDetachedMailListenerIsNotTriggered() |
|
| 195 | { |
||
| 196 | $mailListener = new MailListenerMock(); |
||
| 197 | $this->mailService->attachMailListener($mailListener); |
||
| 198 | $this->mailService->detachMailListener($mailListener); |
||
| 199 | $result = $this->mailService->send(); |
||
| 200 | |||
| 201 | $this->assertTrue($result->isValid()); |
||
| 202 | $this->assertFalse($mailListener->isOnPreSendCalled()); |
||
| 203 | $this->assertFalse($mailListener->isOnPostSendCalled()); |
||
| 204 | $this->assertFalse($mailListener->isOnSendErrorCalled()); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function testValidTemplateMakesBodyToBeMimeMessage() |
||
| 208 | { |
||
| 209 | $resolver = new TemplatePathStack(); |
||
| 210 | $resolver->addPath(__DIR__ . '/../../view'); |
||
| 211 | $this->mailService->getRenderer()->setResolver($resolver); |
||
| 212 | $this->mailService->setTemplate('ac-mailer/mail-templates/mail.phtml'); |
||
| 213 | |||
| 214 | $this->assertInstanceOf('Zend\Mime\Message', $this->mailService->getMessage()->getBody()); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @expectedException \Zend\View\Exception\RuntimeException |
||
| 219 | */ |
||
| 220 | public function testInvalidTemplateThrowsException() |
||
| 221 | { |
||
| 222 | $this->mailService->setTemplate('foo/bar'); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function testAttachmentsTotal() |
||
| 241 | |||
| 242 | public function testAttachmentsAreAddedAsMimeParts() |
||
| 263 | |||
| 264 | public function testStringBypassedBodyIsWrappedIntoMimePartWithAttachments() |
||
| 281 | |||
| 282 | public function testWithDefaultLayout() |
||
| 294 | } |
||
| 295 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: