Skip to content

Commit

Permalink
Updated code to use Drupal coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Mar 14, 2024
1 parent a5d84aa commit 28c7f3f
Show file tree
Hide file tree
Showing 15 changed files with 1,786 additions and 1,854 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ root = true
[*]
end_of_line = LF
indent_style = space
indent_size = 4
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
"symfony/http-client": "^6.0 || ^7.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8",
"behat/mink-browserkit-driver": "^2.2",
"behat/mink-selenium2-driver": "^1.7",
"dealerdirect/phpcodesniffer-composer-installer": "^1",
"drevops/behat-phpserver": "^1.2",
"drupal/coder": "^8.3",
"escapestudios/symfony2-coding-standard": "^3",
"mikey179/vfsstream": "^1.6",
"opis/closure": "^3.6",
"phpmd/phpmd": "^2.13",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^11.0",
"phpunit/phpunit": "^10",
"rector/rector": "^1.0.0",
"squizlabs/php_codesniffer": "^3",
"symfony/process": "^6.4 || ^7.0"
},
"autoload": {
Expand All @@ -43,6 +46,7 @@
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
Expand Down
35 changes: 23 additions & 12 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
<?xml version="1.0"?>
<ruleset name="custom">
<description>Custom PHPCS standard.</description>

<!-- Coding standard. -->
<rule ref="Drupal">
<exclude name="Drupal.Commenting.ClassComment.Short"/>
</rule>

<!-- Show sniff codes in all reports -->
<arg value="s"/>
<!-- Show progress of the run. -->
<arg value="p"/>
<arg name="colors"/>

<rule ref="Symfony"/>

<file>src</file>
<file>tests</file>
<file>tests/phpunit</file>
<file>tests/behat/bootstrap</file>

<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>./tests/*</exclude-pattern>
</rule>
<rule ref="Symfony.NamingConventions.ValidClassName.InvalidInterfaceName">
<exclude-pattern>.*context.*</exclude-pattern>
<!-- Allow long array lines in tests. -->
<rule ref="Drupal.Arrays.Array.LongLineDeclaration">
<exclude-pattern>*.Test\.php</exclude-pattern>
<exclude-pattern>*.TestCase\.php</exclude-pattern>
<exclude-pattern>*.test</exclude-pattern>
</rule>
<rule ref="Symfony.Commenting.FunctionComment.MissingParamTag">
<exclude-pattern>*</exclude-pattern>
</rule>
<rule ref="Symfony.Commenting.Annotations.Invalid">
<exclude-pattern>.*context.*</exclude-pattern>

<!-- Allow missing function names in tests. -->
<rule ref="Drupal.Commenting.FunctionComment.Missing">
<exclude-pattern>*.Test\.php</exclude-pattern>
<exclude-pattern>*.TestCase\.php</exclude-pattern>
<exclude-pattern>*.test</exclude-pattern>
</rule> <!-- Allow missing function names in tests. -->

<rule ref="Drupal.Commenting.DocComment.MissingShort">
<exclude-pattern>tests/behat/bootstrap/BehatCliTrait.php</exclude-pattern>
</rule>
</ruleset>
14 changes: 13 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ parameters:

paths:
- src
- tests/phpunit
- tests/behat/bootstrap

excludePaths:
- vendor/*
- node_modules/*
- tests/behat/bootstrap/BehatCliContext.php

ignoreErrors:
-
# Since tests and data providers do not have to have parameter docblocks,
# it is not possible to specify the type of the parameter, so we ignore
# this error.
message: '#.*no value type specified in iterable type array.#'
path: tests/phpunit/*
reportUnmatched: false
-
message: '#.*#'
path: tests/behat/bootstrap/BehatCliContext.php
Original file line number Diff line number Diff line change
@@ -1,92 +1,90 @@
<?php
/**
* @file
* This file is part of the DrevOps\BehatScreenshot package.
*/

namespace DrevOps\BehatScreenshotExtension\Context\Initializer;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Initializer\ContextInitializer;
use DrevOps\BehatScreenshotExtension\Context\ScreenshotAwareContext;
use DrevOps\BehatScreenshotExtension\Context\ScreenshotAwareContextInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

/**
* Class ScreenshotContextInitializer.
*/
class ScreenshotContextInitializer implements ContextInitializer
{
class ScreenshotContextInitializer implements ContextInitializer {

/**
* ScreenshotContextInitializer constructor.
*
* @param string $dir
* Screenshot dir.
* @param bool $fail
* Screenshot when fail.
* @param string $failPrefix
* File name prefix for a failed test.
* @param bool $purge
* Purge dir before start script.
* @param string $filenamePattern
* File name pattern.
* @param string $filenamePatternFailed
* File name pattern failed.
* @param bool $needsPurging
* Check if need to actually purge.
*/
public function __construct(protected string $dir, protected bool $fail, private readonly string $failPrefix, protected bool $purge, protected string $filenamePattern, protected string $filenamePatternFailed, protected bool $needsPurging = TRUE) {
}

/**
* ScreenshotContextInitializer constructor.
*
* @param string $dir Screenshot dir.
* @param bool $fail Screenshot when fail.
* @param string $failPrefix File name prefix for a failed test.
* @param bool $purge Purge dir before start script.
* @param string $filenamePattern File name pattern.
* @param string $filenamePatternFailed File name pattern failed.
* @param bool $needsPurging Check if need to actually purge.
*/
public function __construct(protected string $dir, protected bool $fail, private readonly string $failPrefix, protected bool $purge, protected string $filenamePattern, protected string $filenamePatternFailed, protected bool $needsPurging = true)
{
/**
* {@inheritdoc}
*/
public function initializeContext(Context $context): void {
if ($context instanceof ScreenshotAwareContextInterface) {
$dir = $this->resolveScreenshotDir();
$context->setScreenshotParameters($dir, $this->fail, $this->failPrefix, $this->filenamePattern, $this->filenamePatternFailed);
if ($this->shouldPurge() && $this->needsPurging) {
$this->purgeFilesInDir($dir);
$this->needsPurging = FALSE;
}
}
}

/**
* {@inheritdoc}
*/
public function initializeContext(Context $context): void
{
if ($context instanceof ScreenshotAwareContext) {
$dir = $this->resolveScreenshotDir();
$context->setScreenshotParameters($dir, $this->fail, $this->failPrefix, $this->filenamePattern, $this->filenamePatternFailed);
if ($this->shouldPurge() && $this->needsPurging) {
$this->purgeFilesInDir($dir);
$this->needsPurging = false;
}
}
/**
* Remove files in directory.
*
* @param string $dir
* Directory to purge files in.
*/
protected function purgeFilesInDir(string $dir): void {
$fs = new Filesystem();
$finder = new Finder();
if ($fs->exists($dir)) {
$fs->remove($finder->files()->in($dir));
}
}

/**
* Remove files in directory.
*
* @param string $dir
* Directory to purge files in.
*/
protected function purgeFilesInDir(string $dir): void
{
$fs = new Filesystem();
$finder = new Finder();
if ($fs->exists($dir)) {
$fs->remove($finder->files()->in($dir));
}
/**
* Resolve directory using one of supported paths.
*
* @return string
* Path to the screenshots directory.
*/
protected function resolveScreenshotDir(): string {
$dir = getenv('BEHAT_SCREENSHOT_DIR');
if (!empty($dir)) {
return $dir;
}

/**
* Resolve directory using one of supported paths.
*
* @return string
* Path to the screenshots directory.
*/
protected function resolveScreenshotDir(): string
{
$dir = getenv('BEHAT_SCREENSHOT_DIR');
if (!empty($dir)) {
return $dir;
}
return $this->dir;
}

return $this->dir;
}
/**
* Decide if 'purge' flag was set.
*
* @return bool
* TRUE if should purge, FALSE otherwise.
*/
protected function shouldPurge(): bool {
return getenv('BEHAT_SCREENSHOT_PURGE') || $this->purge;
}

/**
* Decide if 'purge' flag was set.
*
* @return bool
* TRUE if should purge, FALSE otherwise.
*/
protected function shouldPurge(): bool
{
return getenv('BEHAT_SCREENSHOT_PURGE') || $this->purge;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace DrevOps\BehatScreenshotExtension\Context;

use Behat\Behat\Context\Context;

/**
* Interface ScreenshotContext.
*/
interface ScreenshotAwareContextInterface extends Context {

/**
* Set context parameters.
*
* @param string $dir
* Directory to store screenshots.
* @param bool $fail
* Create screenshots on fail.
* @param string $failPrefix
* File name prefix for a failed test.
* @param string $filenamePattern
* File name pattern.
* @param string $filenamePatternFailed
* File name pattern failed.
*
* @return $this
*/
public function setScreenshotParameters(string $dir, bool $fail, string $failPrefix, string $filenamePattern, string $filenamePatternFailed): static;

}
Loading

0 comments on commit 28c7f3f

Please sign in to comment.