Skip to content

Commit

Permalink
Warn users when the message table is not correctly formatted.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrenssen committed Oct 15, 2018
1 parent 2d98cc6 commit 83f218e
Showing 1 changed file with 59 additions and 12 deletions.
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

0 comments on commit 83f218e

Please sign in to comment.