testInstanceMethodReturnsAnInstanceOfAModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Test\Unit;
4
5
use Test\TestCase;
6
use \Mockery as m;
7
use Taskforcedev\CrudApi\Helpers\CrudApi;
8
use Taskforcedev\CrudApi\Helpers\Model as ModelHelper;
9
10
11
class ModelHelperTest extends TestCase
12
{
13
    public function testGetModelWithoutNamespaceReturnsFalse()
14
    {
15
        $crudApi = new CrudApi(['namespace' => null]);
16
17
        $this->assertFalse($crudApi->getModel());
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Taskforcedev\CrudApi\Helpers\CrudApi. Did you maybe mean getModelDisplayName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
18
    }
19
20 View Code Duplication
    public function testGetModelUser()
0 ignored issues
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...
21
    {
22
        $namespace = 'Test\\Models\\';
23
24
        $options = [
25
            'namespace' => $namespace,
26
            'model' => 'User',
27
        ];
28
29
        $crudApi = new CrudApi($options);
30
31
        $fqModel = $crudApi->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Taskforcedev\CrudApi\Helpers\CrudApi. Did you maybe mean getModelDisplayName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
33
        $this->assertEquals($namespace . 'User', $fqModel);
34
    }
35
36 View Code Duplication
    public function testUserGetModelCanBeInstantiated()
0 ignored issues
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...
37
    {
38
        $namespace = 'Test\\Models\\';
39
40
        $options = [
41
            'namespace' => $namespace,
42
            'model' => 'User',
43
        ];
44
45
        $crudApi = new CrudApi($options);
46
47
        $fqModel = $crudApi->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Taskforcedev\CrudApi\Helpers\CrudApi. Did you maybe mean getModelDisplayName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
        $model = new $fqModel;
49
        $this->assertEquals('Test\\Models\\User', get_class($model));
50
    }
51
52
    public function testAdditionalModelCanBeInstantiated()
53
    {
54
        $namespace = 'Test\\Models\\';
55
56
        $options = [
57
            'namespace' => $namespace,
58
            'model' => 'AdditionalModel',
59
        ];
60
61
        $crudApi = new CrudApi($options);
62
63
        $modelHelper = m::mock('\Taskforcedev\CrudApi\Helpers\Model[getAdditionalNamespaces]', [$crudApi]);
64
        $modelHelper->shouldReceive('getAdditionalNamespaces')->once()->andReturn(['Test\\AnotherNamespace\\']);
65
        $crudApi->modelHelper = $modelHelper;
66
67
        $fqModel = $crudApi->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Taskforcedev\CrudApi\Helpers\CrudApi. Did you maybe mean getModelDisplayName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
        $model = new $fqModel;
69
        $this->assertEquals('Test\\AnotherNamespace\\AdditionalModel', get_class($model));
70
    }
71
72 View Code Duplication
    public function testInstanceMethodReturnsTheSpecifiedInstance()
0 ignored issues
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...
73
    {
74
        $namespace = 'Test\\Models\\';
75
76
        $options = [
77
            'namespace' => $namespace,
78
            'model' => 'AdditionalModel',
79
        ];
80
81
        $crudApi = new CrudApi($options);
82
        $modelHelper = new ModelHelper($crudApi);
83
        $testInstance = 'testing';
84
        $crudApi->setInstance($testInstance);
0 ignored issues
show
Documentation introduced by
$testInstance is of type string, but the function expects a object.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
        $instance = $modelHelper->instance();
86
        $this->assertEquals($testInstance, $instance, 'Given an instance, modelHelper instance() should return that instance.');
87
    }
88
89
    public function testInstanceMethodReturnsAnInstanceOfAModel()
90
    {
91
        $options = [
92
            'namespace' => 'Test\\AnotherNamespace\\',
93
            'model' => 'AdditionalModel',
94
        ];
95
96
        $crudApi = new CrudApi($options);
97
        $modelHelper = new ModelHelper($crudApi);
98
        $instance = $modelHelper->instance();
99
        $class = get_class($instance);
100
        $this->assertEquals('Test\\AnotherNamespace\\AdditionalModel', $class, 'Given an instance, modelHelper instance() should return that instance.');
101
    }
102
103
    public function testGetModelReturnsFalseIfNoModelExists()
104
    {
105
        $options = [
106
            'namespace' => 'Test\\AnotherNamespace\\',
107
            'model' => 'NonExistantModel',
108
        ];
109
110
        $crudApi = new CrudApi($options);
111
        $modelHelper = m::mock('\Taskforcedev\CrudApi\Helpers\Model[getAdditionalNamespaces]', [$crudApi]);
112
        $modelHelper->shouldReceive('getAdditionalNamespaces')->once()->andReturn([]);
113
        $crudApi->modelHelper = $modelHelper;
114
        $output = $crudApi->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Taskforcedev\CrudApi\Helpers\CrudApi. Did you maybe mean getModelDisplayName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
115
        $this->assertFalse($output, 'getModel should return false if asked for non-existant model.');
116
    }
117
}