Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn users when the message table is not correctly formatted #516

Merged
merged 1 commit into from
Oct 18, 2018
Merged
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
71 changes: 59 additions & 12 deletions src/Drupal/DrupalExtension/Context/MessageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ public function assertErrorVisible($message)
/**
* Checks if the current page contains the given set of error messages
*
* @param $messages
* array An array of texts to be checked
* @param array $messages
* An array of texts to be checked. The first row should consist of the
* string "Error messages".
*
* @Then I should see the following error message(s):
*/
public function assertMultipleErrors(TableNode $messages)
{
$this->assertValidMessageTable($messages, 'error messages');
foreach ($messages->getHash() as $key => $value) {
$value = array_change_key_case($value);
$message = trim($value['error messages']);
$this->assertErrorVisible($message);
}
Expand All @@ -74,14 +77,17 @@ public function assertNotErrorVisible($message)
/**
* Checks if the current page does not contain the given set error messages
*
* @param $messages
* array An array of texts to be checked
* @param array $messages
* An array of texts to be checked. The first row should consist of the
* string "Error messages".
*
* @Then I should not see the following error messages:
*/
public function assertNotMultipleErrors(TableNode $messages)
{
$this->assertValidMessageTable($messages, 'error messages');
foreach ($messages->getHash() as $key => $value) {
$value = array_change_key_case($value);
$message = trim($value['error messages']);
$this->assertNotErrorVisible($message);
}
Expand All @@ -108,14 +114,17 @@ public function assertSuccessMessage($message)
/**
* Checks if the current page contains the given set of success messages
*
* @param $message
* array An array of texts to be checked
* @param array $message
* An array of texts to be checked. The first row should consist of the
* string "Success messages".
*
* @Then I should see the following success messages:
*/
public function assertMultipleSuccessMessage(TableNode $messages)
{
$this->assertValidMessageTable($messages, 'success messages');
foreach ($messages->getHash() as $key => $value) {
$value = array_change_key_case($value);
$message = trim($value['success messages']);
$this->assertSuccessMessage($message);
}
Expand All @@ -141,14 +150,17 @@ public function assertNotSuccessMessage($message)
/**
* Checks if the current page does not contain the given set of success messages
*
* @param $message
* array An array of texts to be checked
* @param array $message
* An array of texts to be checked. The first row should consist of the
* string "Success messages".
*
* @Then I should not see the following success messages:
*/
public function assertNotMultipleSuccessMessage(TableNode $messages)
{
$this->assertValidMessageTable($messages, 'success messages');
foreach ($messages->getHash() as $key => $value) {
$value = array_change_key_case($value);
$message = trim($value['success messages']);
$this->assertNotSuccessMessage($message);
}
Expand All @@ -175,14 +187,17 @@ public function assertWarningMessage($message)
/**
* Checks if the current page contains the given set of warning messages
*
* @param $message
* array An array of texts to be checked
* @param array $message
* An array of texts to be checked. The first row should consist of the
* string "Warning messages".
*
* @Then I should see the following warning messages:
*/
public function assertMultipleWarningMessage(TableNode $messages)
{
$this->assertValidMessageTable($messages, 'warning messages');
foreach ($messages->getHash() as $key => $value) {
$value = array_change_key_case($value);
$message = trim($value['warning messages']);
$this->assertWarningMessage($message);
}
Expand All @@ -208,14 +223,17 @@ public function assertNotWarningMessage($message)
/**
* Checks if the current page does not contain the given set of warning messages
*
* @param $message
* array An array of texts to be checked
* @param array $message
* An array of texts to be checked. The first row should consist of the
* string "Warning messages".
*
* @Then I should not see the following warning messages:
*/
public function assertNotMultipleWarningMessage(TableNode $messages)
{
$this->assertValidMessageTable($messages, 'warning messages');
foreach ($messages->getHash() as $key => $value) {
$value = array_change_key_case($value);
$message = trim($value['warning messages']);
$this->assertNotWarningMessage($message);
}
Expand Down Expand Up @@ -256,6 +274,35 @@ public function assertNotMessage($message)
);
}

/**
* Checks whether the given list of messages is valid.
*
* This checks whether the list has only one column and has the correct
* header.
*
* @param \Behat\Gherkin\Node\TableNode $messages
* The list of messages.
* @param string $expected_header
* The header that should be present in the list.
*/
protected function assertValidMessageTable(TableNode $messages, $expected_header)
{
// Check that the table only contains a single column.
$header_row = $messages->getRow(0);

$column_count = count($header_row);
if ($column_count != 1) {
throw new \RuntimeException("The list of $expected_header should only contain 1 column. It has $column_count columns.");
}

// Check that the correct header is used.
$actual_header = reset($header_row);
if (strtolower(trim($actual_header)) !== $expected_header) {
$capitalized_header = ucfirst($expected_header);
throw new \RuntimeException("The list of $expected_header should have the header '$capitalized_header'.");
}
}

/**
* Internal callback to check for a specific message in a given context.
*
Expand Down