Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Database: add support to delete generated database dumps after-scenario. #260

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Context/Traits/FilesystemAwareContextTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace PaulGibbs\WordpressBehatExtension\Context\Traits;

/**
* Provides driver agnostic logic (helper methods) relating to a filesystem or files.
*/
trait FilesystemAwareContextTrait
{
use BaseAwarenessTrait;

/**
* Delete specified file.
*
* @param string $abspath Absolute path to a file, to delete.
*/
public function deleteFile(string $abspath)
{
$this->getDriver()->filesystem->deleteFile($abspath);
}
}
34 changes: 31 additions & 3 deletions src/Context/WordpressContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Event;
use Behat\Mink\Driver\Selenium2Driver;
use PaulGibbs\WordpressBehatExtension\Context\Traits\CacheAwareContextTrait;
use PaulGibbs\WordpressBehatExtension\Context\Traits\DatabaseAwareContextTrait;
use PaulGibbs\WordpressBehatExtension\Context\Traits\FilesystemAwareContextTrait;
use PaulGibbs\WordpressBehatExtension\Context\Traits\PageObjectAwareContextTrait;

/**
* Provides step definitions for a range of common tasks. Recommended for all test suites.
*/
class WordpressContext extends RawWordpressContext
{
use PageObjectAwareContextTrait, CacheAwareContextTrait, DatabaseAwareContextTrait;
use PageObjectAwareContextTrait, CacheAwareContextTrait, DatabaseAwareContextTrait, FilesystemAwareContextTrait;

/**
* Constructor.
Expand Down Expand Up @@ -45,7 +47,7 @@ public function maybeBackupDatabase(BeforeScenarioScope $scope)
* Otherwise, we would use the (static) BeforeSuiteScope hook.
*/
$backup_file = $this->getWordpress()->getSetting('database_backup_file');
if ($backup_file) {
if (! empty($backup_file)) {
return;
}

Expand All @@ -55,6 +57,7 @@ public function maybeBackupDatabase(BeforeScenarioScope $scope)
// If the path specified is not a file, use it as the preferred folder to store a new backup.
if (! $file || ! is_file($file) || ! is_readable($file)) {
$file = $this->exportDatabase(['path' => $file]);
$this->getWordpress()->setSetting('built_database_backup', true);
}

// Note: $file may be either an absolute OR relative file path.
Expand Down Expand Up @@ -118,10 +121,35 @@ public function maybeRestoreDatabase(AfterScenarioScope $scope)
}

$file = $this->getWordpress()->getSetting('database_backup_file');
if (! $file) {
if (empty($file)) {
return;
}

$this->importDatabase(['path' => $file]);
}

/**
* If WordHat created its own database dump, delete it after the test suite has completed.
*
* @AfterSuite @db
*
* @param Event\SuiteEvent $scope
*/
public function maybeRemoveDatabaseDump(Event\SuiteEvent $event)
{
$delete_backup = $this->getWordpress()->getSetting('built_database_backup');
if ($delete_backup === null) {
return;
}

$file = $this->getWordpress()->getSetting('database_backup_file');
if (empty($file)) {
return;
}

$this->getWordpress()->setSetting('database_backup_file', null);
$this->getWordpress()->setSetting('built_database_backup', null);

$this->deleteFile($file);
}
}
28 changes: 28 additions & 0 deletions src/Driver/Element/Wpcli/FilesystemElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli;

use RuntimeException;
use PaulGibbs\WordpressBehatExtension\Driver\Element\BaseElement;

/**
* WP-CLI driver element for manipulating the filesystem.
*/
class FilesystemElement extends BaseElement
{
/**
* Delete specified file.
*
* @param string $abspath Absolute path to a file, to delete.
*/
public function deleteFile(string $abspath)
{
if (empty($abspath)) {
return;
}

$delete_cmd = sprintf('"@unlink(\'%s\');"', $abspath );
$this->drivers->getDriver()->wpcli('eval', $delete_cmd, ['--skip-wordpress']);
}
}

26 changes: 26 additions & 0 deletions src/Driver/Element/Wpphp/FilesystemElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp;

use RuntimeException;
use PaulGibbs\WordpressBehatExtension\Driver\Element\BaseElement;

/**
* WP-API driver element for manipulating the filesystem.
*/
class FilesystemElement extends BaseElement
{
/**
* Delete specified file.
*
* @param string $abspath Absolute path to a file, to delete.
*/
public function deleteFile(string $abspath)
{
if (empty($abspath) || ! is_file($abspath) || ! is_readable($abspath)) {
return;
}

wp_delete_file($abspath);
}
}
8 changes: 8 additions & 0 deletions src/ServiceContainer/config/drivers/wpcli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ parameters:
wordpress.element.wpcli.comment.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\CommentElement
wordpress.element.wpcli.content.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\ContentElement
wordpress.element.wpcli.database.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\DatabaseElement
wordpress.element.wpcli.filesystem.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\FilesystemElement
wordpress.element.wpcli.plugin.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\PluginElement
wordpress.element.wpcli.term.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\TermElement
wordpress.element.wpcli.theme.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\ThemeElement
Expand Down Expand Up @@ -49,6 +50,13 @@ services:
tags:
- { name: wordpress.element, alias: database, driver: wpcli }

wordpress.element.wpcli.filesystem:
class: "%wordpress.element.wpcli.filesystem.class%"
arguments:
- "@wordpress.wordpress"
tags:
- { name: wordpress.element, alias: filesystem, driver: wpcli }

wordpress.element.wpcli.plugin:
class: "%wordpress.element.wpcli.plugin.class%"
arguments:
Expand Down
8 changes: 8 additions & 0 deletions src/ServiceContainer/config/drivers/wpphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ parameters:
wordpress.element.wpphp.comment.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\CommentElement
wordpress.element.wpphp.content.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\ContentElement
wordpress.element.wpphp.database.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\DatabaseElement
wordpress.element.wpphp.filesystem.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\FilesystemElement
wordpress.element.wpphp.plugin.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\PluginElement
wordpress.element.wpphp.term.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\TermElement
wordpress.element.wpphp.theme.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\ThemeElement
Expand Down Expand Up @@ -46,6 +47,13 @@ services:
tags:
- { name: wordpress.element, alias: database, driver: wpphp }

wordpress.element.wpphp.filesystem:
class: "%wordpress.element.wpphp.filesystem.class%"
arguments:
- "@wordpress.wordpress"
tags:
- { name: wordpress.element, alias: filesystem, driver: wpphp }

wordpress.element.wpphp.plugin:
class: "%wordpress.element.wpphp.plugin.class%"
arguments:
Expand Down