Passed
Push — master ( 1c3995...861401 )
by Alex
48s
created

TDataServicesTypeTest::testIsOKWhenSchemaNotOk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 12
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\ODataMetadata\Tests\v3\edmx;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edmx\TDataServicesType;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\Schema;
7
use AlgoWeb\ODataMetadata\Tests\TestCase;
8
use Mockery as m;
9
10
class TDataServicesTypeTest extends TestCase
11
{
12
    public function testServiceVersionGreaterThanMaxVersionWithIntegerVersions()
13
    {
14
        $expected = "Data service version cannot be greater than maximum service version";
15
        $actual = null;
16
17
        try {
18
            new TDataServicesType('3.0', '4.0');
19
        } catch (\InvalidArgumentException $e) {
20
            $actual = $e->getMessage();
21
        }
22
        $this->assertEquals($expected, $actual);
23
    }
24
25
    public function testServiceVersionGreaterThanMaxVersionWithNonIntegerVersions()
26
    {
27
        $expected = "Data service version cannot be greater than maximum service version";
28
        $actual = null;
29
30
        try {
31
            new TDataServicesType('3.1', '3.4');
32
        } catch (\InvalidArgumentException $e) {
33
            $actual = $e->getMessage();
34
        }
35
        $this->assertEquals($expected, $actual);
36
    }
37
38
    public function testMaxServiceVersionNotNumeric()
39
    {
40
        $expected = "Maximum service version must be numeric";
41
        $actual = null;
42
43
        try {
44
            new TDataServicesType(new \DateTime(), '3.4');
1 ignored issue
show
Documentation introduced by
new \DateTime() is of type object<DateTime>, but the function expects a string.

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...
45
        } catch (\InvalidArgumentException $e) {
46
            $actual = $e->getMessage();
47
        }
48
        $this->assertEquals($expected, $actual);
49
    }
50
51
    public function testDataServiceVersionNotNumeric()
52
    {
53
        $expected = "Data service version must be numeric";
54
        $actual = null;
55
56
        try {
57
            new TDataServicesType('3.4', new \DateTime());
1 ignored issue
show
Documentation introduced by
new \DateTime() is of type object<DateTime>, but the function expects a string.

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...
58
        } catch (\InvalidArgumentException $e) {
59
            $actual = $e->getMessage();
60
        }
61
        $this->assertEquals($expected, $actual);
62
    }
63
64
    public function testMaxDataServiceVersionOutOfRange()
65
    {
66
        $expected = "Maximum data service version must be 3.0 or 4.0";
67
        $actual = null;
68
69
        try {
70
            new TDataServicesType('2.0', '2.0');
71
        } catch (\InvalidArgumentException $e) {
72
            $actual = $e->getMessage();
73
        }
74
        $this->assertEquals($expected, $actual);
75
    }
76
77
    public function testDataServiceVersionOutOfRange()
78
    {
79
        $expected = "Data service version must be 1.0, 2.0, 3.0 or 4.0";
80
        $actual = null;
81
82
        try {
83
            new TDataServicesType('3.0', '0.5');
84
        } catch (\InvalidArgumentException $e) {
85
            $actual = $e->getMessage();
86
        }
87
        $this->assertEquals($expected, $actual);
88
    }
89
90
    public function testShouldNotBeAbleToSetEmptySchemaArray()
91
    {
92
        $expected = "Data services array not a valid array";
93
        $actual = null;
94
95
        $foo = new TDataServicesType();
96
97
        try {
98
            $foo->setSchema([]);
99
        } catch (\InvalidArgumentException $e) {
100
            $actual = $e->getMessage();
101
        }
102
        $this->assertEquals($expected, $actual);
103
    }
104
105 View Code Duplication
    public function testTryToAddBadSchema()
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...
106
    {
107
        $expected = "";
108
        $actual = null;
109
110
        $schema = m::mock(Schema::class);
111
        $schema->shouldReceive('isOK')->andReturn(false)->once();
1 ignored issue
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
112
113
        $foo = new TDataServicesType();
114
        try {
115
            $foo->addToSchema($schema);
116
        } catch (\InvalidArgumentException $e) {
117
            $actual = $e->getMessage();
118
        }
119
        $this->assertEquals($expected, $actual);
120
    }
121
122
123
    public function testCheckSchemaExists()
124
    {
125
        $foo = new TDataServicesType();
126
        $this->assertFalse($foo->issetSchema(1));
127
    }
128
129
    public function testUnsetMissingSchema()
130
    {
131
        $foo = new TDataServicesType();
132
        $foo->unsetSchema(1);
133
        $this->assertTrue(true);
134
    }
135
136
    public function testSetHigherDataThanDefaultMaxServiceVersionNotOK()
137
    {
138
        $expected = "Data service version cannot be greater than maximum service version";
139
        $actual = null;
140
141
        $foo = new TDataServicesType();
142
        $foo->setDataServiceVersion('4.0');
143
        $this->assertFalse($foo->isOK($actual));
144
        $this->assertEquals($expected, $actual);
145
    }
146
147 View Code Duplication
    public function testIsOKWhenSchemaNotOk()
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...
148
    {
149
        $expected = "";
150
        $actual = null;
151
152
        $schema = m::mock(Schema::class);
153
        $schema->shouldReceive('isOK')->andReturn(true, false)->twice();
1 ignored issue
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
154
        $foo = new TDataServicesType();
155
        $foo->addToSchema($schema);
156
        $this->assertFalse($foo->isOK($actual));
157
        $this->assertEquals($expected, $actual);
158
    }
159
}
160