Skip to content

Commit

Permalink
Added more steps + fixed Date trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk authored Jul 8, 2018
1 parent 8039457 commit c3390bd
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 59 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ build:
## Remove dependencies.
clean:
$(call title,Removing dependencies)
$(call exec,chmod -Rf 777 $(WEBROOT)/sites/default)
$(call exec,chmod -Rf 777 $(WEBROOT)/sites/default||true)
$(call exec,git ls-files --directory --other -i --exclude-from=.gitignore $(WEBROOT)|xargs rm -Rf)
$(call exec,rm -Rf vendor)
$(call exec,rm -Rf node_modules)

## Remove dependencies and Docker images.
clean-full: docker-stop docker-destroy clean
Expand Down
9 changes: 4 additions & 5 deletions src/D7/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Mink\Exception\ExpectationException;

/**
* Trait EmailTrait.
Expand Down Expand Up @@ -82,15 +81,15 @@ public function emailAssertEmailIsSentTo($address) {
}
}

throw new ExpectationException(sprintf("Unable to find email sent to '%s' retrieved from test email collector.", $address), $this->getSession());
throw new \Exception(sprintf("Unable to find email sent to '%s' retrieved from test email collector.", $address));
}

/**
* @Then no emails were sent
*/
public function emailAssertNoEmailsWereSent() {
if (count($this->emailGetCollectedEmails()) > 0) {
throw new ExpectationException('No emails were supposed to be sent', $this->getSession());
throw new \Exception('No emails were supposed to be sent');
}
}

Expand Down Expand Up @@ -132,7 +131,7 @@ public function emailAssertEmailContains($field, PyStringNode $string, $exact =
}
}

throw new ExpectationException(sprintf("Unable to find email with%s text '%s' in field '%s' retrieved from test email collector.", ($exact ? ' exact' : ''), $string, $field), $this->getSession());
throw new \Exception(sprintf("Unable to find email with%s text '%s' in field '%s' retrieved from test email collector.", ($exact ? ' exact' : ''), $string, $field));
}

