chadicus /
marvel-api-client
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Chadicus\Marvel\Api\Entities; |
||
| 3 | |||
| 4 | /** |
||
| 5 | * Unit tests for the Price class. |
||
| 6 | * |
||
| 7 | * @coversDefaultClass \Chadicus\Marvel\Api\Entities\Price |
||
| 8 | * @covers ::<protected> |
||
| 9 | */ |
||
| 10 | final class PriceTest extends \PHPUnit_Framework_TestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Verify invalid constructor parameters cause exceptions. |
||
| 14 | * |
||
| 15 | * @param mixed $type The description of the price. |
||
| 16 | * @param mixed $price The price of the price. |
||
| 17 | * |
||
| 18 | * @test |
||
| 19 | * @dataProvider constructorBadData |
||
| 20 | * @expectedException \InvalidArgumentException |
||
| 21 | * |
||
| 22 | * @return void |
||
| 23 | */ |
||
| 24 | public function constructWithInvalidParameters($type, $price) |
||
| 25 | { |
||
| 26 | new Price(['type' => $type, 'price' => $price]); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Data provider for constructWithInvalidParameters. |
||
| 31 | * |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | public function constructorBadData() |
||
| 35 | { |
||
| 36 | return [ |
||
| 37 | 'type is empty' => ['', 'a price'], |
||
| 38 | 'type is whitespace' => ["\t \n", 'a price'], |
||
| 39 | 'type is not a string' => [true, 'a price'], |
||
| 40 | 'price is not a float' => ['a type', false], |
||
| 41 | ]; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Verify valid constructor parameters cause no exceptions. |
||
| 46 | * |
||
| 47 | * @param mixed $type The description of the price. |
||
| 48 | * @param mixed $price The price of the price. |
||
| 49 | * |
||
| 50 | * @test |
||
| 51 | * @dataProvider constructorGoodData |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function constructWithValidParameters($type, $price) |
||
| 56 | { |
||
| 57 | $entity = new Price(['type' => $type, 'price' => $price]); |
||
| 58 | $this->assertSame($type, $entity->getType()); |
||
|
0 ignored issues
–
show
|
|||
| 59 | $this->assertSame($price, $entity->getPrice()); |
||
|
0 ignored issues
–
show
The method
getPrice does not exist on object<Chadicus\Marvel\Api\Entities\Price>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Data provider for constructWithInvalidParameters. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function constructorGoodData() |
|
|
0 ignored issues
–
show
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...
|
|||
| 68 | { |
||
| 69 | return [ |
||
| 70 | 'type is null, price is null' => [null, null], |
||
| 71 | 'type is null, price is float' => [null, 1.0], |
||
| 72 | 'type is string, price is null' => ['a type', null], |
||
| 73 | 'type is string, price is float' => ['a type', 1.0], |
||
| 74 | ]; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Verify basic behavior of fromArray(). |
||
| 79 | * |
||
| 80 | * @test |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | public function fromArray() |
||
| 85 | { |
||
| 86 | $price = Price::fromArray(['type' => 'a type', 'price' => 1.0]); |
||
| 87 | $this->assertSame('a type', $price->getType()); |
||
|
0 ignored issues
–
show
The method
getType does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 88 | $this->assertSame(1.0, $price->getPrice()); |
||
|
0 ignored issues
–
show
The method
getPrice does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Verify fromArray throws when input is invalid. |
||
| 93 | * |
||
| 94 | * @test |
||
| 95 | * @expectedException \Exception |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function fromArrayWithInvalidInput() |
||
| 100 | { |
||
| 101 | Price::fromArray(['type' => 'a type', 'price' => 'price not a float']); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Verify basic behavior of fromArrays(). |
||
| 106 | * |
||
| 107 | * @test |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function fromArrays() |
|
|
0 ignored issues
–
show
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...
|
|||
| 112 | { |
||
| 113 | $prices = Price::fromArrays( |
||
| 114 | [ |
||
| 115 | ['type' => 'a type', 'price' => 1.0], |
||
| 116 | ['type' => 'another type', 'price' => 2.0], |
||
| 117 | ] |
||
| 118 | ); |
||
| 119 | |||
| 120 | $this->assertSame(2, count($prices)); |
||
| 121 | $this->assertSame('a type', $prices[0]->getType()); |
||
|
0 ignored issues
–
show
The method
getType does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 122 | $this->assertSame(1.0, $prices[0]->getPrice()); |
||
|
0 ignored issues
–
show
The method
getPrice does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 123 | $this->assertSame('another type', $prices[1]->getType()); |
||
|
0 ignored issues
–
show
The method
getType does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 124 | $this->assertSame(2.0, $prices[1]->getPrice()); |
||
|
0 ignored issues
–
show
The method
getPrice does not exist on object<Chadicus\Marvel\A...ntities\AbstractEntity>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when 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...
|
|||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Verify fromArrays throws when input is invalid. |
||
| 129 | * |
||
| 130 | * @test |
||
| 131 | * @expectedException \Exception |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function fromArraysWithInvalidInput() |
||
| 136 | { |
||
| 137 | Price::fromArrays( |
||
| 138 | [ |
||
| 139 | ['type' => 'a type', 'price' => 1.0], |
||
| 140 | ['type' => '', 'price' => 2.0], |
||
| 141 | ] |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: