Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3
- nightly

matrix:
Expand All @@ -21,7 +22,6 @@ before_script:

script:
- vendor/bin/phpunit
- vendor/bin/phpcs --standard=PSR2 ./src/

# Use Travis' new container-based infrastructure.
# See http://docs.travis-ci.com/user/migrating-from-legacy/#How-can-I-use-container-based-infrastructure%3F
Expand Down
4 changes: 2 additions & 2 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class Enum implements \JsonSerializable
*
* @var array
*/
protected static $cache = array();
protected static $cache = [];

/**
* Creates a new value of some type
Expand Down Expand Up @@ -150,7 +150,7 @@ public static function isValidKey($key)
{
$array = static::toArray();

return isset($array[$key]);
return isset($array[$key]) || \array_key_exists($key, $array);
}

/**
Expand Down
38 changes: 22 additions & 16 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,21 @@ public function testIsValid($value, $isValid)

public function isValidProvider()
{
return array(
return [
/**
* Valid values
*/
array('foo', true),
array(42, true),
array(null, true),
array(0, true),
array('', true),
array(false, true),
['foo', true],
[42, true],
[null, true],
[0, true],
['', true],
[false, true],
/**
* Invalid values
*/
array('baz', false)
);
['baz', false]
];
}

/**
Expand All @@ -190,6 +190,7 @@ public function testIsValidKey()
{
$this->assertTrue(EnumFixture::isValidKey('FOO'));
$this->assertFalse(EnumFixture::isValidKey('BAZ'));
$this->assertTrue(EnumFixture::isValidKey('PROBLEMATIC_NULL'));
}

/**
Expand Down Expand Up @@ -258,13 +259,13 @@ public function testEqualsConflictValues()
*/
public function testJsonSerialize()
{
$this->assertJsonStringEqualsJsonString('"foo"', json_encode(new EnumFixture(EnumFixture::FOO)));
$this->assertJsonStringEqualsJsonString('"bar"', json_encode(new EnumFixture(EnumFixture::BAR)));
$this->assertJsonStringEqualsJsonString('42', json_encode(new EnumFixture(EnumFixture::NUMBER)));
$this->assertJsonStringEqualsJsonString('0', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER)));
$this->assertJsonStringEqualsJsonString('null', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NULL)));
$this->assertJsonStringEqualsJsonString('""', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING)));
$this->assertJsonStringEqualsJsonString('false', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE)));
$this->assertJsonEqualsJson('"foo"', json_encode(new EnumFixture(EnumFixture::FOO)));
$this->assertJsonEqualsJson('"bar"', json_encode(new EnumFixture(EnumFixture::BAR)));
$this->assertJsonEqualsJson('42', json_encode(new EnumFixture(EnumFixture::NUMBER)));
$this->assertJsonEqualsJson('0', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER)));
$this->assertJsonEqualsJson('null', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NULL)));
$this->assertJsonEqualsJson('""', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING)));
$this->assertJsonEqualsJson('false', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE)));
}

public function testNullableEnum()
Expand All @@ -279,4 +280,9 @@ public function testBooleanEnum()
$this->assertFalse(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE()->getValue());
$this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize());
}

private function assertJsonEqualsJson($json1, $json2)
{
$this->assertJsonStringEqualsJsonString($json1, $json2);
}
}