Skip to content

Commit

Permalink
Fixed static lint code issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 16, 2023
1 parent d14f6f5 commit e509359
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
command: docker compose exec phpserver composer install --ansi --no-suggest
- run:
name: Lint code.
command: docker compose exec phpserver composer lint || true
command: docker compose exec phpserver composer lint
- run:
name: Run tests.
command: docker compose exec phpserver composer test
Expand Down
9 changes: 1 addition & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ parameters:
excludePaths:
- vendor/*
- node_modules/*

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/*
- tests/behat/bootstrap/BehatCliContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\Finder\Finder;

/**
* Class ScreenshotContextInitializer
* Class ScreenshotContextInitializer.
*/
class ScreenshotContextInitializer implements ContextInitializer
{
Expand Down Expand Up @@ -58,10 +58,10 @@ class ScreenshotContextInitializer implements ContextInitializer
*
* @param string $dir Screenshot dir.
* @param bool $fail Screenshot when fail.
* @param bool $failPrefix File name prefix for a failed test.
* @param string $failPrefix File name prefix for a failed test.
* @param bool $purge Purge dir before start script.
*/
public function __construct($dir, $fail, $failPrefix, $purge)
public function __construct(string $dir, bool $fail, string $failPrefix, bool $purge)
{
$this->needsPurging = true;
$this->dir = $dir;
Expand All @@ -73,7 +73,7 @@ public function __construct($dir, $fail, $failPrefix, $purge)
/**
* {@inheritdoc}
*/
public function initializeContext(Context $context)
public function initializeContext(Context $context): void
{
if ($context instanceof ScreenshotAwareContext) {
$dir = $this->resolveScreenshotDir();
Expand All @@ -91,7 +91,7 @@ public function initializeContext(Context $context)
* @param string $dir
* Directory to purge files in.
*/
protected function purgeFilesInDir($dir)
protected function purgeFilesInDir(string $dir): void
{
$fs = new Filesystem();
$finder = new Finder();
Expand All @@ -106,7 +106,7 @@ protected function purgeFilesInDir($dir)
* @return string
* Path to the screenshots directory.
*/
protected function resolveScreenshotDir()
protected function resolveScreenshotDir(): string
{
$dir = getenv('BEHAT_SCREENSHOT_DIR');
if (!empty($dir)) {
Expand All @@ -122,7 +122,7 @@ protected function resolveScreenshotDir()
* @return bool
* TRUE if should purge, FALSE otherwise.
*/
protected function shouldPurge()
protected function shouldPurge(): bool
{
return getenv('BEHAT_SCREENSHOT_PURGE') || $this->purge;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ interface ScreenshotAwareContext extends Context
*
* @param string $dir Directory to store screenshots.
* @param bool $fail Create screenshots on fail.
* @param bool $failPrefix File name prefix for a failed test.
* @param string $failPrefix File name prefix for a failed test.
*
* @return $this
*/
public function setScreenshotParameters($dir, $fail, $failPrefix);
public function setScreenshotParameters(string $dir, bool $fail, string $failPrefix): static;
}
26 changes: 15 additions & 11 deletions src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContex
/**
* {@inheritdoc}
*/
public function setScreenshotParameters($dir, $fail, $failPrefix)
public function setScreenshotParameters(string $dir, bool $fail, string $failPrefix): static
{
$this->dir = $dir;
$this->fail = $fail;
Expand All @@ -77,7 +77,7 @@ public function setScreenshotParameters($dir, $fail, $failPrefix)
*
* @BeforeScenario
*/
public function beforeScenarioInit(BeforeScenarioScope $scope)
public function beforeScenarioInit(BeforeScenarioScope $scope): void
{
if ($scope->getScenario()->hasTag('javascript')) {
$driver = $this->getSession()->getDriver();
Expand All @@ -98,9 +98,13 @@ public function beforeScenarioInit(BeforeScenarioScope $scope)
*
* @BeforeStep
*/
public function beforeStepInit(BeforeStepScope $scope)
public function beforeStepInit(BeforeStepScope $scope): void
{
$this->featureFile = $scope->getFeature()->getFile();
$featureFile = $scope->getFeature()->getFile();
if (!$featureFile) {
throw new \RuntimeException('Feature file not found.');
}
$this->featureFile = $featureFile;
$this->stepLine = $scope->getStep()->getLine();
}

Expand All @@ -111,7 +115,7 @@ public function beforeStepInit(BeforeStepScope $scope)
*
* @AfterStep
*/
public function printLastResponseOnError(AfterStepScope $event)
public function printLastResponseOnError(AfterStepScope $event): void
{
if ($this->fail && !$event->getTestResult()->isPassed()) {
$this->iSaveScreenshot(true);
Expand All @@ -129,7 +133,7 @@ public function printLastResponseOnError(AfterStepScope $event)
* @When save screenshot
* @When I save screenshot
*/
public function iSaveScreenshot($fail = false)
public function iSaveScreenshot($fail = false): void
{
$driver = $this->getSession()->getDriver();

Expand Down Expand Up @@ -169,14 +173,14 @@ public function iSaveScreenshot($fail = false)
* @When save :width x :height screenshot
* @When I save :width x :height screenshot
*/
public function iSaveSizedScreenshot($width = 1440, $height = 900)
public function iSaveSizedScreenshot(string|int $width = 1440, string|int $height = 900): void
{
try {
$this->getSession()->resizeWindow((int) $width, (int) $height, 'current');
} catch (UnsupportedDriverActionException $exception) {
// Nothing to do here - drivers without resize support may proceed.
}
$this->iSaveScreenshot(false);
$this->iSaveScreenshot();
}

/**
Expand All @@ -187,7 +191,7 @@ public function iSaveSizedScreenshot($width = 1440, $height = 900)
* @param string $data
* Data to write into a file.
*/
protected function saveScreenshotData($filename, $data)
protected function saveScreenshotData(string $filename, string $data): void
{
$this->prepareDir($this->dir);
file_put_contents($this->dir.DIRECTORY_SEPARATOR.$filename, $data);
Expand All @@ -198,7 +202,7 @@ protected function saveScreenshotData($filename, $data)
*
* @param string $dir Name of preparing directory.
*/
protected function prepareDir($dir)
protected function prepareDir(string $dir): void
{
$fs = new Filesystem();
$fs->mkdir($dir, 0755);
Expand All @@ -214,7 +218,7 @@ protected function prepareDir($dir)
*
* @return string Unique file name.
*/
protected function makeFileName($ext, $prefix = '')
protected function makeFileName(string $ext, string $prefix = ''): string
{
return sprintf('%01.2f.%s%s_%s.%s', microtime(true), $prefix, basename($this->featureFile), $this->stepLine, $ext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeParentInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

Expand All @@ -28,41 +30,45 @@ class BehatScreenshotExtension implements ExtensionInterface
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
}

/**
* {@inheritdoc}
*/
public function getConfigKey()
public function getConfigKey(): string
{
return self::MOD_ID;
}

/**
* {@inheritdoc}
*/
public function initialize(ExtensionManager $extensionManager)
public function initialize(ExtensionManager $extensionManager): void
{
}

/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
public function configure(ArrayNodeDefinition $builder): void
{
$builder->children()
->scalarNode('dir')->cannotBeEmpty()->defaultValue('%paths.base%/screenshots')->end()
->scalarNode('fail')->cannotBeEmpty()->defaultValue(true)->end()
->scalarNode('fail_prefix')->cannotBeEmpty()->defaultValue('failed_')->end()
->scalarNode('purge')->cannotBeEmpty()->defaultValue(false)->end();
$definitionChildren = $builder->children();
if ($definitionChildren instanceof NodeBuilder) {
// @phpstan-ignore-next-line
$definitionChildren
->scalarNode('dir')->cannotBeEmpty()->defaultValue('%paths.base%/screenshots')->end()
->scalarNode('fail')->cannotBeEmpty()->defaultValue(true)->end()
->scalarNode('fail_prefix')->cannotBeEmpty()->defaultValue('failed_')->end()
->scalarNode('purge')->cannotBeEmpty()->defaultValue(false)->end();
}
}

/**
* {@inheritdoc}
*/
public function load(ContainerBuilder $container, array $config)
public function load(ContainerBuilder $container, array $config): void
{
$definition = new Definition('DrevOps\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer', [
$config['dir'],
Expand Down
5 changes: 3 additions & 2 deletions tests/behat/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
*/
class FeatureContext extends MinkContext implements Context
{

use ScreenshotTrait;

/**
* FeatureContext constructor.
*
* @param array $parameters Array of parameters from config.
* @param array<string> $parameters Array of parameters from config.
*/
public function __construct($parameters)
public function __construct(array $parameters)
{
$this->screenshotInitParams($parameters);
}
Expand Down
21 changes: 11 additions & 10 deletions tests/behat/bootstrap/ScreenshotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ trait ScreenshotTrait
/**
* Init test parameters.
*
* @param array $parameters Array of parameters from config.
* @param array<string> $parameters Array of parameters from config.
*/
public function screenshotInitParams($parameters)
public function screenshotInitParams(array $parameters): void
{
if (getenv('BEHAT_SCREENSHOT_DIR')) {
$this->screenshotDir = getenv('BEHAT_SCREENSHOT_DIR');
Expand All @@ -41,7 +41,7 @@ public function screenshotInitParams($parameters)
* @Given /^(?:|I )am on (?:|the )screenshot test page$/
* @Given /^(?:|I )go to (?:|the )screenshot test page$/
*/
public function goToScreenshotTestPage()
public function goToScreenshotTestPage(): void
{
$this->visitPath('screenshot.html');
}
Expand All @@ -53,7 +53,7 @@ public function goToScreenshotTestPage()
*
* @Given /^file wildcard "([^"]*)" should exist$/
*/
public function assertFileShouldExist($wildcard)
public function assertFileShouldExist(string $wildcard): void
{
$wildcard = $this->screenshotDir.DIRECTORY_SEPARATOR.$wildcard;
$matches = glob($wildcard);
Expand All @@ -70,7 +70,7 @@ public function assertFileShouldExist($wildcard)
*
* @Given /^file wildcard "([^"]*)" should not exist$/
*/
public function assertFileShouldNotExist($wildcard)
public function assertFileShouldNotExist(string $wildcard): void
{
$wildcard = $this->screenshotDir.DIRECTORY_SEPARATOR.$wildcard;
$matches = glob($wildcard);
Expand All @@ -85,11 +85,12 @@ public function assertFileShouldNotExist($wildcard)
*
* @Given I remove all files from screenshot directory
*/
public function emptyScreenshotDirectory()
public function emptyScreenshotDirectory(): void
{
array_map(
'unlink',
glob($this->screenshotDir.DIRECTORY_SEPARATOR.'/*')
);
$files = glob($this->screenshotDir.DIRECTORY_SEPARATOR.'/*');

if (!empty($files)) {
array_map('unlink', $files);
}
}
}

0 comments on commit e509359

Please sign in to comment.