2017-07-06 15:29:33 +03:00
|
|
|
<?php
|
|
|
|
|
2018-09-02 13:44:21 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace GraphQL\Tests\Utils;
|
2017-07-06 15:29:33 +03:00
|
|
|
|
|
|
|
use GraphQL\Utils\MixedStore;
|
2018-09-02 13:44:21 +03:00
|
|
|
use GraphQL\Utils\Utils;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-07-06 15:29:33 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class MixedStoreTest extends TestCase
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
2018-09-02 13:44:21 +03:00
|
|
|
/** @var MixedStore */
|
2017-07-06 15:29:33 +03:00
|
|
|
private $mixedStore;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->mixedStore = new MixedStore();
|
|
|
|
}
|
|
|
|
|
2018-09-02 13:44:21 +03:00
|
|
|
public function testAcceptsNullKeys() : void
|
|
|
|
{
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue(null, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:29:33 +03:00
|
|
|
public function getPossibleValues()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
'',
|
|
|
|
'0',
|
|
|
|
'1',
|
|
|
|
'a',
|
|
|
|
[],
|
|
|
|
new \stdClass(),
|
2018-09-02 13:44:21 +03:00
|
|
|
function () {
|
|
|
|
},
|
|
|
|
new MixedStore(),
|
2017-07-06 15:29:33 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-09-02 13:44:21 +03:00
|
|
|
private function assertAcceptsKeyValue($key, $value)
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
2018-09-02 13:44:21 +03:00
|
|
|
$err = 'Failed assertion that MixedStore accepts key ' .
|
|
|
|
Utils::printSafe($key) . ' with value ' . Utils::printSafe($value);
|
|
|
|
|
|
|
|
$this->assertFalse($this->mixedStore->offsetExists($key), $err);
|
|
|
|
$this->mixedStore->offsetSet($key, $value);
|
|
|
|
$this->assertTrue($this->mixedStore->offsetExists($key), $err);
|
|
|
|
$this->assertSame($value, $this->mixedStore->offsetGet($key), $err);
|
|
|
|
$this->mixedStore->offsetUnset($key);
|
|
|
|
$this->assertFalse($this->mixedStore->offsetExists($key), $err);
|
|
|
|
$this->assertProvidesArrayAccess($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertProvidesArrayAccess($key, $value)
|
|
|
|
{
|
|
|
|
$err = 'Failed assertion that MixedStore provides array access for key ' .
|
|
|
|
Utils::printSafe($key) . ' with value ' . Utils::printSafe($value);
|
|
|
|
|
|
|
|
$this->assertFalse(isset($this->mixedStore[$key]), $err);
|
|
|
|
$this->mixedStore[$key] = $value;
|
|
|
|
$this->assertTrue(isset($this->mixedStore[$key]), $err);
|
|
|
|
$this->assertEquals(! empty($value), ! empty($this->mixedStore[$key]), $err);
|
|
|
|
$this->assertSame($value, $this->mixedStore[$key], $err);
|
|
|
|
unset($this->mixedStore[$key]);
|
|
|
|
$this->assertFalse(isset($this->mixedStore[$key]), $err);
|
2017-07-06 15:29:33 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAcceptsBoolKeys() : void
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue(false, $value);
|
|
|
|
}
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue(true, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAcceptsIntKeys() : void
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue(-100000, $value);
|
|
|
|
$this->assertAcceptsKeyValue(-1, $value);
|
|
|
|
$this->assertAcceptsKeyValue(0, $value);
|
|
|
|
$this->assertAcceptsKeyValue(1, $value);
|
|
|
|
$this->assertAcceptsKeyValue(1000000, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAcceptsFloatKeys() : void
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue(-100000.5, $value);
|
|
|
|
$this->assertAcceptsKeyValue(-1.6, $value);
|
|
|
|
$this->assertAcceptsKeyValue(-0.0001, $value);
|
|
|
|
$this->assertAcceptsKeyValue(0.0000, $value);
|
|
|
|
$this->assertAcceptsKeyValue(0.0001, $value);
|
|
|
|
$this->assertAcceptsKeyValue(1.6, $value);
|
|
|
|
$this->assertAcceptsKeyValue(1000000.5, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAcceptsArrayKeys() : void
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue([], $value);
|
|
|
|
$this->assertAcceptsKeyValue([null], $value);
|
|
|
|
$this->assertAcceptsKeyValue([[]], $value);
|
|
|
|
$this->assertAcceptsKeyValue([new \stdClass()], $value);
|
|
|
|
$this->assertAcceptsKeyValue(['a', 'b'], $value);
|
|
|
|
$this->assertAcceptsKeyValue(['a' => 'b'], $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAcceptsObjectKeys() : void
|
2017-07-06 15:29:33 +03:00
|
|
|
{
|
|
|
|
foreach ($this->getPossibleValues() as $value) {
|
|
|
|
$this->assertAcceptsKeyValue(new \stdClass(), $value);
|
|
|
|
$this->assertAcceptsKeyValue(new MixedStore(), $value);
|
2018-09-02 13:44:21 +03:00
|
|
|
$this->assertAcceptsKeyValue(
|
|
|
|
function () {
|
|
|
|
},
|
|
|
|
$value
|
|
|
|
);
|
2017-07-06 15:29:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|