diff --git a/features/domain/pimcore/batch_list.feature b/features/domain/pimcore/batch_list.feature new file mode 100644 index 0000000000..229267d880 --- /dev/null +++ b/features/domain/pimcore/batch_list.feature @@ -0,0 +1,21 @@ +@domain @pimcore +Feature: Using the CoreShop Batch List + In order to use the CoreShop Batch List + I want to create a new Pimcore Class and + iterate over all objects with the CoreShop Batch List + + Background: + Given there is a pimcore class "Test" + And the definition has a checkbox field "enabled" + And the definition has a localized input field "name" + And the definitions parent class is set to "\CoreShop\Behat\Model\Index\TestEnableIndex" + And there are 20 instances of behat-class "Test" with key-prefix "test" + + Scenario: Iterate the Class with Batch Listing + Then iterating the behat-class "Test" should return 20 objects + + Scenario: Iterate the Class with Batch Listing with a offset + Then iterating the behat-class "Test" with a offset of 5 should return 15 objects + + Scenario: Iterate the Class with Batch Listing with a offset and limit + Then iterating the behat-class "Test" with a offset of 5 and limit of 1 should return 1 objects \ No newline at end of file diff --git a/src/CoreShop/Behat/Context/Domain/PimcoreContext.php b/src/CoreShop/Behat/Context/Domain/PimcoreContext.php index add8edace9..381a5ccb72 100644 --- a/src/CoreShop/Behat/Context/Domain/PimcoreContext.php +++ b/src/CoreShop/Behat/Context/Domain/PimcoreContext.php @@ -19,6 +19,10 @@ namespace CoreShop\Behat\Context\Domain; use Behat\Behat\Context\Context; +use CoreShop\Component\Pimcore\BatchProcessing\DataObjectBatchListing; +use Pimcore\Model\DataObject\ClassDefinition; +use Pimcore\Model\DataObject\Listing; + use Webmozart\Assert\Assert; final class PimcoreContext implements Context @@ -64,6 +68,72 @@ public function allEditmodeCssResourceShouldExist(): void $this->checkFilesExist($this->editmodeCss, 'Editmode CSS'); } + /** + * @Then /^iterating the (class|behat-class "[^"]+") should return (\d+) objects$/ + */ + public function iteratingTheClassShouldReturn(ClassDefinition $definition, int $count): void + { + $list = $this->getListingFromClassDefinition($definition); + $batchListing = new DataObjectBatchListing($list, 1); + + Assert::eq($batchListing->count(), $count); + Assert::eq($this->countBatchListingObjects($batchListing), $count); + } + + /** + * @Then /^iterating the (class|behat-class "[^"]+") with a offset of (\d+) should return (\d+) objects$/ + */ + public function iteratingTheClassWithAOffsetShouldReturn(ClassDefinition $definition, int $offset, int $count): void + { + $list = $this->getListingFromClassDefinition($definition); + $list->setOffset($offset); + + $batchListing = new DataObjectBatchListing($list, 1); + + Assert::eq($batchListing->count(), $count); + Assert::eq($this->countBatchListingObjects($batchListing), $count); + } + + /** + * @Then /^iterating the (class|behat-class "[^"]+") with a offset of (\d+) and limit of (\d+) should return (\d+) objects$/ + */ + public function iteratingTheClassWithAOffsetAndLimitShouldReturn(ClassDefinition $definition, int $offset, int $limit, int $count): void + { + $list = $this->getListingFromClassDefinition($definition); + $list->setOffset($offset); + $list->setLimit($limit); + + $batchListing = new DataObjectBatchListing($list, 1); + + Assert::eq($batchListing->count(), $count); + Assert::eq($this->countBatchListingObjects($batchListing), $count); + } + + private function countBatchListingObjects(DataObjectBatchListing $batchListing): int + { + $count = 0; + + foreach ($batchListing as $object) { + ++$count; + } + + return $count; + } + + private function getListingFromClassDefinition(ClassDefinition $definition): Listing + { + $className = sprintf('Pimcore\\Model\\DataObject\\%s\\Listing', $definition->getName()); + + $list = new $className(); + + /** + * @Listing $list + */ + Assert::isInstanceOf($list, Listing::class); + + return $list; + } + private function checkFilesExist(array $files, string $type): void { foreach ($files as $file) { diff --git a/src/CoreShop/Behat/Context/Setup/PimcoreClassContext.php b/src/CoreShop/Behat/Context/Setup/PimcoreClassContext.php index bb7b872329..139287787d 100644 --- a/src/CoreShop/Behat/Context/Setup/PimcoreClassContext.php +++ b/src/CoreShop/Behat/Context/Setup/PimcoreClassContext.php @@ -705,6 +705,27 @@ public function thereIsAnInstanceofClassWithKey(ClassDefinition $definition, $ke $this->sharedStorage->set('object-instance', $instance); } + /** + * @Given /^there are (\d+) instances of (class|behat-class "[^"]+") with key-prefix "([^"]+)"$/ + */ + public function thereAreCountInstancesOfClassWithKey(int $count, ClassDefinition $definition, $key): void + { + /** + * @var class-string $className + */ + $className = sprintf('Pimcore\\Model\\DataObject\\%s', $definition->getName()); + + for ($i = 0; $i < $count; $i++) { + /** + * @var Concrete $instance + */ + $instance = new $className(); + $instance->setKey(sprintf('%s-%s', $key, $i)); + $instance->setParentId(1); + $instance->save(); + } + } + /** * @Given /^I reload the (object-instance) into object-instance-2$/ */