Skip to content

Commit

Permalink
Create abstract class for deleting entities
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsokolowski06 committed Oct 8, 2024
1 parent 17a66e0 commit 0788f41
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
49 changes: 14 additions & 35 deletions src/CommerceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
*
* Provide Behat step-definitions for common Commerce functionalities.
*/
class CommerceContext extends RawDrupalContext {

/**
* An array of promotions created during this context.
*
* @var array
*/
protected $promotions = [];
class CommerceContext extends EntityContextBase {

/**
* Generate coupons.
Expand All @@ -31,15 +24,11 @@ public function createCoupons(TableNode $nodesTable) {
foreach ($nodesTable->getHash() as $nodeHash) {
$coupon = (object) $nodeHash;
$promotion = $this->getPromotionByName($coupon->promotion);
if ($promotion) {
$coupon_saved = $this->couponCreate($coupon);
$coupon_loaded = $storage->load($coupon_saved->id);
$promotion->get('coupons')->appendItem($coupon_loaded);
$promotion->save();
}
else {
throw new \Exception("No parent promotion found.");
}

$coupon_saved = $this->couponCreate($coupon);
$coupon_loaded = $storage->load($coupon_saved->id);
$promotion->get('coupons')->appendItem($coupon_loaded);
$promotion->save();
}
}

Expand All @@ -48,14 +37,13 @@ public function createCoupons(TableNode $nodesTable) {
*/
public function getPromotionByName($promotion_name) {
$storage = \Drupal::entityTypeManager()->getStorage('commerce_promotion');
foreach ($this->promotions as $promotion) {
$loaded_promotion = $storage->load($promotion->id);
if ($loaded_promotion->label() === $promotion_name) {
return $loaded_promotion;
}
$promotions = $storage->loadByProperties(['name' => $promotion_name]);

if (count($promotions) !== 1) {
throw new \Exception('Expected 1 promotion with title ' . $title . ' but found ' . count($promotions));
}

return NULL;
return reset($promotions);
}

/**
Expand All @@ -69,19 +57,10 @@ public function couponCreate($coupon) {
/**
* Visit promotion edit page.
*
* @Then I visit promotion :title edit page
* @Then I visit promotion :name edit page
*/
public function iVisitPromotionEditPage($title) {
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface[] $promotions */
$promotions = \Drupal::entityTypeManager()->getStorage('commerce_promotion')
->loadByProperties([
'name' => $title,
]);
if (count($promotions) !== 1) {
throw new \Exception('Expected 1 promotion with title ' . $title . ' but found ' . count($promotions));
}

$promotion = reset($promotions);
public function iVisitPromotionEditPage($name) {
$promotion = $this->getPromotionByName($name);
$this->getSession()->visit($this->locatePath($promotion->toUrl('edit-form')->toString()));
}

Expand Down
48 changes: 48 additions & 0 deletions src/EntityContextBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Frontkom\DrupalBehatDefinitions;

use Drupal\DrupalExtension\Context\RawDrupalContext;

/**
* Class EntityContextBase.
*
* It is supposed to be extended by classes which implement creating entities. It automatically cleans up
* all the entities created during a single test.
*/
abstract class EntityContextBase extends RawDrupalContext {

/**
* An array of entites created in the context.
*
* @var array
*/
protected $entites = [];

/**
* Remove entities by names.
*
* @Then I remove entities of type :entity_type_id with names :names
*/
public function iRemoveEntities($entity_type_id, $names) {
$entities = \Drupal::entityTypeManager()->getStorage($entity_type_id)
->loadByProperties([
'name' => explode(', ', $names),
]);
foreach ($entities as $entity) {
$entity->delete();
}
}

/**
* Clean up created entities.
*
* @AfterScenario
*/
public function cleanUpEntities() {
foreach ($this->entites as $entity) {
$entity->delete();
}
}

}

0 comments on commit 0788f41

Please sign in to comment.