Werkspot /
Enum
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Werkspot\Enum\Tests; |
||
| 6 | |||
| 7 | use PHPUnit\Framework\TestCase; |
||
| 8 | |||
| 9 | final class AbstractEnumTest extends TestCase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @test |
||
| 13 | */ |
||
| 14 | public function enum(): void |
||
| 15 | { |
||
| 16 | $a = TestEnum::get(TestEnum::A); |
||
| 17 | $b = TestEnum::get(TestEnum::B); |
||
| 18 | |||
| 19 | self::assertSame(TestEnum::A, $a->getValue()); |
||
| 20 | self::assertSame(TestEnum::B, $b->getValue()); |
||
| 21 | self::assertSame(TestEnum::A, (string) $a); |
||
| 22 | self::assertSame(TestEnum::B, (string) $b); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @test |
||
| 27 | */ |
||
| 28 | public function null(): void |
||
| 29 | { |
||
| 30 | $nullEnum = TestEnum::get(null); |
||
| 31 | self::assertNull($nullEnum->getValue()); |
||
| 32 | self::assertSame('', (string) $nullEnum); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @test |
||
| 37 | */ |
||
| 38 | public function integer(): void |
||
| 39 | { |
||
| 40 | $integerEnum = TestEnum::get(3); |
||
| 41 | self::assertSame(3, $integerEnum->getValue()); |
||
| 42 | self::assertSame('3', (string) $integerEnum); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @test |
||
| 47 | */ |
||
| 48 | public function isShouldReturnTrueWhenComparingObjectsWithTheSameTypeAndValue(): void |
||
| 49 | { |
||
| 50 | $a = TestEnum::get(TestEnum::A); |
||
| 51 | |||
| 52 | self::assertTrue($a->isA()); |
||
|
1 ignored issue
–
show
|
|||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @test |
||
| 57 | */ |
||
| 58 | public function isShouldReturnFalseWhenComparingObjectsWithTheSameTypeAndDifferentValue(): void |
||
| 59 | { |
||
| 60 | $a = TestEnum::get(TestEnum::A); |
||
| 61 | |||
| 62 | self::assertFalse($a->isB()); |
||
|
1 ignored issue
–
show
The method
isB does not exist on object<Werkspot\Enum\Tests\TestEnum>? 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...
|
|||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @test |
||
| 67 | */ |
||
| 68 | public function equalsShouldReturnTrueWhenComparingObjectsWithTheSameTypeAndValue(): void |
||
| 69 | { |
||
| 70 | $a = TestEnum::get(TestEnum::A); |
||
| 71 | |||
| 72 | self::assertTrue($a->equals(TestEnum::get(TestEnum::A))); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @test |
||
| 77 | */ |
||
| 78 | public function equalsShouldReturnFalseWhenComparingObjectsWithTheSameTypeButDifferentValue(): void |
||
| 79 | { |
||
| 80 | $a = TestEnum::get(TestEnum::A); |
||
| 81 | |||
| 82 | self::assertFalse($a->equals(TestEnum::get(TestEnum::B))); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @test |
||
| 87 | */ |
||
| 88 | public function equalsShouldReturnFalseWhenComparingObjectsWithTheSameValueButDifferentType(): void |
||
| 89 | { |
||
| 90 | $a = TestEnum::get(TestEnum::A); |
||
| 91 | |||
| 92 | self::assertFalse($a->equals(Test2Enum::get(Test2Enum::A))); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @test |
||
| 97 | */ |
||
| 98 | public function equalsShouldReturnTrueWhenComparingObjectsWithDifferentTypeAndValue(): void |
||
| 99 | { |
||
| 100 | $a = TestEnum::get(TestEnum::A); |
||
| 101 | |||
| 102 | self::assertFalse($a->equals(Test2Enum::get(Test2Enum::C))); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @test |
||
| 107 | * |
||
| 108 | * @expectedException \InvalidArgumentException |
||
| 109 | * @expectedExceptionMessage Value [anything] is not matching any valid value of class "TestEnum". Valid values are ['A', 'BEE', 1, 3, NULL, true]. |
||
| 110 | */ |
||
| 111 | public function exceptionMessage(): void |
||
| 112 | { |
||
| 113 | TestEnum::get('anything'); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @test |
||
| 118 | * |
||
| 119 | * @dataProvider getInvertedCaseOptions |
||
| 120 | * @expectedException \InvalidArgumentException |
||
| 121 | * |
||
| 122 | * @param mixed $option |
||
| 123 | */ |
||
| 124 | public function getWithInvertedCaseIsIncorrect($option): void |
||
| 125 | { |
||
| 126 | TestEnum::get($option); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getInvertedCaseOptions() |
||
| 130 | { |
||
| 131 | return [ |
||
| 132 | [strtolower(TestEnum::A)], |
||
| 133 | [strtolower(TestEnum::B)], |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @test |
||
| 139 | * |
||
| 140 | * @expectedException \InvalidArgumentException |
||
| 141 | */ |
||
| 142 | public function getWithStrictEqualMatchThrowsException(): void |
||
| 143 | { |
||
| 144 | TestEnum::get('1'); |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @test |
||
| 150 | */ |
||
| 151 | public function isShouldAllowToCallMethodsBasedOnConstantNames(): void |
||
| 152 | { |
||
| 153 | $enum = TestEnum::nameWithUnderscore(); |
||
| 154 | |||
| 155 | self::assertInstanceOf(TestEnum::class, $enum); |
||
| 156 | self::assertSame(TestEnum::NAME_WITH_UNDERSCORE, $enum->getValue()); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @test |
||
| 161 | * |
||
| 162 | * @expectedException \BadMethodCallException |
||
| 163 | * @expectedExceptionMessage Werkspot\Enum\Tests\TestEnum::isDoesNotExist() does not exist |
||
| 164 | */ |
||
| 165 | public function isShouldThrowAnExceptionWhenWhenCallingAnInvalidMethod(): void |
||
| 166 | { |
||
| 167 | $enum = TestEnum::a(); |
||
| 168 | $enum->isDoesNotExist(); |
||
|
1 ignored issue
–
show
The method
isDoesNotExist does not exist on object<Werkspot\Enum\Tests\TestEnum>? 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...
|
|||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @test |
||
| 173 | * |
||
| 174 | * @expectedException \BadMethodCallException |
||
| 175 | * @expectedExceptionMessage Werkspot\Enum\Tests\TestEnum::doesNotExist() does not exist |
||
| 176 | */ |
||
| 177 | public function shouldThrowAnExceptionWhenWhenCallingAnInvalidStaticMethod(): void |
||
| 178 | { |
||
| 179 | TestEnum::doesNotExist(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @test |
||
| 184 | */ |
||
| 185 | public function getValidOptions(): void |
||
| 186 | { |
||
| 187 | self::assertSame( |
||
| 188 | [ |
||
| 189 | TestEnum::A, |
||
| 190 | TestEnum::B, |
||
| 191 | TestEnum::A1, |
||
| 192 | TestEnum::A3, |
||
| 193 | TestEnum::ANULL, |
||
| 194 | TestEnum::NAME_WITH_UNDERSCORE, |
||
| 195 | ], |
||
| 196 | TestEnum::getValidOptions() |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | } |
||
| 200 |
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: