Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Add array storage for development/testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Mar 9, 2016
1 parent 2517bb0 commit f296221
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ database backends, you have to configure the actual storage you want to use.
This configuration is obviously specific to all the different storage drivers.
So far the following drivers exist (and are documented here):

* PHP Array
* Doctrine Cache Backend
* SQL Backend with Doctrine DBAL
* Microsoft Windows Azure Table
Expand All @@ -63,6 +64,19 @@ So far the following drivers exist (and are documented here):
Also all those storage backends obviously have different dependencies in terms
of PHP libraries or PHP PECL extensions.

PHP Array
---------

PHP array is used mainly for development and teesting purposes.

.. code-block:: php
<?php
use Doctrine\KeyValueStore\Storage\ArrayStorage;
$storage = new ArrayStorage();
Doctrine Cache Backend
----------------------

Expand Down
136 changes: 136 additions & 0 deletions lib/Doctrine/KeyValueStore/Storage/ArrayStorage.php
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 tests/Doctrine/Tests/KeyValueStore/Storage/ArrayStorageTest.php
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());
}
}

0 comments on commit f296221

Please sign in to comment.