Skip to content

Commit

Permalink
add AspectReadOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
maria-rollun committed Dec 15, 2021
1 parent 6283e1e commit a6894c7
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/DataStore/src/DataStore/Aspect/AspectReadOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @copyright Copyright © 2014 Rollun LC (http://rollun.com/)
* @license LICENSE.md New BSD License
*/

namespace rollun\datastore\DataStore\Aspect;

use rollun\datastore\DataStore\DataStoreException;
use rollun\datastore\DataStore\Traits\NoSupportCreateTrait;
use rollun\datastore\DataStore\Traits\NoSupportDeleteAllTrait;
use rollun\datastore\DataStore\Traits\NoSupportDeleteTrait;
use rollun\datastore\DataStore\Traits\NoSupportUpdateTrait;
use Xiag\Rql\Parser\Query;

/**
* Class AspectReadOnly
*
* This is wrapper for any type of datastore which disallow all methods except ReadInterface.
* It is useful for wrapping datastores from production in local environment.
*
* @see AspectAbstractFactory
* @package rollun\datastore\DataStore\Aspect
*/
class AspectReadOnly extends AspectAbstract
{
use NoSupportCreateTrait;
use NoSupportDeleteAllTrait;
use NoSupportDeleteTrait;
use NoSupportUpdateTrait;

public function multiCreate($records)
{
throw new DataStoreException("Method don't support.");
}

public function multiUpdate($records)
{
throw new DataStoreException("Method don't support.");
}

public function queriedUpdate($record, Query $query)
{
throw new DataStoreException("Method don't support.");
}

public function rewrite($record)
{
throw new DataStoreException("Method don't support.");
}

public function queriedDelete(Query $query)
{
throw new DataStoreException("Method don't support.");
}
}
55 changes: 55 additions & 0 deletions test/unit/DataStore/DataStore/Aspect/AspectReadOnlyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @copyright Copyright © 2014 Rollun LC (http://rollun.com/)
* @license LICENSE.md New BSD License
*/

namespace rollun\test\unit\DataStore\DataStore\Aspect;

use PHPUnit\Framework\TestCase;
use rollun\datastore\DataStore\Aspect\AspectReadOnly;
use rollun\datastore\DataStore\DataStoreException;
use rollun\datastore\DataStore\Interfaces\ReadInterface;
use rollun\datastore\DataStore\Memory;
use Xiag\Rql\Parser\Query;

class AspectReadOnlyTest extends TestCase
{
public function disallowedMethods(): array
{
$excluded = [
'__construct',
];

$methods = get_class_methods(AspectReadOnly::class);

$methods = array_filter($methods, function ($method) use ($excluded) {
return !in_array($method, $excluded) && !method_exists(ReadInterface::class, $method);
});

return array_map(function ($method) {
return [$method];
}, $methods);
}

/**
* @dataProvider disallowedMethods
*/
public function testOnlyReadMethodsAreAvailable($method)
{
$params = [
'default' => [[]],
'delete' => [1],
'queriedUpdate' => [[], new Query()],
'queriedDelete' => [new Query()],
];

$datastore = new Memory();

$aspect = new AspectReadOnly($datastore);

$this->expectException(DataStoreException::class);

$aspect->$method(...($params[$method] ?? $params['default']));
}
}

0 comments on commit a6894c7

Please sign in to comment.