CrudApiHelperTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetRelatedFieldForUserId() 0 6 1
A testGetRelatedFieldForOrganisationId() 0 6 1
A testGetModelDisplayNameWithoutAnInstance() 0 10 1
A testAuthorUserModelBinding() 0 9 1
A testSetModelHelper() 0 7 1
1
<?php
2
3
namespace Test\Unit;
4
5
use Test\TestCase;
6
use \Mockery as m;
7
use Taskforcedev\CrudApi\Helpers\CrudApi;
8
9
class CrudApiHelperTest extends TestCase
10
{
11
    public function testGetRelatedFieldForUserId()
12
    {
13
        $crudApi = new CrudApi(['namespace' => null]);
14
        $related_field = $crudApi->getRelatedField('user_id');
0 ignored issues
show
Documentation Bug introduced by
The method getRelatedField does not exist on object<Taskforcedev\CrudApi\Helpers\CrudApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
15
        $this->assertEquals('user', $related_field);
16
    }
17
18
    public function testGetRelatedFieldForOrganisationId()
19
    {
20
        $crudApi = new CrudApi(['namespace' => null]);
21
        $related_field = $crudApi->getRelatedField('organisation_id');
0 ignored issues
show
Documentation Bug introduced by
The method getRelatedField does not exist on object<Taskforcedev\CrudApi\Helpers\CrudApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
22
        $this->assertEquals('organisation', $related_field);
23
    }
24
25
    public function testGetModelDisplayNameWithoutAnInstance()
26
    {
27
        $options = [
28
            'namespace' => null,
29
            'model' => 'User'
30
        ];
31
        $crudApi = new CrudApi($options);
32
        $display = $crudApi->getModelDisplayName();
33
        $this->assertEquals('User', $display);
34
    }
35
36
    public function testAuthorUserModelBinding()
37
    {
38
        $crudApi = new CrudApi(['namespace' => 'Test\\Models\\', 'model' => 'Post']);
39
        $related_field = $crudApi->getRelatedField('author_id');
0 ignored issues
show
Documentation Bug introduced by
The method getRelatedField does not exist on object<Taskforcedev\CrudApi\Helpers\CrudApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
40
        $this->assertEquals('author', $related_field);
41
        $relation = $crudApi->getRelatedModel($related_field);
42
        $class = get_class($relation);
43
        $this->assertEquals('Test\\Models\\User', $class);
44
    }
45
46
    public function testSetModelHelper()
47
    {
48
        $options = ['namespace' => null];
49
        $crudApi = new CrudApi($options);
50
        $crudApi->setModelHelper('test');
0 ignored issues
show
Documentation introduced by
'test' 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...
51
        $this->assertEquals('test', $crudApi->modelHelper);
52
    }
53
}