From 0fc33d34db75d9fc58449d239f1bfd35ed27f564 Mon Sep 17 00:00:00 2001 From: Maria Date: Mon, 25 Oct 2021 14:46:34 +0300 Subject: [PATCH 1/7] start remove old datastore interface --- .../src/DataStore/Aspect/AspectAbstract.php | 72 +++++-------------- src/DataStore/src/DataStore/CsvBase.php | 47 ++---------- .../src/DataStore/DataStoreAbstract.php | 6 +- src/DataStore/src/DataStore/DbTable.php | 46 +++--------- src/DataStore/src/DataStore/HttpClient.php | 23 ++---- .../Interfaces/DateTimeInterface.php | 2 +- src/DataStore/src/DataStore/Memory.php | 31 ++------ 7 files changed, 43 insertions(+), 184 deletions(-) diff --git a/src/DataStore/src/DataStore/Aspect/AspectAbstract.php b/src/DataStore/src/DataStore/Aspect/AspectAbstract.php index 2ae39995..97b84b68 100644 --- a/src/DataStore/src/DataStore/Aspect/AspectAbstract.php +++ b/src/DataStore/src/DataStore/Aspect/AspectAbstract.php @@ -7,7 +7,6 @@ namespace rollun\datastore\DataStore\Aspect; use rollun\datastore\DataStore\Interfaces\DataStoreInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; use Xiag\Rql\Parser\Query; /** @@ -22,17 +21,17 @@ * @see AspectAbstractFactory * @package rollun\datastore\DataStore\Aspect */ -class AspectAbstract implements DataStoresInterface, DataStoreInterface +class AspectAbstract implements DataStoreInterface { - /** @var DataStoresInterface $dataStore */ + /** @var DataStoreInterface $dataStore */ protected $dataStore; /** * AspectDataStoreAbstract constructor. * - * @param DataStoresInterface $dataStore + * @param DataStoreInterface $dataStore */ - public function __construct(DataStoresInterface $dataStore) + public function __construct(DataStoreInterface $dataStore) { $this->dataStore = $dataStore; } @@ -77,10 +76,9 @@ protected function postGetIterator(\Traversable $iterator) * By default does nothing * * @param $itemData - * @param bool|false $rewriteIfExist * @return array */ - protected function preCreate($itemData, $rewriteIfExist = false) + protected function preCreate($itemData) { return $itemData; } @@ -90,12 +88,12 @@ protected function preCreate($itemData, $rewriteIfExist = false) * * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - $newData = $this->preCreate($itemData, $rewriteIfExist); - $result = $this->dataStore->create($newData, $rewriteIfExist); + $newData = $this->preCreate($itemData); + $result = $this->dataStore->create($newData); - return $this->postCreate($result, $newData, $rewriteIfExist); + return $this->postCreate($result, $newData); } /** @@ -105,10 +103,9 @@ public function create($itemData, $rewriteIfExist = false) * * @param $result * @param $itemData - * @param $rewriteIfExist * @return mixed */ - protected function postCreate($result, $itemData, $rewriteIfExist) + protected function postCreate($result, $itemData) { return $result; } @@ -119,10 +116,9 @@ protected function postCreate($result, $itemData, $rewriteIfExist) * By default does nothing * * @param $itemData - * @param bool|false $createIfAbsent * @return array */ - protected function preUpdate($itemData, $createIfAbsent = false) + protected function preUpdate($itemData) { return $itemData; } @@ -132,12 +128,12 @@ protected function preUpdate($itemData, $createIfAbsent = false) * * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - $newData = $this->preUpdate($itemData, $createIfAbsent); - $result = $this->dataStore->update($newData, $createIfAbsent); + $newData = $this->preUpdate($itemData); + $result = $this->dataStore->update($newData); - return $this->postUpdate($result, $newData, $createIfAbsent); + return $this->postUpdate($result, $newData); } /** @@ -147,10 +143,9 @@ public function update($itemData, $createIfAbsent = false) * * @param mixed $result * @param $itemData - * @param $createIfAbsent * @return mixed */ - protected function postUpdate($result, $itemData, $createIfAbsent) + protected function postUpdate($result, $itemData) { return $result; } @@ -193,41 +188,6 @@ protected function postDelete($result, $id) return $result; } - /** - * The pre-aspect for "deleteAll". - * - * By default does nothing - */ - protected function preDeleteAll() - { - } - - /** - * {@inheritdoc} - * - * {@inheritdoc} - */ - public function deleteAll() - { - $this->preDeleteAll(); - $result = $this->dataStore->deleteAll(); - - return $this->postDeleteAll($result); - } - - /** - * The post-aspect for "deleteAll" - * - * By default does nothing - * - * @param mixed $result - * @return mixed - */ - protected function postDeleteAll($result) - { - return $result; - } - /** * The pre-aspect for "getIdentifier". * diff --git a/src/DataStore/src/DataStore/CsvBase.php b/src/DataStore/src/DataStore/CsvBase.php index 0e8db41f..3719c49d 100644 --- a/src/DataStore/src/DataStore/CsvBase.php +++ b/src/DataStore/src/DataStore/CsvBase.php @@ -119,12 +119,8 @@ public function getIterator() /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - $identifier = $this->getIdentifier(); switch (true) { @@ -133,7 +129,7 @@ public function create($itemData, $rewriteIfExist = false) $item = $this->createNewItem($itemData); $item[$identifier] = $this->generatePrimaryKey(); break; - case (!$rewriteIfExist && !is_null($this->read($itemData[$identifier]))): + case (!is_null($this->read($itemData[$identifier]))): throw new DataStoreException("Item is already exist with id = $itemData[$identifier]"); break; default: @@ -152,12 +148,8 @@ public function create($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - $identifier = $this->getIdentifier(); if (!isset($itemData[$identifier])) { @@ -168,13 +160,8 @@ public function update($itemData, $createIfAbsent = false) $this->checkIdentifierType($id); $item = $this->read($id); - switch (true) { - case (is_null($item) && !$createIfAbsent): - throw new DataStoreException("Can't update item with id = $id: item does not exist."); - case (is_null($item) && $createIfAbsent): - // new item - $item = $this->createNewItem($itemData); - break; + if (is_null($item)) { + throw new DataStoreException("Can't update item with id = $id: item does not exist."); } foreach ($item as $key => $value) { @@ -207,30 +194,6 @@ public function delete($id) return null; } - /** - * {@inheritdoc} - * - * {@inheritdoc} - */ - public function deleteAll() - { - // Count rows - $count = $this->count(); - $tmpFile = tempnam("/tmp", uniqid() . '.tmp'); - $tempHandler = fopen($tmpFile, 'w'); - - // Write the headings only and right away closes file - fputcsv($tempHandler, $this->columns, $this->csvDelimiter); - fclose($tempHandler); - - // Changes the original file to a temporary one. - if (!rename($tmpFile, $this->filename)) { - throw new DataStoreException("Failed to write the results to a file."); - } - - return $count; - } - /** * Flushes all changes to temporary file which then will change the original one * diff --git a/src/DataStore/src/DataStore/DataStoreAbstract.php b/src/DataStore/src/DataStore/DataStoreAbstract.php index 352cd135..22a8a286 100644 --- a/src/DataStore/src/DataStore/DataStoreAbstract.php +++ b/src/DataStore/src/DataStore/DataStoreAbstract.php @@ -22,7 +22,7 @@ * Class DataStoreAbstract * @package rollun\datastore\DataStore */ -abstract class DataStoreAbstract implements DataStoresInterface, DataStoreInterface +abstract class DataStoreAbstract implements DataStoreInterface { /** * @var ConditionBuilderAbstract @@ -377,7 +377,7 @@ protected function querySelect($data, Query $query) /** * {@inheritdoc} */ - abstract public function create($itemData, $rewriteIfExist = false); + abstract public function create($itemData); /** * {@inheritdoc} @@ -405,7 +405,7 @@ public function multiCreate($records) /** * {@inheritdoc} */ - abstract public function update($itemData, $createIfAbsent = false); + abstract public function update($itemData); /** * {@inheritdoc} diff --git a/src/DataStore/src/DataStore/DbTable.php b/src/DataStore/src/DataStore/DbTable.php index 3a7f59a0..c69f6492 100644 --- a/src/DataStore/src/DataStore/DbTable.php +++ b/src/DataStore/src/DataStore/DbTable.php @@ -88,18 +88,13 @@ protected function getSqlQueryBuilder() /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - // 'rewriteIfExist' do not work with multiply insert - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - $adapter = $this->dbTable->getAdapter(); $adapter->getDriver()->getConnection()->beginTransaction(); try { - $insertedItem = $this->insertItem($itemData, $rewriteIfExist); + $insertedItem = $this->insertItem($itemData); $adapter->getDriver()->getConnection()->commit(); } catch (\Throwable $e) { $adapter->getDriver()->getConnection()->rollback(); @@ -119,15 +114,10 @@ public function create($itemData, $rewriteIfExist = false) /** * @param $itemData - * @param bool $rewriteIfExist * @return array|mixed|null */ - protected function insertItem($itemData, $rewriteIfExist = false) + protected function insertItem($itemData) { - if ($rewriteIfExist && isset($itemData[$this->getIdentifier()])) { - $this->delete($itemData[$this->getIdentifier()]); - } - $start = microtime(true); $response = $this->dbTable->insert($itemData); $end = microtime(true); @@ -156,12 +146,8 @@ protected function insertItem($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - if (!isset($itemData[$this->getIdentifier()])) { throw new DataStoreException('Item must has primary key'); } @@ -170,7 +156,7 @@ public function update($itemData, $createIfAbsent = false) $adapter->getDriver()->getConnection()->beginTransaction(); try { - $result = $this->updateItem($itemData, $createIfAbsent); + $result = $this->updateItem($itemData); $adapter->getDriver()->getConnection()->commit(); } catch (\Throwable $e) { $adapter->getDriver()->getConnection()->rollback(); @@ -357,11 +343,10 @@ private function selectForUpdateWithQuery(Query $query) /** * @param $itemData - * @param bool $createIfAbsent * @return array|mixed|null - * @throws DataStoreException + * @throws DataStoreException|\Throwable */ - protected function updateItem($itemData, $createIfAbsent = false) + protected function updateItem($itemData) { $identifier = $this->getIdentifier(); $id = $itemData[$identifier]; @@ -373,13 +358,9 @@ protected function updateItem($itemData, $createIfAbsent = false) $start = microtime(true); - if (!$isExist && $createIfAbsent) { - $response = $this->dbTable->insert($itemData); - $loggedMethod = 'insert'; - } elseif ($isExist) { + if ($isExist) { unset($itemData[$identifier]); $response = $this->dbTable->update($itemData, [$identifier => $id]); - $loggedMethod = 'update'; } else { throw new DataStoreException("[{$this->dbTable->getTable()}]Can't update item with id = $id"); } @@ -387,7 +368,7 @@ protected function updateItem($itemData, $createIfAbsent = false) $end = microtime(true); $logContext = [ - self::LOG_METHOD => $loggedMethod, + self::LOG_METHOD => 'update', self::LOG_TABLE => $this->dbTable->getTable(), self::LOG_REQUEST => $itemData, self::LOG_TIME => $this->getRequestTime($start, $end), @@ -527,15 +508,6 @@ public function read($id) return $response; } - /** - * {@inheritdoc} - */ - public function deleteAll() - { - $where = '1=1'; - return $this->dbTable->delete($where); - } - /** * {@inheritdoc} */ diff --git a/src/DataStore/src/DataStore/HttpClient.php b/src/DataStore/src/DataStore/HttpClient.php index 3506c3ce..ddc9f6d4 100644 --- a/src/DataStore/src/DataStore/HttpClient.php +++ b/src/DataStore/src/DataStore/HttpClient.php @@ -204,10 +204,9 @@ public function read($id) * * @param string $method ('GET', 'HEAD', 'POST', 'PUT', 'DELETE') * @param string $uri - * @param bool $ifMatch * @return Client */ - protected function initHttpClient(string $method, string $uri, $ifMatch = false) + protected function initHttpClient(string $method, string $uri) { $httpClient = clone $this->client; $httpClient->setUri($uri); @@ -218,10 +217,6 @@ protected function initHttpClient(string $method, string $uri, $ifMatch = false) $headers['X-Life-Cycle-Token'] = $this->lifeCycleToken->serialize(); $headers['LifeCycleToken'] = $this->lifeCycleToken->serialize(); - if ($ifMatch) { - $headers['If-Match'] = '*'; - } - $httpClient->setHeaders($headers); if (isset($this->login) && isset($this->password)) { @@ -282,13 +277,9 @@ public function query(Query $query) /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - - $client = $this->initHttpClient(Request::METHOD_POST, $this->url, $rewriteIfExist); + $client = $this->initHttpClient(Request::METHOD_POST, $this->url); $json = Serializer::jsonSerialize($itemData); $client->setRawBody($json); $response = $client->send(); @@ -344,12 +335,8 @@ public function multiCreate($records) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - $identifier = $this->getIdentifier(); if (!isset($itemData[$identifier])) { @@ -361,7 +348,7 @@ public function update($itemData, $createIfAbsent = false) $uri = $this->createUri(null, $itemData[$identifier]); unset($itemData[$identifier]); - $client = $this->initHttpClient(Request::METHOD_PUT, $uri, $createIfAbsent); + $client = $this->initHttpClient(Request::METHOD_PUT, $uri); $client->setRawBody(Serializer::jsonSerialize($itemData)); $response = $client->send(); diff --git a/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php b/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php index ab5b5bcf..75544e19 100644 --- a/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php +++ b/src/DataStore/src/DataStore/Interfaces/DateTimeInterface.php @@ -10,7 +10,7 @@ * Interface DateTimeInterface * @package rollun\datastore\DataStore\Interfaces */ -interface DateTimeInterface extends DataStoresInterface +interface DateTimeInterface extends DataStoreInterface { public const FIELD_DATETIME = "datetime"; diff --git a/src/DataStore/src/DataStore/Memory.php b/src/DataStore/src/DataStore/Memory.php index d443d5ca..9a05d91b 100644 --- a/src/DataStore/src/DataStore/Memory.php +++ b/src/DataStore/src/DataStore/Memory.php @@ -76,18 +76,14 @@ public function read($id) /** * {@inheritdoc} */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - $this->checkOnExistingColumns($itemData); $identifier = $this->getIdentifier(); $id = isset($itemData[$identifier]) ? $itemData[$identifier] : null; if ($id) { - if (isset($this->items[$id]) && !$rewriteIfExist) { + if (isset($this->items[$id])) { throw new DataStoreException("Item with id '{$itemData[$identifier]}' already exist"); } @@ -106,12 +102,8 @@ public function create($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use", E_USER_DEPRECATED); - } - $this->checkOnExistingColumns($itemData); $identifier = $this->getIdentifier(); @@ -129,11 +121,7 @@ public function update($itemData, $createIfAbsent = false) unset($itemData[$id]); } else { - if ($createIfAbsent) { - $this->items[$id] = $itemData; - } else { - throw new DataStoreException("Item doesn't exist with id = $id"); - } + throw new DataStoreException("Item doesn't exist with id = $id"); } return $this->items[$id]; @@ -156,17 +144,6 @@ public function delete($id) return null; } - /** - * {@inheritdoc} - */ - public function deleteAll() - { - $deletedItemsCount = count($this->items); - $this->items = []; - - return $deletedItemsCount; - } - /** * {@inheritdoc} */ From bb2c77808235d21c0b38aaf43b3da8559f017df2 Mon Sep 17 00:00:00 2001 From: Maria Date: Fri, 29 Oct 2021 16:16:23 +0300 Subject: [PATCH 2/7] remove old datastore interface --- src/DataStore/src/AbstractFactoryAbstract.php | 4 +- .../src/Cleaner/CleanableListAdapter.php | 6 +- src/DataStore/src/Cleaner/Cleaner.php | 4 +- .../DataStore/Aspect/AbstractMapperAspect.php | 2 - .../src/DataStore/Aspect/AspectAbstract.php | 2 +- .../src/DataStore/Aspect/AspectTyped.php | 14 +-- .../Aspect/AspectWithEventManagerAbstract.php | 42 ++----- .../Aspect/Factory/AspectAbstractFactory.php | 2 - src/DataStore/src/DataStore/Cacheable.php | 103 +++++++++++------- .../src/DataStore/DataStoreAbstract.php | 30 +---- .../src/DataStore/DataStorePluginManager.php | 4 +- .../Factory/CacheableAbstractFactory.php | 2 +- .../Factory/MemoryAbstractFactory.php | 1 - .../Interfaces/DataStoreInterface.php | 2 +- .../DataStore/Interfaces/DbTableInterface.php | 2 +- .../src/DataStore/Traits/DateTimeTrait.php | 10 +- .../DataStore/Traits/NoSupportCreateTrait.php | 3 +- .../DataStore/Traits/NoSupportUpdateTrait.php | 3 +- .../DataStore/Traits/PrepareFieldsTrait.php | 13 +-- .../src/Middleware/DataStoreAbstract.php | 13 +-- .../src/Middleware/DataStoreRest.php | 8 +- .../Factory/TableGatewayAbstractFactory.php | 2 +- .../ModelRepositoryAbstractFactory.php | 2 +- src/Uploader/src/Iterator/DataStorePack.php | 8 +- src/Uploader/src/Uploader.php | 10 +- 25 files changed, 127 insertions(+), 165 deletions(-) diff --git a/src/DataStore/src/AbstractFactoryAbstract.php b/src/DataStore/src/AbstractFactoryAbstract.php index 32a36b0f..f2b65162 100644 --- a/src/DataStore/src/AbstractFactoryAbstract.php +++ b/src/DataStore/src/AbstractFactoryAbstract.php @@ -6,7 +6,7 @@ namespace rollun\datastore; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use Zend\ServiceManager\Factory\AbstractFactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Interop\Container\ContainerInterface; @@ -58,7 +58,7 @@ abstract public function canCreate(ContainerInterface $container, $requestedName * @param ContainerInterface $container * @param string $requestedName * @param array $options - * @return DataStoresInterface + * @return DataStoreInterface */ abstract public function __invoke(ContainerInterface $container, $requestedName, array $options = null); diff --git a/src/DataStore/src/Cleaner/CleanableListAdapter.php b/src/DataStore/src/Cleaner/CleanableListAdapter.php index 18af7f0a..402e2873 100644 --- a/src/DataStore/src/Cleaner/CleanableListAdapter.php +++ b/src/DataStore/src/Cleaner/CleanableListAdapter.php @@ -8,17 +8,17 @@ use Exception; use rollun\utils\Cleaner\CleanableList\CleanableListInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStoreCleanerException; class CleanableListAdapter implements \IteratorAggregate, CleanableListInterface { /** - * @var DataStoresInterface + * @var DataStoreInterface */ protected $datastore; - public function __construct(DataStoresInterface $datastore) + public function __construct(DataStoreInterface $datastore) { $this->datastore = $datastore; } diff --git a/src/DataStore/src/Cleaner/Cleaner.php b/src/DataStore/src/Cleaner/Cleaner.php index e4762861..0aa138e9 100644 --- a/src/DataStore/src/Cleaner/Cleaner.php +++ b/src/DataStore/src/Cleaner/Cleaner.php @@ -8,7 +8,7 @@ use rollun\utils\Cleaner\CleaningValidator\CleaningValidatorInterface; use rollun\utils\Cleaner\Cleaner as BaseCleaner; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; /** * Class Cleaner @@ -16,7 +16,7 @@ */ class Cleaner extends BaseCleaner { - public function __construct(DataStoresInterface $datastore, CleaningValidatorInterface $cleaningValidator) + public function __construct(DataStoreInterface $datastore, CleaningValidatorInterface $cleaningValidator) { $cleanableList = new CleanableListAdapter($datastore); parent::__construct($cleanableList, $cleaningValidator); diff --git a/src/DataStore/src/DataStore/Aspect/AbstractMapperAspect.php b/src/DataStore/src/DataStore/Aspect/AbstractMapperAspect.php index b77e1eb7..4bd51595 100644 --- a/src/DataStore/src/DataStore/Aspect/AbstractMapperAspect.php +++ b/src/DataStore/src/DataStore/Aspect/AbstractMapperAspect.php @@ -8,7 +8,6 @@ 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\Node\AbstractQueryNode; @@ -22,7 +21,6 @@ abstract class AbstractMapperAspect extends AspectAbstract { use NoSupportUpdateTrait; use NoSupportCreateTrait; - use NoSupportDeleteAllTrait; use NoSupportDeleteTrait; /** diff --git a/src/DataStore/src/DataStore/Aspect/AspectAbstract.php b/src/DataStore/src/DataStore/Aspect/AspectAbstract.php index 97b84b68..01712f97 100644 --- a/src/DataStore/src/DataStore/Aspect/AspectAbstract.php +++ b/src/DataStore/src/DataStore/Aspect/AspectAbstract.php @@ -13,7 +13,7 @@ * Class AspectAbstract * * This is wrapper for any type of datastore which allows to do 'pre' and 'post' actions - * for each method of the DataStoresInterface. + * for each method of the DataStoreInterface. * * The class is NOT abstract. It is so named because in this view it does nothing and have no difference at work * with usual datastore any type. diff --git a/src/DataStore/src/DataStore/Aspect/AspectTyped.php b/src/DataStore/src/DataStore/Aspect/AspectTyped.php index fefc3198..7c9e3793 100644 --- a/src/DataStore/src/DataStore/Aspect/AspectTyped.php +++ b/src/DataStore/src/DataStore/Aspect/AspectTyped.php @@ -10,7 +10,7 @@ use rollun\datastore\DataStore\BaseDto; use rollun\datastore\DataStore\Formatter\FormatterInterface; use rollun\datastore\DataStore\Formatter\FormatterPluginManager; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Interfaces\SchemableInterface; use rollun\datastore\DataStore\Type\TypePluginManager; use RuntimeException; @@ -41,14 +41,14 @@ class AspectTyped extends AspectAbstract implements SchemableInterface /** * AspectTyped constructor. - * @param DataStoresInterface $dataStore + * @param DataStoreInterface $dataStore * @param array $scheme * @param string $dtoClassName * @param TypePluginManager|null $typePluginManager * @param FormatterPluginManager|null $formatterPluginManager */ public function __construct( - DataStoresInterface $dataStore, + DataStoreInterface $dataStore, array $scheme, string $dtoClassName, TypePluginManager $typePluginManager = null, @@ -89,7 +89,7 @@ public function __construct( /** * {@inheritdoc} */ - protected function preUpdate($itemData, $createIfAbsent = false) + protected function preUpdate($itemData) { if ($itemData instanceof BaseDto) { $itemData = $this->dtoToArray($itemData); @@ -101,7 +101,7 @@ protected function preUpdate($itemData, $createIfAbsent = false) /** * {@inheritdoc} */ - protected function postUpdate($result, $itemData, $rewriteIfExist) + protected function postUpdate($result, $itemData) { if (is_array($result)) { return $this->arrayToDto($result); @@ -113,7 +113,7 @@ protected function postUpdate($result, $itemData, $rewriteIfExist) /** * {@inheritdoc} */ - protected function preCreate($itemData, $rewriteIfExist = false) + protected function preCreate($itemData) { if ($itemData instanceof BaseDto) { $itemData = $this->dtoToArray($itemData); @@ -125,7 +125,7 @@ protected function preCreate($itemData, $rewriteIfExist = false) /** * {@inheritdoc} */ - protected function postCreate($result, $itemData, $rewriteIfExist) + protected function postCreate($result, $itemData) { if (is_array($result)) { return $this->arrayToDto($result); diff --git a/src/DataStore/src/DataStore/Aspect/AspectWithEventManagerAbstract.php b/src/DataStore/src/DataStore/Aspect/AspectWithEventManagerAbstract.php index fd785841..87b08c53 100644 --- a/src/DataStore/src/DataStore/Aspect/AspectWithEventManagerAbstract.php +++ b/src/DataStore/src/DataStore/Aspect/AspectWithEventManagerAbstract.php @@ -8,7 +8,7 @@ use rollun\datastore\DataStore\WithEventManagerInterface; use Xiag\Rql\Parser\Query; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use Zend\EventManager\EventManager; use Zend\EventManager\EventManagerInterface; @@ -34,11 +34,11 @@ class AspectWithEventManagerAbstract extends AspectAbstract implements WithEvent /** * AspectWithEventManagerAbstract constructor. * - * @param DataStoresInterface $dataStore + * @param DataStoreInterface $dataStore * @param EventManagerInterface|null $eventManager * @param string|null $dataStoreName */ - public function __construct(DataStoresInterface $dataStore, EventManagerInterface $eventManager = null, string $dataStoreName = null) + public function __construct(DataStoreInterface $dataStore, EventManagerInterface $eventManager = null, string $dataStoreName = null) { parent::__construct($dataStore); @@ -73,41 +73,41 @@ protected function postGetIterator(\Traversable $iterator) /** * @inheritDoc */ - protected function preCreate($itemData, $rewriteIfExist = false) + protected function preCreate($itemData) { $this->triggerEvent('onPreCreate', ['itemData' => $itemData]); - return parent::preCreate($itemData, $rewriteIfExist); + return parent::preCreate($itemData); } /** * @inheritDoc */ - protected function postCreate($result, $itemData, $rewriteIfExist) + protected function postCreate($result, $itemData) { $this->triggerEvent('onPostCreate', ['itemData' => $itemData, 'result' => $result]); - return parent::postCreate($result, $itemData, $rewriteIfExist); + return parent::postCreate($result, $itemData); } /** * @inheritDoc */ - protected function preUpdate($itemData, $createIfAbsent = false) + protected function preUpdate($itemData) { $this->triggerEvent('onPreUpdate', ['itemData' => $itemData]); - return parent::preUpdate($itemData, $createIfAbsent); + return parent::preUpdate($itemData); } /** * @inheritDoc */ - protected function postUpdate($result, $itemData, $createIfAbsent) + protected function postUpdate($result, $itemData) { $this->triggerEvent('onPostUpdate', ['itemData' => $itemData, 'result' => $result]); - return parent::postUpdate($result, $itemData, $createIfAbsent); + return parent::postUpdate($result, $itemData); } /** @@ -130,26 +130,6 @@ protected function postDelete($result, $id) return parent::postDelete($result, $id); } - /** - * @inheritDoc - */ - protected function preDeleteAll() - { - $this->triggerEvent('onPreDeleteAll'); - - parent::preDeleteAll(); - } - - /** - * @inheritDoc - */ - protected function postDeleteAll($result) - { - $this->triggerEvent('onPostDeleteAll', ['result' => $result]); - - return parent::postDeleteAll($result); - } - /** * @inheritDoc */ diff --git a/src/DataStore/src/DataStore/Aspect/Factory/AspectAbstractFactory.php b/src/DataStore/src/DataStore/Aspect/Factory/AspectAbstractFactory.php index 32f53713..f5eaf128 100644 --- a/src/DataStore/src/DataStore/Aspect/Factory/AspectAbstractFactory.php +++ b/src/DataStore/src/DataStore/Aspect/Factory/AspectAbstractFactory.php @@ -52,8 +52,6 @@ class AspectAbstractFactory extends DataStoreAbstractFactory 'onPostUpdate', 'onPreDelete', 'onPostDelete', - 'onPreDeleteAll', - 'onPostDeleteAll', 'onPreGetIdentifier', 'onPostGetIdentifier', 'onPreRead', diff --git a/src/DataStore/src/DataStore/Cacheable.php b/src/DataStore/src/DataStore/Cacheable.php index cd038856..b0fc96a9 100644 --- a/src/DataStore/src/DataStore/Cacheable.php +++ b/src/DataStore/src/DataStore/Cacheable.php @@ -9,17 +9,17 @@ use Xiag\Rql\Parser\Query; use rollun\datastore\DataSource\DataSourceInterface; use rollun\datastore\DataStore\Interfaces\RefreshableInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; -class Cacheable implements DataStoresInterface, RefreshableInterface +class Cacheable implements DataStoreInterface, RefreshableInterface { - /** @var DataStoresInterface */ + /** @var DataStoreInterface */ protected $cashStore; /** @var DataSourceInterface */ protected $dataSource; - public function __construct(DataSourceInterface $dataSource, DataStoresInterface $cashStore = null) + public function __construct(DataSourceInterface $dataSource, DataStoreInterface $cashStore = null) { $this->dataSource = $dataSource; @@ -87,12 +87,12 @@ public function has($id) public function refresh() { - $this->cashStore->deleteAll(); + $this->cashStore->queriedDelete(new Query()); $all = $this->dataSource->getAll(); if ($all instanceof \Traversable or is_array($all)) { foreach ($all as $item) { - $this->cashStore->create($item, true); + $this->cashStore->rewrite($item); } } else { throw new DataStoreException("Not return data by DataSource"); @@ -120,27 +120,21 @@ public function count() * You can get item "id" for created item us result this function. * * If $item["id"] !== null, item set with that id. - * If item with same id already exist - method will throw exception, - * but if $rewriteIfExist = true item will be rewrites.
+ * If item with same id already exist - method will throw exception
* * If $item["id"] is not set or $item["id"]===null, * item will be insert with autoincrement PrimaryKey.
* * @param array $itemData associated array with or without PrimaryKey - * @param bool $rewriteIfExist * @return int|null|string "id" for created item or null if item wasn't created * @throws DataStoreException */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { - if ($rewriteIfExist) { - trigger_error("Option 'rewriteIfExist' is no more use", E_USER_DEPRECATED); - } - if (method_exists($this->dataSource, "create")) { - return $this->dataSource->create($itemData, $rewriteIfExist); + return $this->dataSource->create($itemData); } else { - throw new DataStoreException("Refreshable don't haw method create"); + throw new DataStoreException("Refreshable doesn't have method create"); } } @@ -153,25 +147,19 @@ public function create($itemData, $rewriteIfExist = false) *
* If $item["id"] isn't set - method will throw exception.
*
- * If item with PrimaryKey == $item["id"] is absent - method will throw exception,
- * but if $createIfAbsent = true item will be created and method return inserted item
+ * If item with PrimaryKey == $item["id"] is absent - method will throw exception
*
* * @param array $itemData associated array with PrimaryKey - * @param bool $createIfAbsent * @return array updated item or inserted item * @throws DataStoreException */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { - if ($createIfAbsent) { - trigger_error("Option 'createIfAbsent' is no more use.", E_USER_DEPRECATED); - } - if (method_exists($this->dataSource, "update")) { - return $this->dataSource->update($itemData, $createIfAbsent); + return $this->dataSource->update($itemData); } else { - throw new DataStoreException("Refreshable don't haw method update"); + throw new DataStoreException("Refreshable doesn't have method update"); } } @@ -187,21 +175,7 @@ public function publicdelete($id) if (method_exists($this->dataSource, "delete")) { return $this->dataSource->delete($id); } else { - throw new DataStoreException("Refreshable don't haw method delete"); - } - } - - /** - * Delete all Items. - * @return int number of deleted items or null if object doesn't support it - * @throws DataStoreException - */ - public function deleteAll() - { - if (method_exists($this->dataSource, "deleteAll")) { - return $this->dataSource->deleteAll(); - } else { - throw new DataStoreException("Refreshable don't haw method deleteAll"); + throw new DataStoreException("Refreshable doesn't have method delete"); } } @@ -217,7 +191,52 @@ public function delete($id) if (method_exists($this->dataSource, "delete")) { return $this->dataSource->delete($id); } else { - throw new DataStoreException("Refreshable don't haw method delete"); + throw new DataStoreException("Refreshable doesn't have method delete"); + } + } + + public function multiCreate($records) + { + if (method_exists($this->dataSource, "multiCreate")) { + return $this->dataSource->multiCreate($records); + } else { + throw new DataStoreException("Refreshable doesn't have method multiCreate"); + } + } + + public function multiUpdate($records) + { + if (method_exists($this->dataSource, "multiUpdate")) { + return $this->dataSource->multiUpdate($records); + } else { + throw new DataStoreException("Refreshable doesn't have method multiUpdate"); + } + } + + public function queriedUpdate($record, Query $query) + { + if (method_exists($this->dataSource, "queriedUpdate")) { + return $this->dataSource->queriedUpdate($record, $query); + } else { + throw new DataStoreException("Refreshable doesn't have method queriedUpdate"); + } + } + + public function rewrite($record) + { + if (method_exists($this->dataSource, "rewrite")) { + return $this->dataSource->rewrite($record); + } else { + throw new DataStoreException("Refreshable doesn't have method rewrite"); + } + } + + public function queriedDelete(Query $query) + { + if (method_exists($this->dataSource, "queriedDelete")) { + return $this->dataSource->queriedDelete($query); + } else { + throw new DataStoreException("Refreshable doesn't have method queriedDelete"); } } } diff --git a/src/DataStore/src/DataStore/DataStoreAbstract.php b/src/DataStore/src/DataStore/DataStoreAbstract.php index 22a8a286..e700a77c 100644 --- a/src/DataStore/src/DataStore/DataStoreAbstract.php +++ b/src/DataStore/src/DataStore/DataStoreAbstract.php @@ -8,7 +8,6 @@ use rollun\datastore\DataStore\ConditionBuilder\ConditionBuilderAbstract; use rollun\datastore\DataStore\Interfaces\DataStoreInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; use rollun\datastore\DataStore\Iterators\DataStoreIterator; use rollun\datastore\Rql\Node\AggregateFunctionNode; use rollun\datastore\Rql\Node\AggregateSelectNode; @@ -461,27 +460,6 @@ public function queriedUpdate($record, Query $query) return $updatedIds; } - /** - * {@inheritdoc} - */ - public function deleteAll() - { - $keys = $this->getKeys(); - $deletedItemsNumber = 0; - - foreach ($keys as $id) { - $deletedItems = $this->delete($id); - - if (is_null($deletedItems)) { - return null; - } - - $deletedItemsNumber++; - } - - return $deletedItemsNumber; - } - /** * Return array of keys or empty array * @@ -516,13 +494,13 @@ public function rewrite($record) throw new DataStoreException("Identifier is required for 'rewrite' action"); } - $rewriteIfExist = false; - if ($this->has($record[$this->getIdentifier()])) { - $rewriteIfExist = true; + $result = $this->update($record); + } else { + $result = $this->create($record); } - return $this->create($record, $rewriteIfExist); + return $result; } /** diff --git a/src/DataStore/src/DataStore/DataStorePluginManager.php b/src/DataStore/src/DataStore/DataStorePluginManager.php index bf03b89d..2775b17c 100644 --- a/src/DataStore/src/DataStore/DataStorePluginManager.php +++ b/src/DataStore/src/DataStore/DataStorePluginManager.php @@ -6,10 +6,10 @@ namespace rollun\datastore\DataStore; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use Zend\ServiceManager\AbstractPluginManager; class DataStorePluginManager extends AbstractPluginManager { - protected $instanceOf = DataStoresInterface::class; + protected $instanceOf = DataStoreInterface::class; } diff --git a/src/DataStore/src/DataStore/Factory/CacheableAbstractFactory.php b/src/DataStore/src/DataStore/Factory/CacheableAbstractFactory.php index f108aa25..1bcf3e3d 100644 --- a/src/DataStore/src/DataStore/Factory/CacheableAbstractFactory.php +++ b/src/DataStore/src/DataStore/Factory/CacheableAbstractFactory.php @@ -59,7 +59,7 @@ public function canCreate(ContainerInterface $container, $requestedName) * @param ContainerInterface $container * @param string $requestedName * @param array|null $options - * @return Cacheable|\rollun\datastore\DataStore\Interfaces\DataStoresInterface + * @return Cacheable|\rollun\datastore\DataStore\Interfaces\DataStoreInterface * @throws DataStoreException */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) diff --git a/src/DataStore/src/DataStore/Factory/MemoryAbstractFactory.php b/src/DataStore/src/DataStore/Factory/MemoryAbstractFactory.php index 258a446d..d327279e 100644 --- a/src/DataStore/src/DataStore/Factory/MemoryAbstractFactory.php +++ b/src/DataStore/src/DataStore/Factory/MemoryAbstractFactory.php @@ -8,7 +8,6 @@ use Interop\Container\ContainerInterface; use rollun\datastore\DataStore\DataStoreException; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; use rollun\datastore\DataStore\Memory; /** diff --git a/src/DataStore/src/DataStore/Interfaces/DataStoreInterface.php b/src/DataStore/src/DataStore/Interfaces/DataStoreInterface.php index 72eac911..3056dce3 100644 --- a/src/DataStore/src/DataStore/Interfaces/DataStoreInterface.php +++ b/src/DataStore/src/DataStore/Interfaces/DataStoreInterface.php @@ -15,7 +15,7 @@ * All records in data store should have identifiers (field with unique value). * Expect that any record, that retrieved from data store has identifier. * - * Interface DataStoresInterface + * Interface DataStoreInterface * @package rollun\datastore\DataStore\Interfaces */ interface DataStoreInterface extends ReadInterface diff --git a/src/DataStore/src/DataStore/Interfaces/DbTableInterface.php b/src/DataStore/src/DataStore/Interfaces/DbTableInterface.php index 454e029e..e90755c8 100644 --- a/src/DataStore/src/DataStore/Interfaces/DbTableInterface.php +++ b/src/DataStore/src/DataStore/Interfaces/DbTableInterface.php @@ -12,7 +12,7 @@ * Interface DbTableInterface * @package rollun\datastore\DataStore\Interfaces */ -interface DbTableInterface extends DataStoresInterface +interface DbTableInterface extends DataStoreInterface { /** * @return TableGateway diff --git a/src/DataStore/src/DataStore/Traits/DateTimeTrait.php b/src/DataStore/src/DataStore/Traits/DateTimeTrait.php index 06a974f5..1a2969d0 100644 --- a/src/DataStore/src/DataStore/Traits/DateTimeTrait.php +++ b/src/DataStore/src/DataStore/Traits/DateTimeTrait.php @@ -19,33 +19,31 @@ trait DateTimeTrait /** * @param $itemData - * @param bool $rewriteIfExist * @return mixed * @throws \Exception */ - public function insertItem($itemData, $rewriteIfExist = false) + public function insertItem($itemData) { if (empty($itemData[DateTimeInterface::FIELD_CREATED_AT])) { $date = new \DateTime('now', new \DateTimeZone('UTC')); $itemData[DateTimeInterface::FIELD_CREATED_AT] = $date->format($this->dateTimeFormat); } - return parent::insertItem($itemData, $rewriteIfExist); + return parent::insertItem($itemData); } /** * @param $itemData - * @param bool $createIfAbsent * @return mixed * @throws \Exception */ - public function updateItem($itemData, $createIfAbsent = false) + public function updateItem($itemData) { if (empty($itemData[DateTimeInterface::FIELD_UPDATED_AT])) { $date = new \DateTime('now', new \DateTimeZone('UTC')); $itemData[DateTimeInterface::FIELD_UPDATED_AT] = $date->format($this->dateTimeFormat); } - return parent::updateItem($itemData, $createIfAbsent); + return parent::updateItem($itemData); } } \ No newline at end of file diff --git a/src/DataStore/src/DataStore/Traits/NoSupportCreateTrait.php b/src/DataStore/src/DataStore/Traits/NoSupportCreateTrait.php index fa9e1f8e..f6faf927 100644 --- a/src/DataStore/src/DataStore/Traits/NoSupportCreateTrait.php +++ b/src/DataStore/src/DataStore/Traits/NoSupportCreateTrait.php @@ -18,10 +18,9 @@ trait NoSupportCreateTrait { /** * @param $itemData - * @param bool $rewriteIfExist * @throws DataStoreException */ - public function create($itemData, $rewriteIfExist = false) + public function create($itemData) { trigger_error(NoSupportCreateTrait::class . ' trait is deprecated', E_USER_DEPRECATED); diff --git a/src/DataStore/src/DataStore/Traits/NoSupportUpdateTrait.php b/src/DataStore/src/DataStore/Traits/NoSupportUpdateTrait.php index fbbf1dae..54e837b8 100644 --- a/src/DataStore/src/DataStore/Traits/NoSupportUpdateTrait.php +++ b/src/DataStore/src/DataStore/Traits/NoSupportUpdateTrait.php @@ -18,10 +18,9 @@ trait NoSupportUpdateTrait { /** * @param $itemData - * @param bool $createIfAbsent * @throws DataStoreException */ - public function update($itemData, $createIfAbsent = false) + public function update($itemData) { trigger_error(NoSupportUpdateTrait::class . ' trait is deprecated', E_USER_DEPRECATED); diff --git a/src/DataStore/src/DataStore/Traits/PrepareFieldsTrait.php b/src/DataStore/src/DataStore/Traits/PrepareFieldsTrait.php index d8d3b811..266d946a 100644 --- a/src/DataStore/src/DataStore/Traits/PrepareFieldsTrait.php +++ b/src/DataStore/src/DataStore/Traits/PrepareFieldsTrait.php @@ -19,11 +19,11 @@ trait PrepareFieldsTrait * * @throws \Throwable */ - public function createData($itemData, $rewriteIfExist = false) + public function createData($itemData) { $itemData = $this->prepareData($itemData); - return $this->callAttempts(function() use ($itemData, $rewriteIfExist) { - return $this->create($itemData, $rewriteIfExist); + return $this->callAttempts(function() use ($itemData) { + return $this->create($itemData); }); } @@ -48,17 +48,16 @@ public function insertData($itemData) * Пытается сделать это определенное количество раз, если с первого раза не получается * * @param $itemData - * @param bool $createIfAbsent * * @return array * * @throws \Throwable */ - public function updateData($itemData, $createIfAbsent = false) + public function updateData($itemData) { $itemData = $this->prepareData($itemData); - return $this->callAttempts(function() use ($itemData, $createIfAbsent){ - return $this->update($itemData, $createIfAbsent); + return $this->callAttempts(function() use ($itemData){ + return $this->update($itemData); }); } diff --git a/src/DataStore/src/Middleware/DataStoreAbstract.php b/src/DataStore/src/Middleware/DataStoreAbstract.php index 65de2147..228ca8a4 100644 --- a/src/DataStore/src/Middleware/DataStoreAbstract.php +++ b/src/DataStore/src/Middleware/DataStoreAbstract.php @@ -8,7 +8,6 @@ use Psr\Http\Server\MiddlewareInterface; use rollun\datastore\DataStore\Interfaces\DataStoreInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; /** * Abstract middleware with injected data store @@ -19,21 +18,17 @@ abstract class DataStoreAbstract implements MiddlewareInterface { /** - * @var DataStoresInterface|DataStoreInterface + * @var DataStoreInterface */ protected $dataStore; /** * DataStoreAbstract constructor. * - * @param DataStoresInterface|DataStoreInterface $dataStore + * @param DataStoreInterface $dataStore */ - public function __construct($dataStore) + public function __construct(DataStoreInterface $dataStore) { - if ($dataStore instanceof DataStoreInterface || $dataStore instanceof DataStoresInterface) { - $this->dataStore = $dataStore; - } else { - throw new \InvalidArgumentException("DataStore '$dataStore' should be instance of DataStoreInterface or DataStoresInterface"); - } + $this->dataStore = $dataStore; } } diff --git a/src/DataStore/src/Middleware/DataStoreRest.php b/src/DataStore/src/Middleware/DataStoreRest.php index b251ce17..459fce7d 100644 --- a/src/DataStore/src/Middleware/DataStoreRest.php +++ b/src/DataStore/src/Middleware/DataStoreRest.php @@ -10,7 +10,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\Middleware\Handler; use Zend\Stratigility\MiddlewarePipe; @@ -26,15 +26,15 @@ class DataStoreRest implements MiddlewareInterface protected $middlewarePipe; /** - * @var DataStoresInterface + * @var DataStoreInterface */ private $dataStore; /** * DataStoreRest constructor. - * @param DataStoresInterface $dataStore + * @param DataStoreInterface $dataStore */ - public function __construct(DataStoresInterface $dataStore) + public function __construct(DataStoreInterface $dataStore) { $this->middlewarePipe = new MiddlewarePipe(); $this->dataStore = $dataStore; diff --git a/src/DataStore/src/TableGateway/Factory/TableGatewayAbstractFactory.php b/src/DataStore/src/TableGateway/Factory/TableGatewayAbstractFactory.php index 84f02777..f81f14a5 100644 --- a/src/DataStore/src/TableGateway/Factory/TableGatewayAbstractFactory.php +++ b/src/DataStore/src/TableGateway/Factory/TableGatewayAbstractFactory.php @@ -93,7 +93,7 @@ protected function setDbAdapter(ContainerInterface $container, $requestedName) * @param ContainerInterface $container * @param string $requestedName * @param array|null $options - * @return \rollun\datastore\DataStore\Interfaces\DataStoresInterface|TableGateway + * @return \rollun\datastore\DataStore\Interfaces\DataStoreInterface|TableGateway */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { diff --git a/src/Repository/src/Factory/ModelRepositoryAbstractFactory.php b/src/Repository/src/Factory/ModelRepositoryAbstractFactory.php index 2b7f0626..17b14287 100644 --- a/src/Repository/src/Factory/ModelRepositoryAbstractFactory.php +++ b/src/Repository/src/Factory/ModelRepositoryAbstractFactory.php @@ -40,7 +40,7 @@ class ModelRepositoryAbstractFactory extends AbstractFactoryAbstract * @param string $requestedName * @param array|null $options * - * @return mixed|\rollun\datastore\DataStore\Interfaces\DataStoresInterface + * @return mixed|\rollun\datastore\DataStore\Interfaces\DataStoreInterface * * @throws \Exception */ diff --git a/src/Uploader/src/Iterator/DataStorePack.php b/src/Uploader/src/Iterator/DataStorePack.php index c01671b0..949ac98f 100644 --- a/src/Uploader/src/Iterator/DataStorePack.php +++ b/src/Uploader/src/Iterator/DataStorePack.php @@ -6,7 +6,7 @@ namespace rollun\uploader\Iterator; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use SeekableIterator; use Xiag\Rql\Parser\Node\LimitNode; use Xiag\Rql\Parser\Node\Query\ScalarOperator\GtNode; @@ -21,7 +21,7 @@ class DataStorePack implements SeekableIterator protected $limit; /** - * @var DataStoresInterface + * @var DataStoreInterface */ protected $dataStore; @@ -32,10 +32,10 @@ class DataStorePack implements SeekableIterator /** * DataStoreIterator constructor. - * @param DataStoresInterface $dataStore + * @param DataStoreInterface $dataStore * @param int $limit */ - public function __construct(DataStoresInterface $dataStore, $limit = 100) + public function __construct(DataStoreInterface $dataStore, $limit = 100) { $this->dataStore = $dataStore; $this->limit = $limit; diff --git a/src/Uploader/src/Uploader.php b/src/Uploader/src/Uploader.php index 5b487184..4ea3016c 100644 --- a/src/Uploader/src/Uploader.php +++ b/src/Uploader/src/Uploader.php @@ -6,7 +6,7 @@ namespace rollun\uploader; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use SeekableIterator; use Traversable; @@ -22,7 +22,7 @@ class Uploader protected $sourceDataIteratorAggregator; /** - * @var DataStoresInterface + * @var DataStoreInterface */ protected $destinationDataStore; @@ -34,11 +34,11 @@ class Uploader /** * Uploader constructor. * @param Traversable $sourceDataIteratorAggregator - * @param DataStoresInterface $destinationDataStore + * @param DataStoreInterface $destinationDataStore */ public function __construct( Traversable $sourceDataIteratorAggregator, - DataStoresInterface $destinationDataStore + DataStoreInterface $destinationDataStore ) { $this->sourceDataIteratorAggregator = $sourceDataIteratorAggregator; $this->destinationDataStore = $destinationDataStore; @@ -52,7 +52,7 @@ public function upload() foreach ($this->sourceDataIteratorAggregator as $key => $value) { $this->key = $key; - $this->destinationDataStore->create($value, true); + $this->destinationDataStore->rewrite($value); } } From 7cb2e797b54bab9de4fc93f72ac1de81a933912d Mon Sep 17 00:00:00 2001 From: Maria Date: Mon, 8 Nov 2021 11:11:06 +0200 Subject: [PATCH 3/7] remove old datastore interface from tests --- .../Middleware/BaseMiddlewareTest.php | 6 +- .../DataStore/Middleware/DataStoreApiTest.php | 8 +-- .../Middleware/DataStoreRestTest.php | 6 +- .../DataStore/Middleware/DeterminatorTest.php | 6 +- .../Middleware/Handler/CreateHandlerTest.php | 4 +- .../Middleware/Handler/DeleteHandlerTest.php | 4 +- .../Middleware/Handler/QueryHandlerTest.php | 8 +-- .../Middleware/Handler/ReadHandlerTest.php | 4 +- .../Middleware/Handler/RefreshHandlerTest.php | 6 +- .../Middleware/Handler/UpdateHandlerTest.php | 4 +- .../DataStore/AutoIdGeneratorTraitTest.php | 4 +- .../DataStore/Aspect/AspectTypedTest.php | 30 +++++----- .../DataStore/DataStore/CacheableTest.php | 60 +++++++++---------- .../Uploader/Iterator/DataStorePackTest.php | 4 +- 14 files changed, 77 insertions(+), 77 deletions(-) diff --git a/test/functional/DataStore/Middleware/BaseMiddlewareTest.php b/test/functional/DataStore/Middleware/BaseMiddlewareTest.php index 044f0373..d6cc77b5 100644 --- a/test/functional/DataStore/Middleware/BaseMiddlewareTest.php +++ b/test/functional/DataStore/Middleware/BaseMiddlewareTest.php @@ -12,7 +12,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use Zend\Diactoros\Response; use Zend\Diactoros\Stream; @@ -64,11 +64,11 @@ protected function assertDelegateCall( /** * @param string $identifier - * @return \PHPUnit_Framework_MockObject_MockObject|DataStoresInterface + * @return \PHPUnit_Framework_MockObject_MockObject|DataStoreInterface */ protected function createDataStoreEmptyMock($identifier = 'id') { - $mockObject = $this->getMockBuilder(DataStoresInterface::class) + $mockObject = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); $mockObject->expects($this->any()) diff --git a/test/functional/DataStore/Middleware/DataStoreApiTest.php b/test/functional/DataStore/Middleware/DataStoreApiTest.php index d90f4b77..b8adcab7 100644 --- a/test/functional/DataStore/Middleware/DataStoreApiTest.php +++ b/test/functional/DataStore/Middleware/DataStoreApiTest.php @@ -16,7 +16,7 @@ use Psr\Http\Message\ResponseInterface; use rollun\datastore\DataStore\DataStoreException; use rollun\datastore\DataStore\DataStorePluginManager; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Interfaces\RefreshableInterface; use rollun\datastore\DataStore\Memory; use rollun\datastore\Middleware\DataStoreApi; @@ -30,7 +30,7 @@ class DataStoreApiTest extends BaseMiddlewareTest { /** - * @var DataStoresInterface + * @var DataStoreInterface */ protected $dataStore; @@ -199,8 +199,8 @@ public function testProcessFailWithNoDataStore() public function testConstruct() { - /** @var DataStoresInterface| $dataStoreMock */ - $dataStoreMock = $this->getMockBuilder(DataStoresInterface::class) + /** @var DataStoreInterface| $dataStoreMock */ + $dataStoreMock = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); $dataStorePluginManagerMock = $this->getMockBuilder(DataStorePluginManager::class) diff --git a/test/functional/DataStore/Middleware/DataStoreRestTest.php b/test/functional/DataStore/Middleware/DataStoreRestTest.php index 5af6d956..c275eae9 100644 --- a/test/functional/DataStore/Middleware/DataStoreRestTest.php +++ b/test/functional/DataStore/Middleware/DataStoreRestTest.php @@ -7,7 +7,7 @@ namespace rollun\test\functional\DataStore\Middleware; use PHPUnit\Framework\TestCase; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\Middleware\DataStoreRest; use rollun\datastore\Middleware\Handler; use SplQueue; @@ -18,8 +18,8 @@ class DataStoreRestTest extends TestCase { public function testConstruct() { - /** @var DataStoresInterface| $dataStoreMock */ - $dataStoreMock = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface| $dataStoreMock */ + $dataStoreMock = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $middlewarePipe = new MiddlewarePipe(); $middlewarePipe->pipe(new Handler\QueryHandler($dataStoreMock)); diff --git a/test/functional/DataStore/Middleware/DeterminatorTest.php b/test/functional/DataStore/Middleware/DeterminatorTest.php index 6536998a..7035ccd3 100644 --- a/test/functional/DataStore/Middleware/DeterminatorTest.php +++ b/test/functional/DataStore/Middleware/DeterminatorTest.php @@ -13,7 +13,7 @@ use rollun\datastore\DataStore\BaseDto; use rollun\datastore\DataStore\DataStorePluginManager; use rollun\datastore\DataStore\Formatter\IntFormatter; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Memory; use rollun\datastore\DataStore\Type\TypeInt; use rollun\datastore\DataStore\Type\TypeString; @@ -39,8 +39,8 @@ public function testProcessSuccess() ); $serviceName = 'dataStoreService'; - /** @var DataStoresInterface $dataStoreMock */ - $dataStoreMock = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface $dataStoreMock */ + $dataStoreMock = $this->getMockBuilder(DataStoreInterface::class)->getMock(); /** @var DataStorePluginManager|PHPUnit_Framework_MockObject_MockObject $dataStorePluginManagerMock */ $dataStorePluginManagerMock = $this->getMockBuilder(DataStorePluginManager::class) diff --git a/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php b/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php index a334a429..fbf818d5 100644 --- a/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php @@ -9,14 +9,14 @@ use Psr\Http\Message\UriInterface; use Psr\Http\Server\RequestHandlerInterface; use rollun\datastore\DataStore\DataStoreException; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\Middleware\Handler\CreateHandler; use rollun\datastore\Rql\RqlQuery; use Zend\Diactoros\ServerRequest; class CreateHandlerTest extends BaseHandlerTest { - protected function createObject(DataStoresInterface $dataStore = null) + protected function createObject(DataStoreInterface $dataStore = null) { return new CreateHandler(is_null($dataStore) ? $this->createDataStoreEmptyMock() : $dataStore); } diff --git a/test/functional/DataStore/Middleware/Handler/DeleteHandlerTest.php b/test/functional/DataStore/Middleware/Handler/DeleteHandlerTest.php index dd11f0ff..6fc1d469 100644 --- a/test/functional/DataStore/Middleware/Handler/DeleteHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/DeleteHandlerTest.php @@ -6,14 +6,14 @@ namespace rollun\test\functional\DataStore\Middleware\Handler; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\Middleware\Handler\DeleteHandler; use rollun\datastore\Rql\RqlQuery; use Zend\Diactoros\ServerRequest; class DeleteHandlerTest extends BaseHandlerTest { - protected function createObject(DataStoresInterface $dataStore = null) + protected function createObject(DataStoreInterface $dataStore = null) { return new DeleteHandler(is_null($dataStore) ? $this->createDataStoreEmptyMock() : $dataStore); } diff --git a/test/functional/DataStore/Middleware/Handler/QueryHandlerTest.php b/test/functional/DataStore/Middleware/Handler/QueryHandlerTest.php index 5d4142c9..23cd6a49 100644 --- a/test/functional/DataStore/Middleware/Handler/QueryHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/QueryHandlerTest.php @@ -7,7 +7,7 @@ namespace rollun\test\functional\DataStore\Middleware\Handler; use PHPUnit_Framework_MockObject_MockObject; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Interfaces\ReadInterface; use rollun\datastore\Middleware\Handler\QueryHandler; use rollun\datastore\Rql\Node\AggregateFunctionNode; @@ -18,7 +18,7 @@ class QueryHandlerTest extends BaseHandlerTest { - protected function createObject(DataStoresInterface $dataStore = null) + protected function createObject(DataStoreInterface $dataStore = null) { return new QueryHandler(is_null($dataStore) ? $this->createDataStoreEmptyMock() : $dataStore); } @@ -158,11 +158,11 @@ public function testProcessSuccessWithOutContentRange(RqlQuery $rqlQuery) /** * @param RqlQuery $rqlQuery - * @param DataStoresInterface|PHPUnit_Framework_MockObject_MockObject $dataStore + * @param DataStoreInterface|PHPUnit_Framework_MockObject_MockObject $dataStore * @param $items * @return string */ - protected function getContentRange(RqlQuery $rqlQuery, DataStoresInterface $dataStore, $items) + protected function getContentRange(RqlQuery $rqlQuery, DataStoreInterface $dataStore, $items) { $limitNode = $rqlQuery->getLimit(); $aggregateCount = [['count(id)' => 5]]; diff --git a/test/functional/DataStore/Middleware/Handler/ReadHandlerTest.php b/test/functional/DataStore/Middleware/Handler/ReadHandlerTest.php index 600d4eec..07d602aa 100644 --- a/test/functional/DataStore/Middleware/Handler/ReadHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/ReadHandlerTest.php @@ -6,14 +6,14 @@ namespace rollun\test\functional\DataStore\Middleware\Handler; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\Middleware\Handler\ReadHandler; use rollun\datastore\Rql\RqlQuery; use Zend\Diactoros\ServerRequest; class ReadHandlerTest extends BaseHandlerTest { - protected function createObject(DataStoresInterface $dataStore = null) + protected function createObject(DataStoreInterface $dataStore = null) { return new ReadHandler(is_null($dataStore) ? $this->createDataStoreEmptyMock() : $dataStore); } diff --git a/test/functional/DataStore/Middleware/Handler/RefreshHandlerTest.php b/test/functional/DataStore/Middleware/Handler/RefreshHandlerTest.php index 85c67ed3..9d8b3f52 100644 --- a/test/functional/DataStore/Middleware/Handler/RefreshHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/RefreshHandlerTest.php @@ -8,7 +8,7 @@ use Interop\Http\ServerMiddleware\DelegateInterface; use Psr\Http\Server\RequestHandlerInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Interfaces\RefreshableInterface; use rollun\datastore\DataStore\Memory; use rollun\datastore\Middleware\Handler\RefreshHandler; @@ -18,7 +18,7 @@ class RefreshHandlerTest extends BaseHandlerTest { - protected function createObject(DataStoresInterface $dataStore = null) + protected function createObject(DataStoreInterface $dataStore = null) { return new RefreshHandler(is_null($dataStore) ? $this->createDataStoreEmptyMock() : $dataStore); } @@ -98,7 +98,7 @@ public function testProcessFail() } } -interface TestInterface extends DataStoresInterface, RefreshableInterface +interface TestInterface extends DataStoreInterface, RefreshableInterface { } diff --git a/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php b/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php index b9229b65..f7bacc37 100644 --- a/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php @@ -6,14 +6,14 @@ namespace rollun\test\functional\DataStore\Middleware\Handler; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\Middleware\Handler\UpdateHandler; use rollun\datastore\Rql\RqlQuery; use Zend\Diactoros\ServerRequest; class UpdateHandlerTest extends BaseHandlerTest { - protected function createObject(DataStoresInterface $dataStore = null) + protected function createObject(DataStoreInterface $dataStore = null) { return new UpdateHandler(is_null($dataStore) ? $this->createDataStoreEmptyMock() : $dataStore); } diff --git a/test/old/DataStore/AutoIdGeneratorTraitTest.php b/test/old/DataStore/AutoIdGeneratorTraitTest.php index 827afe7b..6eec5d45 100644 --- a/test/old/DataStore/AutoIdGeneratorTraitTest.php +++ b/test/old/DataStore/AutoIdGeneratorTraitTest.php @@ -7,7 +7,7 @@ namespace rollun\test\old\DataStore; use PHPUnit\Framework\TestCase; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Memory; use rollun\datastore\DataStore\Traits\AutoIdGeneratorTrait; use rollun\utils\IdGenerator; @@ -15,7 +15,7 @@ class AutoIdGeneratorTraitTest extends TestCase { /** - * @var DataStoresInterface with AutoIdGeneratorTrait + * @var DataStoreInterface with AutoIdGeneratorTrait */ protected $object; diff --git a/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php b/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php index 436d99a4..36edafbe 100644 --- a/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php +++ b/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php @@ -12,7 +12,7 @@ use rollun\datastore\DataStore\Aspect\AspectTyped; use rollun\datastore\DataStore\BaseDto; use rollun\datastore\DataStore\Formatter\FormatterInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Type\TypeInt; use rollun\datastore\DataStore\Type\TypeInterface; use rollun\datastore\DataStore\Type\TypeString; @@ -30,8 +30,8 @@ public function testConstructSuccess() ]; $dtoClassName = BaseDto::class; - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); $object = new AspectTyped($dataStore, $scheme, $dtoClassName); @@ -51,8 +51,8 @@ public function testConstructFailSchemeWithInvalidType() ]; $dtoClassName = BaseDto::class; - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); new AspectTyped($dataStore, $scheme, $dtoClassName); } @@ -68,8 +68,8 @@ public function testConstructFailSchemeWithInvalidFormatter() ]; $dtoClassName = BaseDto::class; - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); new AspectTyped($dataStore, $scheme, $dtoClassName); } @@ -88,8 +88,8 @@ public function testConstructFailDtoClassName() { }); - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); new AspectTyped($dataStore, $scheme, $dtoClassName); } @@ -107,8 +107,8 @@ public function testCreateAndUpdate() ], ]; - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); $dtoCreate = new UserDto(['id' => new TypeInt(1), 'name' => new TypeString('foo')]); @@ -147,8 +147,8 @@ public function testQuery() ], ]; - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); $dataStore->method('query') @@ -183,8 +183,8 @@ public function testGetScheme() ], ]; - /** @var PHPUnit_Framework_MockObject_MockObject|DataStoresInterface $dataStore */ - $dataStore = $this->getMockBuilder(DataStoresInterface::class) + /** @var PHPUnit_Framework_MockObject_MockObject|DataStoreInterface $dataStore */ + $dataStore = $this->getMockBuilder(DataStoreInterface::class) ->getMock(); $object = new AspectTyped($dataStore, $scheme, UserDto::class); $this->assertEquals($object->getScheme(), [ diff --git a/test/unit/DataStore/DataStore/CacheableTest.php b/test/unit/DataStore/DataStore/CacheableTest.php index de40cb71..5665c79c 100644 --- a/test/unit/DataStore/DataStore/CacheableTest.php +++ b/test/unit/DataStore/DataStore/CacheableTest.php @@ -11,13 +11,13 @@ use rollun\datastore\DataStore\Cacheable; use rollun\datastore\DataStore\DataStoreException; use rollun\datastore\DataSource\DataSourceInterface; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Memory; use rollun\datastore\Rql\RqlQuery; class CacheableTest extends TestCase { - public function createObject(DataSourceInterface $dataSource, DataStoresInterface $cashStore = null) + public function createObject(DataSourceInterface $dataSource, DataStoreInterface $cashStore = null) { return new Cacheable($dataSource, $cashStore); } @@ -29,8 +29,8 @@ public function testQuery() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(DataSourceInterface::class)->getMock(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $cashStore->expects($this->once())->method('query')->with($rqlQuery); $this->createObject($dataSource, $cashStore)->query($rqlQuery); @@ -43,8 +43,8 @@ public function testRead() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(DataSourceInterface::class)->getMock(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $cashStore->expects($this->once())->method('read')->with($id); $this->createObject($dataSource, $cashStore)->read($id); @@ -55,8 +55,8 @@ public function testGetIterator() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(DataSourceInterface::class)->getMock(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $cashStore->expects($this->once())->method('getIterator'); $this->createObject($dataSource, $cashStore)->getIterator(); @@ -67,8 +67,8 @@ public function testGetIdentifier() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(DataSourceInterface::class)->getMock(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $cashStore->expects($this->once())->method('getIdentifier'); $this->createObject($dataSource, $cashStore)->getIdentifier(); @@ -81,8 +81,8 @@ public function testHas() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(DataSourceInterface::class)->getMock(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $cashStore->expects($this->once())->method('has')->with($id); $this->createObject($dataSource, $cashStore)->has($id); @@ -93,8 +93,8 @@ public function testCount() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(DataSourceInterface::class)->getMock(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $cashStore->expects($this->once())->method('count'); $this->createObject($dataSource, $cashStore)->count(); @@ -108,8 +108,8 @@ public function testCreateSuccess() ]; $dataSource = new Foo(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->assertEquals($item, $this->createObject($dataSource, $cashStore)->create($item)); } @@ -123,8 +123,8 @@ public function testCreateFail() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = new Boo(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->create($items); } @@ -137,8 +137,8 @@ public function testUpdateSuccess() $dataSource = $this->getMockBuilder(Foo::class)->getMock(); $dataSource->expects($this->once())->method('update')->with($items, 1); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->update($items, 1); } @@ -152,8 +152,8 @@ public function testUpdateFail() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = new Boo(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->update($items); } @@ -165,8 +165,8 @@ public function testDeleteAllSuccess() $dataSource = $this->getMockBuilder(Foo::class)->getMock(); $dataSource->expects($this->once())->method('deleteAll'); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->deleteAll(); } @@ -179,8 +179,8 @@ public function testDeleteAllFail() /** @var DataSourceInterface|DataStoreException|MockObject $dataSource */ $dataSource = new Boo(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->deleteAll(); } @@ -193,8 +193,8 @@ public function testDeleteSuccess() $dataSource = $this->getMockBuilder(Foo::class)->getMock(); $dataSource->expects($this->once())->method('delete')->with($id); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->delete($id); } @@ -208,8 +208,8 @@ public function testDeleteFail() /** @var DataSourceInterface|DataStoreException|MockObject $dataSource */ $dataSource = new Boo(); - /** @var DataStoresInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoresInterface::class)->getMock(); + /** @var DataStoreInterface|MockObject $cashStore */ + $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); $this->createObject($dataSource, $cashStore)->delete($id); } diff --git a/test/unit/Uploader/Iterator/DataStorePackTest.php b/test/unit/Uploader/Iterator/DataStorePackTest.php index adfddb3b..ec341a33 100644 --- a/test/unit/Uploader/Iterator/DataStorePackTest.php +++ b/test/unit/Uploader/Iterator/DataStorePackTest.php @@ -2,7 +2,7 @@ namespace rollun\test\uploader\Uploader; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Memory; use rollun\uploader\Iterator\DataStorePack as DataStorePackIterator; use PHPUnit\Framework\TestCase; @@ -13,7 +13,7 @@ class DataStorePackTest extends TestCase /** @var DataStorePackIterator */ protected $object; - /** @var DataStoresInterface */ + /** @var DataStoreInterface */ protected $dataStore; public function setUp() From 9ba74c16e9b4ab7ccff218b43250e5a7d8608022 Mon Sep 17 00:00:00 2001 From: Maria Date: Mon, 8 Nov 2021 17:27:48 +0200 Subject: [PATCH 4/7] fix tests --- .../DataStore/DataStore/DbTableTest.php | 2 +- .../DataStore/BaseDataStoreTest.php | 20 ++-------- .../DataStore/Aspect/AspectTypedTest.php | 4 +- .../DataStore/DataStore/CacheableTest.php | 37 +++---------------- test/unit/DataStore/DataStore/CsvBaseTest.php | 20 ++-------- .../DataStore/DataStore/HttpClientTest.php | 8 ++-- test/unit/DataStore/DataStore/MemoryTest.php | 3 +- 7 files changed, 22 insertions(+), 72 deletions(-) diff --git a/test/functional/DataStore/DataStore/DbTableTest.php b/test/functional/DataStore/DataStore/DbTableTest.php index 301bf010..89ca6a9e 100644 --- a/test/functional/DataStore/DataStore/DbTableTest.php +++ b/test/functional/DataStore/DataStore/DbTableTest.php @@ -207,7 +207,7 @@ public function testDeleteAll() $this->create($itemData2); $object = $this->createObject(); - $object->deleteAll(); + $object->queriedDelete(new Query()); $this->assertEquals($this->read($itemData1['id']), null); $this->assertEquals($this->read($itemData2['id']), null); } diff --git a/test/intagration/DataStore/BaseDataStoreTest.php b/test/intagration/DataStore/BaseDataStoreTest.php index 8e4cb8a7..746b83cd 100644 --- a/test/intagration/DataStore/BaseDataStoreTest.php +++ b/test/intagration/DataStore/BaseDataStoreTest.php @@ -20,6 +20,7 @@ use Xiag\Rql\Parser\Node\Query\ScalarOperator\GeNode; use Xiag\Rql\Parser\Node\Query\ScalarOperator\LeNode; use Xiag\Rql\Parser\Node\SelectNode; +use Xiag\Rql\Parser\Query; abstract class BaseDataStoreTest extends TestCase { @@ -156,7 +157,7 @@ public function testCreateWithRewrite() ]; $object->create($item1); - $object->create($item2, 1); + $object->rewrite($item2); $this->assertEquals($item2, $object->read(1)); } @@ -174,19 +175,6 @@ public function testUpdateItemDoesNotExistException() $object->update($item); } - public function testUpdateWithCreate() - { - $object = $this->createObject(); - - $item = [ - $object->getIdentifier() => $this->identifierToType(1), - 'name' => 'name', - 'surname' => 'surname', - ]; - - $this->assertEquals($item, $object->update($item, 1)); - } - public function testQueryCombineWhereClauseSuccess() { $object = $this->createObject(); @@ -523,7 +511,7 @@ public function testDeleteSuccess() ->delete(1)); } - public function testCountSuccess() + public function testDeleteAll() { $object = $this->createObject(); $count = 5; @@ -538,7 +526,7 @@ public function testCountSuccess() $this->assertTrue($object instanceof \Countable); $this->assertEquals($object->count(), $count); - $object->deleteAll(); + $object->queriedDelete(new Query()); $this->assertEquals($object->count(), 0); } diff --git a/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php b/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php index 36edafbe..551a9ca3 100644 --- a/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php +++ b/test/unit/DataStore/DataStore/Aspect/AspectTypedTest.php @@ -122,11 +122,11 @@ public function testCreateAndUpdate() 'name' => 'bar', ]; $dataStore->method('create') - ->with($createItem, false) + ->with($createItem) ->willReturn($createItem); $dataStore->method('update') - ->with($updateItem, false) + ->with($updateItem) ->willReturn($updateItem); $object = new AspectTyped($dataStore, $scheme, UserDto::class); diff --git a/test/unit/DataStore/DataStore/CacheableTest.php b/test/unit/DataStore/DataStore/CacheableTest.php index 5665c79c..1f49aaa9 100644 --- a/test/unit/DataStore/DataStore/CacheableTest.php +++ b/test/unit/DataStore/DataStore/CacheableTest.php @@ -117,7 +117,7 @@ public function testCreateSuccess() public function testCreateFail() { $this->expectException(DataStoreException::class); - $this->expectExceptionMessage("Refreshable don't haw method create"); + $this->expectExceptionMessage("Refreshable doesn't have method create"); $items = []; /** @var DataSourceInterface|MockObject $dataSource */ @@ -135,18 +135,18 @@ public function testUpdateSuccess() /** @var DataSourceInterface|MockObject $dataSource */ $dataSource = $this->getMockBuilder(Foo::class)->getMock(); - $dataSource->expects($this->once())->method('update')->with($items, 1); + $dataSource->expects($this->once())->method('update')->with($items); /** @var DataStoreInterface|MockObject $cashStore */ $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); - $this->createObject($dataSource, $cashStore)->update($items, 1); + $this->createObject($dataSource, $cashStore)->update($items); } public function testUpdateFail() { $this->expectException(DataStoreException::class); - $this->expectExceptionMessage("Refreshable don't haw method update"); + $this->expectExceptionMessage("Refreshable doesn't have method update"); $items = []; /** @var DataSourceInterface|MockObject $dataSource */ @@ -158,33 +158,6 @@ public function testUpdateFail() $this->createObject($dataSource, $cashStore)->update($items); } - public function testDeleteAllSuccess() - { - - /** @var DataSourceInterface|DataStoreException|MockObject $dataSource */ - $dataSource = $this->getMockBuilder(Foo::class)->getMock(); - $dataSource->expects($this->once())->method('deleteAll'); - - /** @var DataStoreInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); - - $this->createObject($dataSource, $cashStore)->deleteAll(); - } - - public function testDeleteAllFail() - { - $this->expectException(DataStoreException::class); - $this->expectExceptionMessage("Refreshable don't haw method deleteAll"); - - /** @var DataSourceInterface|DataStoreException|MockObject $dataSource */ - $dataSource = new Boo(); - - /** @var DataStoreInterface|MockObject $cashStore */ - $cashStore = $this->getMockBuilder(DataStoreInterface::class)->getMock(); - - $this->createObject($dataSource, $cashStore)->deleteAll(); - } - public function testDeleteSuccess() { $id = 1; @@ -203,7 +176,7 @@ public function testDeleteFail() { $id = 1; $this->expectException(DataStoreException::class); - $this->expectExceptionMessage("Refreshable don't haw method delete"); + $this->expectExceptionMessage("Refreshable doesn't have method delete"); /** @var DataSourceInterface|DataStoreException|MockObject $dataSource */ $dataSource = new Boo(); diff --git a/test/unit/DataStore/DataStore/CsvBaseTest.php b/test/unit/DataStore/DataStore/CsvBaseTest.php index 24f93459..84dee349 100644 --- a/test/unit/DataStore/DataStore/CsvBaseTest.php +++ b/test/unit/DataStore/DataStore/CsvBaseTest.php @@ -12,6 +12,7 @@ use rollun\datastore\DataStore\Iterators\CsvIterator; use rollun\datastore\Rql\RqlQuery; use Symfony\Component\Filesystem\LockHandler; +use Xiag\Rql\Parser\Query; class CsvBaseTest extends TestCase { @@ -71,7 +72,7 @@ public function testCreateFailItemExistAndWithoutOverwrite() $this->assertEquals($item, $this->read($item['id'])); } - public function testCreateFailItemExistAndWithOverwrite() + public function testCreateWithRewrite() { $item = [ 'id' => 1, @@ -85,7 +86,7 @@ public function testCreateFailItemExistAndWithOverwrite() ]); $object = $this->createObject(); - $object->create($item, 1); + $object->rewrite($item); $this->assertEquals($item, $this->read($item['id'])); } @@ -247,19 +248,6 @@ public function testUpdateFailItemDoesNotExistAndCreateIfAbsent() $this->assertEquals($item, $this->read($item['id'])); } - public function testUpdateNotAllItems() - { - $item = [ - 'id' => 1, - 'name' => 'name', - 'surname' => 'surname', - ]; - - $object = $this->createObject(); - $object->update($item, 1); - $this->assertEquals($item, $this->read($item['id'])); - } - public function testQueriedUpdateSuccess() { $object = $this->createObject(); @@ -368,7 +356,7 @@ public function testDeleteAll() } $this->createObject() - ->deleteAll(); + ->queriedDelete(new Query()); foreach ($range as $id) { $this->assertEquals($this->read($id), []); diff --git a/test/unit/DataStore/DataStore/HttpClientTest.php b/test/unit/DataStore/DataStore/HttpClientTest.php index 3a45c255..69bfd413 100644 --- a/test/unit/DataStore/DataStore/HttpClientTest.php +++ b/test/unit/DataStore/DataStore/HttpClientTest.php @@ -153,7 +153,7 @@ public function testCreateSuccessWithOverwrite() $object = $this->createObject($clientMock, $url); - $this->assertEquals($object->create($items, 1), $items); + $this->assertEquals($object->rewrite($items), $items); } public function testCreateFail() @@ -180,7 +180,7 @@ public function testCreateFail() $object = $this->createObject($clientMock, $url); - $object->create($items, 1); + $object->create($items); } public function testUpdateSuccess() @@ -524,7 +524,7 @@ public function testException() $object = $this->createObject($clientMock, $url); - $object->create($items, 1); + $object->create($items); } public function testExceptionWithRedirect() @@ -581,7 +581,7 @@ public function testExceptionWithRedirect() $object = $this->createObject($clientMock, $url); - $object->create($items, 1); + $object->create($items); } public function testHeaderIdentifier() diff --git a/test/unit/DataStore/DataStore/MemoryTest.php b/test/unit/DataStore/DataStore/MemoryTest.php index fba06752..5ee1d14b 100644 --- a/test/unit/DataStore/DataStore/MemoryTest.php +++ b/test/unit/DataStore/DataStore/MemoryTest.php @@ -12,6 +12,7 @@ use rollun\datastore\DataStore\Memory; use rollun\datastore\Rql\RqlParser; use rollun\datastore\Rql\RqlQuery; +use Xiag\Rql\Parser\Query; class MemoryTest extends TestCase { @@ -395,7 +396,7 @@ public function testDeleteAll() ] ] ); - $object->deleteAll(); + $object->queriedDelete(new Query()); $this->assertAttributeEquals([], 'items', $object); } From 07a797bb7e5c9910cdda4ef5671bb5e26e66d268 Mon Sep 17 00:00:00 2001 From: Maria Date: Tue, 30 Nov 2021 16:28:50 +0200 Subject: [PATCH 5/7] add rewrite to DbTable, HttpClient and DataStoreAbstract --- .../src/DataStore/DataStoreAbstract.php | 10 +++---- src/DataStore/src/DataStore/DbTable.php | 29 +++++++++++++++---- src/DataStore/src/DataStore/HttpClient.php | 29 ++++++++++++++++++- .../src/Middleware/Handler/CreateHandler.php | 7 ++++- .../src/Middleware/Handler/UpdateHandler.php | 15 ++-------- 5 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/DataStore/src/DataStore/DataStoreAbstract.php b/src/DataStore/src/DataStore/DataStoreAbstract.php index e700a77c..224ea69e 100644 --- a/src/DataStore/src/DataStore/DataStoreAbstract.php +++ b/src/DataStore/src/DataStore/DataStoreAbstract.php @@ -494,13 +494,13 @@ public function rewrite($record) throw new DataStoreException("Identifier is required for 'rewrite' action"); } - if ($this->has($record[$this->getIdentifier()])) { - $result = $this->update($record); - } else { - $result = $this->create($record); + $id = $record[$this->getIdentifier()]; + + if ($this->has($id)) { + $this->delete($id); } - return $result; + return $this->create($record); } /** diff --git a/src/DataStore/src/DataStore/DbTable.php b/src/DataStore/src/DataStore/DbTable.php index c69f6492..304b41b6 100644 --- a/src/DataStore/src/DataStore/DbTable.php +++ b/src/DataStore/src/DataStore/DbTable.php @@ -90,14 +90,9 @@ protected function getSqlQueryBuilder() */ public function create($itemData) { - $adapter = $this->dbTable->getAdapter(); - $adapter->getDriver()->getConnection()->beginTransaction(); - try { $insertedItem = $this->insertItem($itemData); - $adapter->getDriver()->getConnection()->commit(); } catch (\Throwable $e) { - $adapter->getDriver()->getConnection()->rollback(); $logContext = [ self::LOG_METHOD => __METHOD__, self::LOG_TABLE => $this->dbTable->getTable(), @@ -174,6 +169,30 @@ public function update($itemData) return $result; } + public function rewrite($record) + { + $adapter = $this->dbTable->getAdapter(); + $adapter->getDriver()->getConnection()->beginTransaction(); + + try { + $result = parent::rewrite($record); + $adapter->getDriver()->getConnection()->commit(); + } catch (\Throwable $e) { + $adapter->getDriver()->getConnection()->rollback(); + $logContext = [ + self::LOG_METHOD => __METHOD__, + self::LOG_TABLE => $this->dbTable->getTable(), + self::LOG_REQUEST => $record, + self::LOG_ROLLBACK => true, + 'exception' => $e, + ]; + $this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed"); + throw new DataStoreException("[{$this->dbTable->getTable()}]Can't rewrite item. {$e->getMessage()}", 0, $e); + } + + return $result; + } + /** * {@inheritdoc} * diff --git a/src/DataStore/src/DataStore/HttpClient.php b/src/DataStore/src/DataStore/HttpClient.php index ddc9f6d4..cca83868 100644 --- a/src/DataStore/src/DataStore/HttpClient.php +++ b/src/DataStore/src/DataStore/HttpClient.php @@ -204,9 +204,10 @@ public function read($id) * * @param string $method ('GET', 'HEAD', 'POST', 'PUT', 'DELETE') * @param string $uri + * @param bool $ifMatch * @return Client */ - protected function initHttpClient(string $method, string $uri) + protected function initHttpClient(string $method, string $uri, $ifMatch = false) { $httpClient = clone $this->client; $httpClient->setUri($uri); @@ -217,6 +218,10 @@ protected function initHttpClient(string $method, string $uri) $headers['X-Life-Cycle-Token'] = $this->lifeCycleToken->serialize(); $headers['LifeCycleToken'] = $this->lifeCycleToken->serialize(); + if ($ifMatch) { + $headers['If-Match'] = '*'; + } + $httpClient->setHeaders($headers); if (isset($this->login) && isset($this->password)) { @@ -386,6 +391,28 @@ public function delete($id) return $result; } + /** + * {@inheritdoc} + */ + public function rewrite($record) + { + $client = $this->initHttpClient(Request::METHOD_POST, $this->url, true); + $json = Serializer::jsonSerialize($record); + $client->setRawBody($json); + $response = $client->send(); + + $this->checkResonseHeaderIdentifier($response); + + if ($response->isSuccess()) { + $result = Serializer::jsonUnserialize($response->getBody()); + } else { + $responseMessage = $this->createResponseMessage($this->url, Request::METHOD_POST, $response); + throw new DataStoreException("Can't rewrite item {$responseMessage}"); + } + + return $result; + } + protected function createResponseMessage($uri, $method, Response $response) { $messages = [ diff --git a/src/DataStore/src/Middleware/Handler/CreateHandler.php b/src/DataStore/src/Middleware/Handler/CreateHandler.php index f8c32658..21958e32 100644 --- a/src/DataStore/src/Middleware/Handler/CreateHandler.php +++ b/src/DataStore/src/Middleware/Handler/CreateHandler.php @@ -72,7 +72,12 @@ protected function handle(ServerRequestInterface $request): ResponseInterface $response = $response->withHeader('Location', $location); } - $newItem = $this->dataStore->create($row, $overwriteMode); + if ($overwriteMode) { + $newItem = $this->dataStore->rewrite($row); + } else { + $newItem = $this->dataStore->create($row); + } + $response = $response->withBody($this->createStream($newItem)); return $response; diff --git a/src/DataStore/src/Middleware/Handler/UpdateHandler.php b/src/DataStore/src/Middleware/Handler/UpdateHandler.php index 32e07187..3196ddc4 100644 --- a/src/DataStore/src/Middleware/Handler/UpdateHandler.php +++ b/src/DataStore/src/Middleware/Handler/UpdateHandler.php @@ -53,24 +53,15 @@ protected function handle(ServerRequestInterface $request): ResponseInterface $primaryKeyIdentifier = $this->dataStore->getIdentifier(); $item = $request->getParsedBody(); - if(!$primaryKeyValue && isset($item[$this->dataStore->getIdentifier()])) { - $primaryKeyValue = $item[$this->dataStore->getIdentifier()]; - } else { - $item = array_merge([$primaryKeyIdentifier => $primaryKeyValue], $item); + if ($primaryKeyValue && !isset($item[$primaryKeyIdentifier])) { + $item = array_merge([$primaryKeyIdentifier => $primaryKeyValue], $item); } - - $overwriteMode = $request->getAttribute('overwriteMode'); - $isItemExist = !empty($this->dataStore->read($primaryKeyValue)); - $newItem = $this->dataStore->update($item, $overwriteMode); + $newItem = $this->dataStore->update($item); $response = new Response(); $response = $response->withBody($this->createStream($newItem)); - if ($overwriteMode && !$isItemExist) { - $response = $response->withStatus(201); - } - return $response; } } From 6b1cb61f82b82fd51672de1de46772fdc6778571 Mon Sep 17 00:00:00 2001 From: Maria Date: Tue, 30 Nov 2021 16:32:27 +0200 Subject: [PATCH 6/7] fix tests --- .../Middleware/Handler/CreateHandlerTest.php | 9 +++--- .../Middleware/Handler/UpdateHandlerTest.php | 25 ++-------------- test/unit/DataStore/DataStore/CsvBaseTest.php | 2 +- .../DataStore/DataStore/HttpClientTest.php | 16 +++++----- .../DataStore/NoSupportTraitTest.php | 30 +++++++++++++++++-- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php b/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php index fbf818d5..d0abed38 100644 --- a/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/CreateHandlerTest.php @@ -93,7 +93,7 @@ public function testCanUpdateFailWithNotEmptyRqlQueryObject() $this->assertFalse($object->canHandle($request)); } - public function testProcessWithoutExistingPrimaryKeyAndRowExist() + public function testRewrite() { $item = [ 'id' => 1, @@ -115,8 +115,8 @@ public function testProcessWithoutExistingPrimaryKeyAndRowExist() ->willReturn($item); $dataStore->expects($this->once()) - ->method('create') - ->with($item, true) + ->method('rewrite') + ->with($item) ->willReturn($item); $object = $this->createObject($dataStore); @@ -152,7 +152,7 @@ public function testProcessWithoutOverwriteMode() $object->process($request, $delegateMock); } - public function testProcessWithoutExistingPrimaryKeyAndRowDoesNotExist() + public function testCreate() { $item = [ 'id' => 1, @@ -166,7 +166,6 @@ public function testProcessWithoutExistingPrimaryKeyAndRowDoesNotExist() $request = $request->withMethod('POST'); $request = $request->withParsedBody($item); $request = $request->withAttribute('primaryKeyValue', $item['id']); - $request = $request->withAttribute('overwriteMode', true); $dataStore = $this->createDataStoreEmptyMock(); $dataStore->expects($this->once()) diff --git a/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php b/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php index f7bacc37..27c1644e 100644 --- a/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php +++ b/test/functional/DataStore/Middleware/Handler/UpdateHandlerTest.php @@ -93,23 +93,7 @@ public function testCanUpdateFailWithNotEmptyRqlQueryObject() $this->assertFalse($object->canHandle($request)); } - public function attributeDataProvider() - { - return [ - [200, true, true], - [201, true, false], - [200, false, true], - [200, false, false], - ]; - } - - /** - * @dataProvider attributeDataProvider - * @param $status - * @param $overwriteMode - * @param $readReturn - */ - public function testProcess($status, $overwriteMode, $readReturn) + public function testProcess() { $item = [ 'id' => 1, @@ -119,16 +103,11 @@ public function testProcess($status, $overwriteMode, $readReturn) $request = new ServerRequest(); $request = $request->withMethod('PUT'); $request = $request->withAttribute('primaryKeyValue', $item['id']); - $request = $request->withAttribute('overwriteMode', $overwriteMode); $request = $request->withParsedBody($item); - $response = $this->createResponse($status, [], $item); + $response = $this->createResponse(200, [], $item); $dataStore = $this->createDataStoreEmptyMock(); - $dataStore->expects($this->once()) - ->method('read') - ->with($item['id']) - ->willReturn($readReturn); $dataStore->expects($this->once()) ->method('update') diff --git a/test/unit/DataStore/DataStore/CsvBaseTest.php b/test/unit/DataStore/DataStore/CsvBaseTest.php index 84dee349..553393d2 100644 --- a/test/unit/DataStore/DataStore/CsvBaseTest.php +++ b/test/unit/DataStore/DataStore/CsvBaseTest.php @@ -244,7 +244,7 @@ public function testUpdateFailItemDoesNotExistAndCreateIfAbsent() $item['name'] = 'name1'; $object = $this->createObject(); - $object->update($item, 1); + $object->update($item); $this->assertEquals($item, $this->read($item['id'])); } diff --git a/test/unit/DataStore/DataStore/HttpClientTest.php b/test/unit/DataStore/DataStore/HttpClientTest.php index 69bfd413..2b82705a 100644 --- a/test/unit/DataStore/DataStore/HttpClientTest.php +++ b/test/unit/DataStore/DataStore/HttpClientTest.php @@ -133,7 +133,7 @@ public function testCreateSuccess() $this->assertEquals($object->create($items), $items); } - public function testCreateSuccessWithOverwrite() + public function testRewriteSuccess() { $items = ['id' => 1, 'name' => 'name',]; $url = ''; @@ -164,7 +164,7 @@ public function testCreateFail() $this->expectException(DataStoreException::class); - $clientMock = $this->createClientMock('POST', $url, [], 1); + $clientMock = $this->createClientMock('POST', $url, []); $clientMock->expects($this->once()) ->method('setRawBody') ->with(Serializer::jsonSerialize($items)); @@ -216,7 +216,7 @@ public function testUpdateSuccessWithOverwrite() $itemsWithId = array_merge($items, ['id' => 1]); $url = ''; - $clientMock = $this->createClientMock('PUT', $url . '/1', [], 1); + $clientMock = $this->createClientMock('PUT', $url . '/1', []); $response = $this->createResponse($itemsWithId); $response->expects($this->once()) @@ -234,7 +234,7 @@ public function testUpdateSuccessWithOverwrite() $object = $this->createObject($clientMock, $url); $object->setIdendifier('id'); - $this->assertEquals($object->update($itemsWithId, 1), $itemsWithId); + $this->assertEquals($object->update($itemsWithId), $itemsWithId); } public function testUpdateFail() @@ -245,7 +245,7 @@ public function testUpdateFail() $itemsWithId = array_merge($items, ['id' => 1]); $url = ''; - $clientMock = $this->createClientMock('PUT', $url . '/1', [], 1); + $clientMock = $this->createClientMock('PUT', $url . '/1', []); $clientMock->expects($this->once()) ->method('setRawBody') ->with(Serializer::jsonSerialize($items)); @@ -262,7 +262,7 @@ public function testUpdateFail() $object = $this->createObject($clientMock, $url); $object->setIdendifier('id'); - $object->update($itemsWithId, 1); + $object->update($itemsWithId); } public function testReadSuccess() @@ -500,7 +500,7 @@ public function testException() $this->expectException(DataStoreException::class); $this->expectExceptionMessage("Can't create item {$method} {$url} ${status} {$reasonPhrase}"); - $clientMock = $this->createClientMock($method, $url, [], 1); + $clientMock = $this->createClientMock($method, $url, []); $clientMock->expects($this->once()) ->method('setRawBody') ->with(Serializer::jsonSerialize($items)); @@ -542,7 +542,7 @@ public function testExceptionWithRedirect() "Can't create item {$method} {$url} ${status} {$reasonPhrase} \"{$body}\" New location is '{$location}'" ); - $clientMock = $this->createClientMock($method, $url, [], 1); + $clientMock = $this->createClientMock($method, $url, []); $clientMock->expects($this->once()) ->method('setRawBody') ->with(Serializer::jsonSerialize($items)); diff --git a/test/unit/DataStore/DataStore/NoSupportTraitTest.php b/test/unit/DataStore/DataStore/NoSupportTraitTest.php index 6e211fde..627f1e1e 100644 --- a/test/unit/DataStore/DataStore/NoSupportTraitTest.php +++ b/test/unit/DataStore/DataStore/NoSupportTraitTest.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase; use rollun\datastore\DataStore\DataStoreException; -use rollun\datastore\DataStore\Interfaces\DataStoresInterface; +use rollun\datastore\DataStore\Interfaces\DataStoreInterface; use rollun\datastore\DataStore\Traits\NoSupportCountTrait; use rollun\datastore\DataStore\Traits\NoSupportCreateTrait; use rollun\datastore\DataStore\Traits\NoSupportDeleteAllTrait; @@ -20,12 +20,13 @@ use rollun\datastore\DataStore\Traits\NoSupportReadTrait; use rollun\datastore\DataStore\Traits\NoSupportUpdateTrait; use rollun\datastore\Rql\RqlQuery; +use Xiag\Rql\Parser\Query; class NoSupportTraitTest extends TestCase { protected function createObject() { - return new class implements DataStoresInterface { + return new class implements DataStoreInterface { use NoSupportCreateTrait, NoSupportDeleteAllTrait, NoSupportGetIdentifier, @@ -36,6 +37,31 @@ protected function createObject() NoSupportReadTrait, NoSupportUpdateTrait, NoSupportCountTrait; + + public function rewrite($record) + { + // TODO: Implement rewrite() method. + } + + public function multiCreate($records) + { + // TODO: Implement multiCreate() method. + } + + public function multiUpdate($records) + { + // TODO: Implement multiUpdate() method. + } + + public function queriedUpdate($record, Query $query) + { + // TODO: Implement queriedUpdate() method. + } + + public function queriedDelete(Query $query) + { + // TODO: Implement queriedDelete() method. + } }; } From 7000f59124c8e3011b82e6fa00b349574a6d6562 Mon Sep 17 00:00:00 2001 From: misha-rollun <72918877+misha-rollun@users.noreply.github.com> Date: Tue, 5 Apr 2022 06:28:34 +0300 Subject: [PATCH 7/7] docs(10045): fix in and out query incorrect format --- docs/index.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/index.md b/docs/index.md index f066852f..8e3ccd13 100644 --- a/docs/index.md +++ b/docs/index.md @@ -467,7 +467,7 @@ RQL - простой язык построения запросов для аб ``` - Строковое представление: `in`. Пример: ```php - $query = 'in(name,John,Jackson,Liam)'; + $query = 'in(name,(John,Jackson,Liam))'; ``` * `out`. Оператор который позволяет определить, НЕ совпадает ли значение поля со значением в списке (обратное к `in`). - Объект: `Xiag\Rql\Parser\Node\Query\ArrayOperator\OutNode`. Пример: @@ -476,7 +476,7 @@ RQL - простой язык построения запросов для аб ``` - Строковое представление: `out`. Пример: ```php - $query = 'out(name,Grayson,Lucas)'; + $query = 'out(name,(Grayson,Lucas))'; 2. Логические операторы. * `and`. Оператор, который отображает только те записи, когда все условие является правдой (`true`). @@ -1139,4 +1139,4 @@ $result = $fileObject->getRowById('1'); // array До версии "6.6.1" есть баг в классе CsvBinaryStrategy (которы отвечает за бинарный поиск), из-за которой некорректно работал поиск (если запускать его больше одного раза) и добавление новых строк. Причина в том, что после поиска не -обнулялось поле $uniqueIterations и влияло на результаты следующих поисков. \ No newline at end of file +обнулялось поле $uniqueIterations и влияло на результаты следующих поисков.