This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array storage for development/testing purposes
- Loading branch information
1 parent
2517bb0
commit f296221
Showing
3 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\KeyValueStore\Storage; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\KeyValueStore\NotFoundException; | ||
|
||
/** | ||
* Array storage, mainly used for development purposes. | ||
* | ||
* @author Emanuele Minotto <[email protected]> | ||
*/ | ||
class ArrayStorage implements Storage | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $data = []; | ||
|
||
public function supportsPartialUpdates() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Does this storage support composite primary keys? | ||
* | ||
* @return bool | ||
*/ | ||
public function supportsCompositePrimaryKeys() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Does this storage require composite primary keys? | ||
* | ||
* @return bool | ||
*/ | ||
public function requiresCompositePrimaryKeys() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Insert data into the storage key specified. | ||
* | ||
* @param array|string $key | ||
* @param array $data | ||
*/ | ||
public function insert($storageName, $key, array $data) | ||
{ | ||
$this->update($storageName, $key, $data); | ||
} | ||
|
||
/** | ||
* Update data into the given key. | ||
* | ||
* @param array|string $key | ||
* @param array $data | ||
*/ | ||
public function update($storageName, $key, array $data) | ||
{ | ||
if (!isset($this->data[$storageName])) { | ||
$this->data[$storageName] = []; | ||
} | ||
|
||
$this->data[$storageName][serialize($key)] = $data; | ||
} | ||
|
||
/** | ||
* Delete data at key | ||
* | ||
* @param array|string $key | ||
*/ | ||
public function delete($storageName, $key) | ||
{ | ||
if (!isset($this->data[$storageName])) { | ||
return; | ||
} | ||
|
||
if (!isset($this->data[$storageName][serialize($key)])) { | ||
return; | ||
} | ||
|
||
unset($this->data[$storageName][serialize($key)]); | ||
} | ||
|
||
/** | ||
* Find data at key | ||
* | ||
* @param array|string $key | ||
* | ||
* @return array | ||
*/ | ||
public function find($storageName, $key) | ||
{ | ||
if (!isset($this->data[$storageName])) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
if (!isset($this->data[$storageName][serialize($key)])) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
unset($this->data[$storageName][serialize($key)]); | ||
} | ||
|
||
/** | ||
* Return a name of the underlying storage. | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'array'; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
tests/Doctrine/Tests/KeyValueStore/Storage/ArrayStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Tests\KeyValueStore\Storage; | ||
|
||
use Doctrine\KeyValueStore\Storage\ArrayStorage; | ||
use ReflectionProperty; | ||
|
||
/** | ||
* @author Emanuele Minotto <[email protected]> | ||
*/ | ||
class ArrayStorageTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ArrayStorage | ||
*/ | ||
private $storage; | ||
|
||
protected function setup() | ||
{ | ||
$this->storage = new ArrayStorage(); | ||
} | ||
|
||
public function testSupportsPartialUpdates() | ||
{ | ||
$this->assertFalse($this->storage->supportsPartialUpdates()); | ||
} | ||
|
||
public function testSupportsCompositePrimaryKeys() | ||
{ | ||
$this->assertFalse($this->storage->supportsCompositePrimaryKeys()); | ||
} | ||
|
||
public function testRequiresCompositePrimaryKeys() | ||
{ | ||
$this->assertFalse($this->storage->requiresCompositePrimaryKeys()); | ||
} | ||
|
||
/** | ||
* @dataProvider methodsProvider | ||
*/ | ||
public function testInsert($method) | ||
{ | ||
$data = [ | ||
'author' => 'John Doe', | ||
'title' => 'example book', | ||
]; | ||
|
||
$this->storage->$method('foo', 'bar', $data); | ||
|
||
$reflector = new ReflectionProperty(ArrayStorage::class, 'data'); | ||
$reflector->setAccessible(true); | ||
|
||
$storedValue = $reflector->getValue($this->storage); | ||
|
||
$this->assertEquals( | ||
[ | ||
'foo' => [ | ||
serialize('bar') => $data, | ||
], | ||
], | ||
$storedValue | ||
); | ||
|
||
$this->storage->$method('foo', 'bar', $data); | ||
$this->assertCount(1, $storedValue); | ||
$this->assertCount(1, $storedValue['foo']); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function methodsProvider() | ||
{ | ||
return [ | ||
['insert'], | ||
['update'], | ||
]; | ||
} | ||
|
||
public function testGetName() | ||
{ | ||
$this->assertEquals('array', $this->storage->getName()); | ||
} | ||
} |