/**
Expand All @@ -155,7 +154,7 @@ public function emailAssertEmailNotContains($field, PyStringNode $string, $exact
foreach ($this->emailGetCollectedEmails() as $email) {
$field_string = $exact ? $email[$field] : trim(preg_replace('/\s+/', ' ', $email[$field]));;
if (strpos($field_string, $string) !== FALSE) {
throw new ExpectationException(sprintf("Found email with%s text '%s' in field '%s' retrieved from test email collector, but should not.", ($exact ? ' exact' : ''), $string, $field), $this->getSession());
throw new \Exception(sprintf("Found email with%s text '%s' in field '%s' retrieved from test email collector, but should not.", ($exact ? ' exact' : ''), $string, $field));
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/D7/FileDownloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand Down Expand Up @@ -91,10 +90,10 @@ public function fileDownloadAssertLinkPresence($link, $presense) {
$link_element = $page->findLink($link);

if ($should_be_present && !$link_element) {
throw new ExpectationException(sprintf('No link "%s" is present on the page, but expected to be present', $link));
throw new \Exception(sprintf('No link "%s" is present on the page, but expected to be present', $link));
}
elseif (!$should_be_present && $link_element) {
throw new ExpectationException(sprintf('Link "%s" is present on the page, but expected to be absent', $link));
throw new \Exception(sprintf('Link "%s" is present on the page, but expected to be absent', $link));
}

return $link_element;
Expand All @@ -120,7 +119,7 @@ public function fileDownloadAssertFileContains(PyStringNode $string) {
}
}

throw new ExpectationException('Unable to find a content line with searched string.');
throw new \Exception('Unable to find a content line with searched string.');
}

/**
Expand All @@ -132,7 +131,7 @@ public function fileDownloadAssertFileName($name) {
}

if ($name != $this->fileDownloadDownloadedFileInfo['file_name']) {
throw new ExpectationException(sprintf('Downloaded file %s, but expected %s', $this->fileDownloadDownloadedFileInfo['file_name'], $name));
throw new \Exception(sprintf('Downloaded file %s, but expected %s', $this->fileDownloadDownloadedFileInfo['file_name'], $name));
}
}

Expand All @@ -150,7 +149,7 @@ public function fileDownloadAssertZipContains(TableNode $files) {
}

if (!empty($errors)) {
throw new ExpectationException(implode(PHP_EOL, $errors));
throw new \Exception(implode(PHP_EOL, $errors));
}
}

Expand All @@ -168,7 +167,7 @@ public function fileDownloadAssertNoZipContains(TableNode $files) {
}

if (!empty($errors)) {
throw new ExpectationException(implode(PHP_EOL, $errors));
throw new \Exception(implode(PHP_EOL, $errors));
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/D7/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
trait FileTrait {

/**
* Create managed file from existing local file.
*
* @Given managed file:
*/
public function fileCreateManaged(TableNode $nodesTable) {
Expand All @@ -23,25 +21,34 @@ public function fileCreateManaged(TableNode $nodesTable) {
if (empty($node->path)) {
throw new \RuntimeException('"path" property is required');
}
$path = ltrim($node->path, '/');

// Limited support for remote files: all remote files are considered
// oembed objects and therefore only oembed'able objects will be saved.
if (parse_url($node->path, PHP_URL_SCHEME) !== NULL) {
$provider = media_internet_get_provider($node->path);
$provider = media_internet_get_provider($path);
$file = $provider->save();
}
// Local file.
else {
$file_path = dirname(dirname(__FILE__)) . '/' . ltrim($node->path, '/');
$destination = 'public://' . basename($file_path);
if (!is_readable($file_path)) {
throw new \RuntimeException('Unable to find file ' . $file_path);
// Get fixture file path.
if ($this->getMinkParameter('files_path')) {
$full_path = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
if (is_file($full_path)) {
$path = $full_path;
}
}
$file = file_save_data(file_get_contents($file_path), $destination, FILE_EXISTS_REPLACE);

if (!is_readable($path)) {
throw new \RuntimeException('Unable to find file ' . $path);
}

$destination = 'public://' . basename($path);
$file = file_save_data(file_get_contents($path), $destination, FILE_EXISTS_REPLACE);
}

if (!$file) {
throw new \RuntimeException('Unable to save managed file ' . $file_path);
throw new \RuntimeException('Unable to save managed file ' . $path);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/D7/OverrideTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
trait OverrideTrait {

/**
* {@inheritdoc}
* Creates and authenticates a user with the given role(s).
*
* @Given I am logged in as a user with the :role role(s)
* @Given I am logged in as a/an :role
*/
public function assertAuthenticatedByRole($role) {
// Override parent assertion to allow using 'anonymous user' role without
Expand Down
28 changes: 28 additions & 0 deletions src/D7/TaxonomyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace IntegratedExperts\BehatSteps\D7;

/**
* Trait TaxonomyTrait.
*/
trait TaxonomyTrait {

/**
* @Given taxonomy term :name from vocabulary :vocab exists
*/
public function taxonomyAssertTermExistsByName($name, $vocabulary) {
$vocab = taxonomy_vocabulary_machine_name_load($vocabulary);
if (!$vocab) {
throw new RuntimeException(sprintf('"%s" vocabulary does not exist', $vocabulary));
}
$found = taxonomy_term_load_multiple(NULL, [
'name' => $name,
'vid' => $vocab->vid,
]);

if (count($found) == 0) {
throw new \Exception(printf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vocabulary));
}
}

}
7 changes: 3 additions & 4 deletions src/D7/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace IntegratedExperts\BehatSteps\D7;

use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;

/**
* Trait UserTrait.
Expand Down Expand Up @@ -58,7 +57,7 @@ public function userAssertHasRoles($name, $roles) {
}, $roles);

if (count(array_intersect($roles, $user->roles)) != count($roles)) {
throw new ExpectationException(sprintf('User "%s" does not have role(s) "%s", but has roles "%s"', $name, implode('", "', $roles), implode('", "', $user->roles)));
throw new \Exception(sprintf('User "%s" does not have role(s) "%s", but has roles "%s"', $name, implode('", "', $roles), implode('", "', $user->roles)));
}
}

Expand All @@ -74,7 +73,7 @@ public function userAssertHasNoRoles($name, $roles) {
}, $roles);

if (count(array_intersect($roles, $user->roles)) > 0) {
throw new ExpectationException(sprintf('User "%s" should not have roles(s) "%s", but has "%s"', $name, implode('", "', $roles), implode('", "', $user->roles)));
throw new \Exception(sprintf('User "%s" should not have roles(s) "%s", but has "%s"', $name, implode('", "', $roles), implode('", "', $user->roles)));
}
}

Expand All @@ -90,7 +89,7 @@ public function userAssertHasStatus($name, $status) {
}

if ($user->status != $status) {
throw new ExpectationException(sprintf('User "%s" is expected to have status "%s", but has status "%s"', $name, $status ? 'active' : 'blocked', $user->status ? 'active' : 'blocked'));
throw new \Exception(sprintf('User "%s" is expected to have status "%s", but has status "%s"', $name, $status ? 'active' : 'blocked', $user->status ? 'active' : 'blocked'));
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/D7/VariableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Exception\ExpectationException;

/**
* Class VariableTrait.
Expand Down Expand Up @@ -36,7 +35,7 @@ public function variableAssertValue($name, $value) {
$this->variableRefresh();
$variable_value = variable_get($name);
if ($value != $variable_value) {
throw new ExpectationException(sprintf('Variable %s has value "%s", but should have value "%s".', $name, $variable_value, $value));
throw new \Exception(sprintf('Variable %s has value "%s", but should have value "%s".', $name, $variable_value, $value));
}
}

Expand All @@ -47,7 +46,7 @@ public function variableAssertNoValue($name, $value) {
$this->variableRefresh();
$variable_value = variable_get($name);
if ($value == $variable_value) {
throw new ExpectationException(sprintf('Variable %s has value "%s", but should not have it.', $name, $variable_value));
throw new \Exception(sprintf('Variable %s has value "%s", but should not have it.', $name, $variable_value));
}
}

Expand All @@ -58,7 +57,7 @@ public function variableAssertNullValue($name) {
$this->variableRefresh();
$variable_value = variable_get($name);
if (!is_null($variable_value)) {
throw new ExpectationException(sprintf('Variable %s has value "%s", but should not have any value set.', $name, $variable_value));
throw new \Exception(sprintf('Variable %s has value "%s", but should not have any value set.', $name, $variable_value));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/D7/WatchdogTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Mink\Exception\ExpectationException;

/**
* Trait WatchdogTrait.
Expand Down Expand Up @@ -68,7 +67,7 @@ public function checkWatchdog(AfterScenarioScope $scope) {
}

if (!empty($entries)) {
throw new ExpectationException('PHP errors were logged to watchdog during this scenario.');
throw new \Exception('PHP errors were logged to watchdog during this scenario.');
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/D8/ContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;

/**
* Trait ContentTrait.
Expand Down Expand Up @@ -112,7 +111,7 @@ public function contentModeratePageWithTitle($type, $title, $old_state, $new_sta
$node = current($nodes);
$current_old_state = $node->get('moderation_state')->first()->getString();
if ($current_old_state != $old_state) {
throw new ExpectationException(sprintf('The current state "%s" is different from "%s"', $current_old_state, $old_state));
throw new \Exception(sprintf('The current state "%s" is different from "%s"', $current_old_state, $old_state));
}

$node->set('moderation_state', $new_state);
Expand Down
7 changes: 3 additions & 4 deletions src/D8/TaxonomyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace IntegratedExperts\BehatSteps\D8;

use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Drupal\taxonomy\Entity\Vocabulary;

/**
Expand All @@ -19,10 +18,10 @@ trait TaxonomyTrait {
public function taxonomyAssertVocabularyExist($name, $vid) {
$vocab = Vocabulary::load($vid);
if (!$vocab) {
throw new ExpectationException(sprintf('"%s" vocabulary does not exist', $vid));
throw new \Exception(sprintf('"%s" vocabulary does not exist', $vid));
}
elseif ($vocab->get('name') != $name) {
throw new ExpectationException(sprintf('"%s" vocabulary name is not "%s"', $vid, $name));
throw new \Exception(sprintf('"%s" vocabulary name is not "%s"', $vid, $name));
}
}

Expand All @@ -44,7 +43,7 @@ public function taxonomyAssertTermExistsByName($name, $vid) {
]);

if (count($found) == 0) {
throw new ExpectationException(sprintf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vid));
throw new \Exception(sprintf('Taxonomy term "%s" from vocabulary "%s" does not exist', $name, $vid));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/D8/WatchdogTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Mink\Exception\ExpectationException;

/**
* Trait WatchdogTrait.
Expand Down Expand Up @@ -68,7 +67,7 @@ public function checkWatchdog(AfterScenarioScope $scope) {
}

if (!empty($entries)) {
throw new ExpectationException('PHP errors were logged to watchdog during this scenario.');
throw new \Exception('PHP errors were logged to watchdog during this scenario.');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/DateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ trait DateTrait {
public static function dateProcess($value, $now = NULL) {
// If `now` is not provided, round to the current hour to make sure that
// assertions are running within the same timeframe (for long tests).
$now = $now ? $now : strtotime(date('Y-m-d H:m:00', time()));
$now = $now ? $now : strtotime(date('Y-m-d 12:30:00', time()));

return preg_replace_callback('/\[([^:]+):([^\]\[\#]+)(?:\#([^\]\[]+))?\]/', function ($matches) use ($now) {
$timestamp = strtotime($matches[2], $now);
Expand Down
7 changes: 3 additions & 4 deletions src/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace IntegratedExperts\BehatSteps;

use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;

/**
* Trait Field.
Expand Down Expand Up @@ -39,7 +38,7 @@ public function fieldAssertNotExists($field_name) {
$field = $field ? $field : $page->findById($field_name);

if ($field !== NULL) {
throw new ExpectationException(sprintf('A field "%s" appears on this page, but it should not.', $field_name), $this->getSession()
throw new \Exception(sprintf('A field "%s" appears on this page, but it should not.', $field_name), $this->getSession()
->getDriver());
}
}
Expand All @@ -63,11 +62,11 @@ public function fieldAssertState($field_name, $disabled) {
$field = $this->fieldAssertExists($field_name);

if ($disabled == 'disabled' && !$field->hasAttribute('disabled')) {
throw new ExpectationException(sprintf('A field "%s" should be disabled, but it is not.', $field_name), $this->getSession()
throw new \Exception(sprintf('A field "%s" should be disabled, but it is not.', $field_name), $this->getSession()
->getDriver());
}
elseif ($disabled != 'disabled' && $field->hasAttribute('disabled')) {
throw new ExpectationException(sprintf('A field "%s" should not be disabled, but it is.', $field_name), $this->getSession()
throw new \Exception(sprintf('A field "%s" should not be disabled, but it is.', $field_name), $this->getSession()
->getDriver());
}
}
Expand Down
Loading

0 comments on commit c3390bd

Please sign in to comment.