Failed Conditions
Pull Request — master (#25)
by Chad
02:50
created

ImageTest::constructWithInvalidParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace Chadicus\Marvel\Api\Entities;
3
4
/**
5
 * Unit tests for the Image class.
6
 *
7
 * @coversDefaultClass \Chadicus\Marvel\Api\Entities\Image
8
 * @covers ::<protected>
9
 */
10
final class ImageTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * Verify basic behavior of getPath.
14
     *
15
     * @test
16
     *
17
     * @return void
18
     */
19
    public function getPath()
20
    {
21
        $this->assertSame('a path', (new Image(['path' => 'a path']))->getPath());
0 ignored issues
show
Documentation Bug introduced by
The method getPath does not exist on object<Chadicus\Marvel\Api\Entities\Image>? 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
    }
23
24
    /**
25
     * Verify basic behavior of getExtension.
26
     *
27
     * @test
28
     *
29
     * @return void
30
     */
31
    public function getExtension()
32
    {
33
        $this->assertSame('an extension', (new Image(['extension' => 'an extension']))->getExtension());
0 ignored issues
show
Documentation Bug introduced by
The method getExtension does not exist on object<Chadicus\Marvel\Api\Entities\Image>? 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...
34
    }
35
36
    /**
37
     * Verify basic behavior of getUrl.
38
     *
39
     * @test
40
     * @covers ::getUrl
41
     *
42
     * @return void
43
     */
44
    public function getUrl()
45
    {
46
        $this->assertSame(
47
            'a url/portrait_small.an extension',
48
            (new Image(['path' => 'a url', 'extension' => 'an extension']))->getUrl(ImageVariant::PORTRAIT_SMALL())
49
        );
50
    }
51
52
    /**
53
     * Verify invalid constructor parameters cause exceptions.
54
     *
55
     * @param mixed $path      The full URL (including scheme, domain, and path).
56
     * @param mixed $extension The text identifier for the URL.
57
     *
58
     * @test
59
     * @dataProvider constructorBadData
60
     * @expectedException \InvalidArgumentException
61
     *
62
     * @return void
63
     */
64
    public function constructWithInvalidParameters($path, $extension)
65
    {
66
        new Image(['path' => $path, 'extension' => $extension]);
67
    }
68
69
    /**
70
     * Data provider for constructWithInvalidParameters.
71
     *
72
     * @return array
73
     */
74 View Code Duplication
    public function constructorBadData()
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...
75
    {
76
        return [
77
            'path is not a string' => [true, 'an extension'],
78
            'extension is not a string' => ['a path', false],
79
        ];
80
    }
81
82
    /**
83
     * Verify basic functionality of fromArray().
84
     *
85
     * @test
86
     *
87
     * @return void
88
     */
89
    public function fromArray()
90
    {
91
        $image = Image::fromArray(['path' => 'a path', 'extension' => 'an extension']);
92
        $this->assertSame('a path', $image->getPath());
0 ignored issues
show
Documentation Bug introduced by
The method getPath does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? 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...
93
        $this->assertSame('an extension', $image->getExtension());
0 ignored issues
show
Documentation Bug introduced by
The method getExtension does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? 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...
94
    }
95
96
    /**
97
     * Verify fromArray() throws filter exception.
98
     *
99
     * @test
100
     * @expectedException \Exception
101
     *
102
     * @return void
103
     */
104
    public function fromArrayInvalidPath()
105
    {
106
        $image = Image::fromArray(['path' => 1, 'extension' => 'an extension']);
0 ignored issues
show
Unused Code introduced by
$image is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
    }
108
109
    /**
110
     * Verify basic behavior of fromArrays().
111
     *
112
     * @test
113
     *
114
     * @return void
115
     */
116 View Code Duplication
    public function fromArrays()
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...
117
    {
118
        $images = Image::fromArrays(
119
            [
120
                ['path' => 'a path', 'extension' => 'an extension'],
121
                ['path' => 'another path', 'extension' => 'another extension'],
122
            ]
123
        );
124
125
        $this->assertSame(2, count($images));
126
        $this->assertSame('an extension', $images[0]->getExtension());
0 ignored issues
show
Documentation Bug introduced by
The method getExtension does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? 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...
127
        $this->assertSame('a path', $images[0]->getPath());
0 ignored issues
show
Documentation Bug introduced by
The method getPath does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? 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...
128
        $this->assertSame('another extension', $images[1]->getExtension());
0 ignored issues
show
Documentation Bug introduced by
The method getExtension does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? 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...
129
        $this->assertSame('another path', $images[1]->getPath());
0 ignored issues
show
Documentation Bug introduced by
The method getPath does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? 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...
130
    }
131
132
    /**
133
     * Verify fromArrays throws when input is invalid.
134
     *
135
     * @test
136
     * @expectedException \Exception
137
     *
138
     * @return void
139
     */
140
    public function fromArraysWithInvalidInput()
141
    {
142
        Image::fromArrays(
143
            [
144
                ['path' => 'a path', 'extension' => 'an extension'],
145
                ['path' => 'another path', 'extension' => true],
146
            ]
147
        );
148
    }
149
150
    /**
151
     * Verify behaviour with null values.
152
     *
153
     * @test
154
     *
155
     * @return void
156
     */
157
    public function constructWithNulls()
158
    {
159
        $image = new Image(['path' => null, 'extension' => null]);
160
        $this->assertNull($image->path);
161
        $this->assertNull($image->extension);
162
    }
163
}
164