Skip to content

Commit

Permalink
Fixed coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Jul 4, 2024
1 parent 22b04d1 commit ecb5fed
Show file tree
Hide file tree
Showing 30 changed files with 149 additions and 105 deletions.
10 changes: 5 additions & 5 deletions src/BigPipeTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
Expand All @@ -26,8 +28,6 @@ trait BigPipeTrait {

/**
* Skip Big Pipe BeforeStep.
*
* @var bool
*/
protected bool $bigPipeBeforeStepSkip = FALSE;

Expand Down Expand Up @@ -62,13 +62,13 @@ public function bigPipeBeforeScenarioInit(BeforeScenarioScope $scope): void {
$driver->executeScript('true');
$this->bigPipeNoJS = FALSE;
}
catch (UnsupportedDriverActionException $e) {
catch (UnsupportedDriverActionException) {
$this->bigPipeNoJS = TRUE;
$this
->getSession()
->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true');
}
catch (\Exception $e) {
catch (\Exception) {
// Mute exceptions.
}
}
Expand All @@ -88,7 +88,7 @@ public function bigPipeBeforeStep(BeforeStepScope $scope): void {
->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true');
}
}
catch (DriverException $e) {
catch (DriverException) {
// Mute not visited page exception.
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ContentTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Gherkin\Node\TableNode;
Expand Down
6 changes: 3 additions & 3 deletions src/DateTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Gherkin\Node\TableNode;
Expand Down Expand Up @@ -43,9 +45,7 @@ public function dateRelativeTransformTable(TableNode $table): TableNode {
$rows[] = $row;
}

$new_table = new TableNode($rows);

return $new_table;
return new TableNode($rows);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/DraggableViewsTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Gherkin\Node\TableNode;
Expand Down
2 changes: 2 additions & 0 deletions src/EckTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Behat\Hook\Scope\AfterScenarioScope;
Expand Down
4 changes: 3 additions & 1 deletion src/ElementTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

/**
Expand Down Expand Up @@ -30,7 +32,7 @@ public function elementAssertAttributeHasValue(string $selector, string $attribu
$attr = $element->getAttribute($attribute);
if (!empty($attr)) {
$attr_found = TRUE;
if (str_contains($attr, strval($value))) {
if (str_contains((string) $attr, strval($value))) {
$attr_value_found = TRUE;
break;
}
Expand Down
59 changes: 28 additions & 31 deletions src/EmailTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Behat\Hook\Scope\AfterScenarioScope;
Expand Down Expand Up @@ -132,7 +134,7 @@ public function emailClearTestEmailSystemQueue(bool $force = FALSE): void {
*/
public function emailAssertEmailIsSentTo(string $address): void {
foreach (self::emailGetCollectedEmails() as $record) {
$email_to = explode(',', $record['to']);
$email_to = explode(',', (string) $record['to']);

if (in_array($address, $email_to)) {
return;
Expand Down Expand Up @@ -160,20 +162,20 @@ public function emailAssertNoEmailsWereSent(): void {
*/
public function emailAssertNoEmailsWereSentToAddress(string $address): void {
foreach ($this->emailGetCollectedEmails() as $record) {
$email_to = explode(',', $record['to']);
$email_to = explode(',', (string) $record['to']);
if (in_array($address, $email_to)) {
throw new \Exception(sprintf('An email sent to "%s" retrieved from test email collector.', $address));
}

if (!empty($record['headers']['Cc'])) {
$email_cc = explode(',', $record['headers']['Cc']);
$email_cc = explode(',', (string) $record['headers']['Cc']);
if (in_array($address, $email_cc)) {
throw new \Exception(sprintf('An email cc\'ed to "%s" retrieved from test email collector.', $address));
}
}

if (!empty($record['headers']['Bcc'])) {
$email_bcc = explode(',', $record['headers']['Bcc']);
$email_bcc = explode(',', (string) $record['headers']['Bcc']);
if (in_array($address, $email_bcc)) {
throw new \Exception(sprintf('An email bcc\'ed to "%s" retrieved from test email collector.', $address));
}
Expand All @@ -188,13 +190,13 @@ public function emailAssertNoEmailsWereSentToAddress(string $address): void {
*/
public function emailAssertEmailHeadersContains(string $header, PyStringNode $string, bool $exact = FALSE): array {
$string_value = (string) $string;
$string_value = $exact ? $string_value : trim(preg_replace('/\s+/', ' ', $string_value));
$string_value = $exact ? $string_value : trim((string) preg_replace('/\s+/', ' ', $string_value));

foreach ($this->emailGetCollectedEmails() as $record) {
$header_value = $record['headers'][$header] ?? '';
$header_value = $exact ? $header_value : trim(preg_replace('/\s+/', ' ', $header_value));

if (str_contains($header_value, $string_value)) {
if (str_contains((string) $header_value, $string_value)) {
return $record;
}
}
Expand All @@ -217,16 +219,16 @@ public function emailAssertEmailHeadersContainsExact(string $header, PyStringNod
* @Then /^an email to "(?P<name>[^"]*)" user is "(?P<action>[^"]*)" with "(?P<field>[^"]*)" content:$/
*/
public function emailAssertEmailToUserIsActionWithContent(string $name, string $action, string $field, PyStringNode $string): void {
$user = $name == 'current' && !empty($this->getUserManager()->getCurrentUser()) ? $this->getUserManager()->getCurrentUser() : user_load_by_name($name);
$user = $name === 'current' && !empty($this->getUserManager()->getCurrentUser()) ? $this->getUserManager()->getCurrentUser() : user_load_by_name($name);
if (!$user) {
throw new \Exception(sprintf('Unable to find a user "%s"', $name));
}

if ($action == 'sent') {
if ($action === 'sent') {
$this->emailAssertEmailContains('to', new PyStringNode([$user->mail], 0), TRUE);
$this->emailAssertEmailContains($field, $string);
}
elseif ($action == 'not sent') {
elseif ($action === 'not sent') {
$this->emailAssertEmailNotContains($field, $string);
}
else {
Expand All @@ -246,13 +248,13 @@ public function emailAssertEmailContains(string $field, PyStringNode $string, bo
}

$string = strval($string);
$string = $exact ? $string : trim(preg_replace('/\s+/', ' ', $string));
$string = $exact ? $string : trim((string) preg_replace('/\s+/', ' ', $string));

foreach (self::emailGetCollectedEmails() as $record) {
$field_string = $record[$field] ?? '';
$field_string = $exact ? $field_string : trim(preg_replace('/\s+/', ' ', $field_string));

if (str_contains($field_string, $string)) {
if (str_contains((string) $field_string, $string)) {
return $record;
}
}
Expand Down Expand Up @@ -282,12 +284,12 @@ public function emailAssertEmailNotContains(string $field, PyStringNode $string,
}

$string = strval($string);
$string = $exact ? $string : trim(preg_replace('/\s+/', ' ', $string));
$string = $exact ? $string : trim((string) preg_replace('/\s+/', ' ', $string));

foreach (self::emailGetCollectedEmails() as $record) {
$field_string = $exact ? $record[$field] : trim(preg_replace('/\s+/', ' ', $record[$field]));

if (str_contains($field_string, $string)) {
if (str_contains((string) $field_string, $string)) {
throw new \Exception(sprintf('Found record with%s text "%s" in field "%s" retrieved from test record collector, but should not.', ($exact ? ' exact' : ''), $string, $field));
}
}
Expand Down Expand Up @@ -350,14 +352,14 @@ public function emailAssertEmailContainsAttachmentWithName(string $name, PyStrin
* Get default mail system value.
*/
protected static function emailGetMailSystemDefault(string $type = 'default'): mixed {
return \Drupal::config('system.mail')->get("interface.$type");
return \Drupal::config('system.mail')->get('interface.' . $type);
}

/**
* Set default mail system value.
*/
protected static function emailSetMailSystemDefault(string $type, mixed $value): void {
\Drupal::configFactory()->getEditable('system.mail')->set("interface.$type", $value)->save();
\Drupal::configFactory()->getEditable('system.mail')->set('interface.' . $type, $value)->save();

// Maisystem module completely takes over default interface, so we need to
// update it as well if the module is installed.
Expand All @@ -376,14 +378,14 @@ protected static function emailSetMailSystemDefault(string $type, mixed $value):
* Get original mail system value.
*/
protected static function emailGetMailSystemOriginal(string $type = 'default'): mixed {
return \Drupal::config('system.mail_original')->get("interface.$type");
return \Drupal::config('system.mail_original')->get('interface.' . $type);
}

/**
* Set original mail system value.
*/
protected static function emailSetMailSystemOriginal(string $type, mixed $value): void {
\Drupal::configFactory()->getEditable('system.mail_original')->set("interface.$type", $value)->save();
\Drupal::configFactory()->getEditable('system.mail_original')->set('interface.' . $type, $value)->save();
}

/**
Expand All @@ -399,9 +401,9 @@ protected static function emailDeleteMailSystemOriginal(): void {
protected function emailGetCollectedEmails(): array {
// Directly read data from the database to avoid cache invalidation that
// may corrupt the system under test.
$emails = array_map('unserialize', Database::getConnection()->query("SELECT name, value FROM {key_value} WHERE name = 'system.test_mail_collector'")->fetchAllKeyed());
$emails = array_map(unserialize(...), Database::getConnection()->query("SELECT name, value FROM {key_value} WHERE name = 'system.test_mail_collector'")->fetchAllKeyed());

$emails = !empty($emails['system.test_mail_collector']) ? $emails['system.test_mail_collector'] : [];
$emails = empty($emails['system.test_mail_collector']) ? [] : $emails['system.test_mail_collector'];

if ($this->emailDebug) {
$fields = ['to', 'from', 'subject', 'body'];
Expand Down Expand Up @@ -432,13 +434,13 @@ protected function emailGetCollectedEmails(): array {
protected static function emailExtractLinks(string $string): array {
// Correct links before extraction.
$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
$string = preg_replace_callback("#$pattern#i", function (array $matches): string {
return preg_match('!^https?://!i', $matches[0]) ? $matches[0] : "http://$matches[0]";
$string = preg_replace_callback(sprintf('#%s#i', $pattern), function (array $matches): string {
return preg_match('!^https?://!i', $matches[0]) ? $matches[0] : 'http://' . $matches[0];
}, $string);

preg_match_all("#$pattern#i", $string, $matches);
preg_match_all(sprintf('#%s#i', $pattern), (string) $string, $matches);

return !empty($matches[0]) ? $matches[0] : [];
return empty($matches[0]) ? [] : $matches[0];
}

/**
Expand All @@ -448,14 +450,9 @@ protected static function emailExtractTypes(array $tags): array {
$types = [];

foreach ($tags as $tag) {
if (str_starts_with($tag, 'email')) {
$parts = explode(':', $tag);
if (count($parts) > 1) {
$types[] = implode(':', array_slice($parts, 1));
}
else {
$types[] = 'default';
}
if (str_starts_with((string) $tag, 'email')) {
$parts = explode(':', (string) $tag);
$types[] = count($parts) > 1 ? implode(':', array_slice($parts, 1)) : 'default';
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/FieldTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Mink\Element\NodeElement;
Expand Down Expand Up @@ -72,7 +74,7 @@ public function fieldAssertNotExists(string $field_name): void {
* @Then field :name :exists on the page
*/
public function fieldAssertExistence(string $field_name, string $exists): void {
if ($exists == 'exists') {
if ($exists === 'exists') {
$this->fieldAssertExists($field_name);
}
else {
Expand All @@ -95,10 +97,10 @@ public function fieldAssertExistence(string $field_name, string $exists): void {
public function fieldAssertState(string $field_name, string $disabled): void {
$field = $this->fieldAssertExists($field_name);

if ($disabled == 'disabled' && !$field->hasAttribute('disabled')) {
if ($disabled === 'disabled' && !$field->hasAttribute('disabled')) {
throw new \Exception(sprintf('A field "%s" should be disabled, but it is not.', $field_name));
}
elseif ($disabled != 'disabled' && $field->hasAttribute('disabled')) {
elseif ($disabled !== 'disabled' && $field->hasAttribute('disabled')) {
throw new \Exception(sprintf('A field "%s" should not be disabled, but it is.', $field_name));
}
}
Expand All @@ -114,7 +116,7 @@ public function fieldAssertState(string $field_name, string $disabled): void {
* @Then field :name should be :presence on the page and have state :state
*/
public function fieldAssertExistsState(string $field_name, string $presence, string $state = 'enabled'): void {
if ($presence == 'present') {
if ($presence === 'present') {
$this->fieldAssertExists($field_name);
$this->fieldAssertState($field_name, $state);
}
Expand Down
12 changes: 7 additions & 5 deletions src/FileDownloadTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatSteps;

use Behat\Behat\Hook\Scope\AfterScenarioScope;
Expand Down Expand Up @@ -120,7 +122,7 @@ public function fileDownloadFromLink(string $link): void {
* @Then I see download :link link :presence(on the page)
*/
public function fileDownloadAssertLinkPresence(string $link, string $presence): NodeElement {
$should_be_present = $presence == 'present';
$should_be_present = $presence === 'present';

$page = $this->getSession()->getPage();
$link_element = $page->findLink($link);
Expand All @@ -145,7 +147,7 @@ public function fileDownloadAssertFileContains(PyStringNode $string): void {
if (!$this->fileDownloadDownloadedFileInfo) {
throw new \RuntimeException('Downloaded file content has no data.');
}
$lines = preg_split('/\R/', $this->fileDownloadDownloadedFileInfo['content']);
$lines = preg_split('/\R/', (string) $this->fileDownloadDownloadedFileInfo['content']);
foreach ($lines as $line) {
if (preg_match('/^\/.+\/[a-z]*$/i', $string)) {
if (preg_match($string, $line)) {
Expand Down Expand Up @@ -297,7 +299,7 @@ protected function fileDownloadProcess(string $url, array $options = []): array
$url_file_name = parse_url($url, PHP_URL_PATH);
$url_file_name = $url_file_name ? basename($url_file_name) : $url_file_name;
$headers['file_name'] = empty($headers['file_name']) && !empty($url_file_name) ? $url_file_name : $headers['file_name'];
$file_path = !empty($headers['file_name']) ? $dir . DIRECTORY_SEPARATOR . $headers['file_name'] : tempnam($dir, 'behat');
$file_path = empty($headers['file_name']) ? tempnam($dir, 'behat') : $dir . DIRECTORY_SEPARATOR . $headers['file_name'];
$file_name = basename($file_path);

// Write file contents.
Expand All @@ -323,12 +325,12 @@ protected function fileDownloadProcess(string $url, array $options = []): array
protected function fileDownloadParseHeaders(array $headers): array {
$parsed_headers = [];
foreach ($headers as $header) {
if (preg_match('/Content-Disposition:\s*attachment;\s*filename\s*=\s*\"([^"]+)"/', $header, $matches) && isset($matches[1])) {
if (preg_match('/Content-Disposition:\s*attachment;\s*filename\s*=\s*\"([^"]+)"/', (string) $header, $matches) && isset($matches[1])) {
$parsed_headers['file_name'] = trim($matches[1]);
continue;
}

if (preg_match('/Content-Type:\s*(.+)/', $header, $matches) && isset($matches[1])) {
if (preg_match('/Content-Type:\s*(.+)/', (string) $header, $matches) && isset($matches[1])) {
$parsed_headers['content_type'] = trim($matches[1]);
continue;
}
Expand Down
Loading

0 comments on commit ecb5fed

Please sign in to comment.