ModuleIntegrationTest::testCanLoadModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * @license See the file LICENSE for copying permission.
4
 */
5
6
namespace Thorr\Persistence\Test;
7
8
use PHPUnit_Framework_TestCase as TestCase;
9
use Thorr\Persistence;
10
use Thorr\Persistence\DataMapper;
11
use Zend\InputFilter\InputFilter;
12
use Zend\Mvc\Application;
13
use Zend\ServiceManager\ServiceManager;
14
15
class ModuleIntegrationTest extends TestCase
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $appConfig;
21
22
    protected function setUp()
23
    {
24
        $this->serviceManager = new ServiceManager();
0 ignored issues
show
Bug introduced by
The property serviceManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->appConfig      = [
26
            'modules' => [
27
                'Thorr\Persistence',
28
            ],
29
            'module_listener_options' => [],
30
        ];
31
    }
32
33
    public function testCanLoadModule()
34
    {
35
        $app           = Application::init($this->appConfig);
36
        $loadedModules = $app->getServiceManager()->get('ModuleManager')->getLoadedModules();
37
        $this->assertArrayHasKey('Thorr\Persistence', $loadedModules);
38
        $this->assertInstanceOf(Persistence\Module::class, $loadedModules['Thorr\Persistence']);
39
    }
40
41
    public function testServicesAreRegistered()
42
    {
43
        $app            = Application::init($this->appConfig);
44
        $serviceManager = $app->getServiceManager();
45
46
        $this->assertTrue($serviceManager->has('DataMapperManager'));
47
        $this->assertTrue($serviceManager->has(DataMapper\Manager\DataMapperManager::class));
48
49
        $dataMapperManager = $serviceManager->get(DataMapper\Manager\DataMapperManager::class);
50
        $this->assertInstanceOf(DataMapper\Manager\DataMapperManager::class, $dataMapperManager);
51
    }
52
53
    public function testValidatorsAreRegistered()
54
    {
55
        $app              = Application::init($this->appConfig);
56
        $validatorManager = $app->getServiceManager()->get('ValidatorManager');
57
58
        $this->assertTrue($validatorManager->has(Persistence\Validator\EntityExistsValidator::class));
59
        $this->assertTrue($validatorManager->has(Persistence\Validator\EntityNotExistsValidator::class));
60
    }
61
62
    public function testValidatorConfigCanBeInitializedByZendInputFilterFactory()
63
    {
64
        $fooMapper = $this->getMock(DataMapper\EntityFinderInterface::class);
65
        $fooMapper->expects($this->any())->method('getEntityClass')->willReturn(Asset\Entity::class);
66
        $dmm = new DataMapper\Manager\DataMapperManager(
67
            new  DataMapper\Manager\DataMapperManagerConfig(
68
                [
69
                    'entity_data_mapper_map' => [
70
                        Asset\Entity::class => 'FooMapper',
71
                    ],
72
                    'services' => [
73
                        'FooMapper' => $fooMapper,
74
                    ],
75
                ]
76
            )
77
        );
78
79
        $app = Application::init($this->appConfig);
80
        $sm  = $app->getServiceManager();
81
        $sm->setAllowOverride(true);
82
        $sm->setService(DataMapper\Manager\DataMapperManager::class, $dmm);
83
84
        $inputFilter = new InputFilter();
85
        $app->getServiceManager()->get('InputFilterManager')->populateFactory($inputFilter);
86
87
        $inputFilter->add([
88
            'name'       => 'test',
89
            'validators' => [
90
                [
91
                    'name'    => Persistence\Validator\EntityExistsValidator::class,
92
                    'options' => [
93
                        'entity_class' => Asset\Entity::class,
94
                    ],
95
                ],
96
                [
97
                    'name'    => Persistence\Validator\EntityNotExistsValidator::class,
98
                    'options' => [
99
                        'entity_class' => Asset\Entity::class,
100
                    ],
101
                ],
102
            ],
103
        ]);
104
105
        $this->assertInstanceOf(
106
            Persistence\Validator\EntityExistsValidator::class,
107
            $inputFilter->get('test')->getValidatorChain()->getValidators()[0]['instance']
0 ignored issues
show
Bug introduced by
The method getValidatorChain does only exist in Zend\InputFilter\InputInterface, but not in Zend\InputFilter\InputFilterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
108
        );
109
110
        $this->assertInstanceOf(
111
            Persistence\Validator\EntityNotExistsValidator::class,
112
            $inputFilter->get('test')->getValidatorChain()->getValidators()[1]['instance']
113
        );
114
    }
115
}
116