Completed
Push — develop ( f3c189...e92436 )
by Alejandro
08:55
created

SendMailPluginAbstractFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 61
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testCanCreateServiceWithInvalidName() 0 5 1
A testCanCreateServiceWithBaseName() 0 5 1
A testCanCreateServiceWhenConcreteServiceIsNotDefined() 0 10 1
A testCreateServiceWithName() 0 15 1
A createControllerManager() 0 9 1
1
<?php
2
namespace AcMailerTest\Controller\Plugin;
3
4
use AcMailer\Controller\Plugin\Factory\SendMailPluginAbstractFactory;
5
use AcMailer\Service\Factory\MailServiceAbstractFactory;
6
use AcMailer\Service\MailServiceMock;
7
use PHPUnit_Framework_TestCase as TestCase;
8
use Zend\ServiceManager\ServiceManager;
9
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
10
11
/**
12
 * Class SendMailPluginFactoryTest
13
 * @author Alejandro Celaya Alastrué
14
 * @link http://www.alejandrocelaya.com
15
 */
16
class SendMailPluginAbstractFactoryTest extends TestCase
17
{
18
    /**
19
     * @var SendMailPluginAbstractFactory
20
     */
21
    private $factory;
22
23
    public function setUp()
24
    {
25
        $this->factory = new SendMailPluginAbstractFactory();
26
    }
27
28
    public function testCanCreateServiceWithInvalidName()
29
    {
30
        $pm = $this->createControllerManager();
31
        $this->assertFalse($this->factory->canCreateServiceWithName($pm, '', 'foo'));
32
    }
33
34
    public function testCanCreateServiceWithBaseName()
35
    {
36
        $pm = $this->createControllerManager();
37
        $this->assertTrue($this->factory->canCreateServiceWithName($pm, '', 'sendMail'));
38
    }
39
40
    public function testCanCreateServiceWhenConcreteServiceIsNotDefined()
41
    {
42
        $pm = $this->createControllerManager([
43
            'acmailer_options' => [
44
                'concrete' => []
45
            ]
46
        ]);
47
        $this->assertTrue($this->factory->canCreateServiceWithName($pm, '', 'sendMailConcrete'));
48
        $this->assertFalse($this->factory->canCreateServiceWithName($pm, '', 'sendMailInvalid'));
49
    }
50
51
    public function testCreateServiceWithName()
52
    {
53
        $pm = $this->createControllerManager();
54
        $mailServiceName = sprintf(
55
            '%s.%s.%s',
56
            MailServiceAbstractFactory::ACMAILER_PART,
57
            MailServiceAbstractFactory::SPECIFIC_PART,
58
            'concrete'
59
        );
60
        $pm->getServiceLocator()->setService($mailServiceName, new MailServiceMock());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\ServiceManager\ServiceLocatorInterface as the method setService() does only exist in the following implementations of said interface: Zend\Cache\PatternPluginManager, Zend\Cache\Storage\AdapterPluginManager, Zend\Cache\Storage\PluginManager, Zend\Config\ReaderPluginManager, Zend\Config\WriterPluginManager, Zend\Filter\FilterPluginManager, Zend\Form\FormElementMan...lementManagerV2Polyfill, Zend\Form\FormElementMan...lementManagerV3Polyfill, Zend\Hydrator\HydratorPluginManager, Zend\I18n\Translator\LoaderPluginManager, Zend\InputFilter\InputFilterPluginManager, Zend\Mail\Protocol\SmtpPluginManager, Zend\Mvc\Controller\ControllerManager, Zend\Mvc\Controller\PluginManager, Zend\Mvc\Router\RoutePluginManager, Zend\Serializer\AdapterPluginManager, Zend\ServiceManager\AbstractPluginManager, Zend\ServiceManager\ServiceManager, Zend\Validator\ValidatorPluginManager, Zend\View\HelperPluginManager, Zend\View\Helper\Navigation\PluginManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
        $this->assertInstanceOf(
62
            'AcMailer\Controller\Plugin\SendMailPlugin',
63
            $this->factory->createServiceWithName($pm, '', 'sendMailConcrete')
64
        );
65
    }
66
67
    protected function createControllerManager($config = [])
68
    {
69
        $pm = new ControllerPluginManager();
70
        $sm = new ServiceManager();
71
        $sm->setService('Config', $config);
72
        $pm->setServiceLocator($sm);
73
74
        return $pm;
75
    }
76
}
77