-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create abstract class for deleting entities
- Loading branch information
1 parent
17a66e0
commit 0788f41
Showing
2 changed files
with
62 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} | ||
|
||
} |