-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6283e1e
commit a6894c7
Showing
2 changed files
with
111 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
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
55
test/unit/DataStore/DataStore/Aspect/AspectReadOnlyTest.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,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'])); | ||
} | ||
} |