Skip to content

Commit

Permalink
[Pimcore] add tests for DataObjectBatchListing
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed Sep 20, 2024
1 parent 7d83b7e commit 8d77c71
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
21 changes: 21 additions & 0 deletions features/domain/pimcore/batch_list.feature
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions src/CoreShop/Behat/Context/Domain/PimcoreContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/CoreShop/Behat/Context/Setup/PimcoreClassContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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$/
*/
Expand Down

0 comments on commit 8d77c71

Please sign in to comment.