Skip to content

Commit

Permalink
Increased test coverage. (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 authored Jul 25, 2024
1 parent ef34030 commit 7bd4a09
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ public function iSaveSizedScreenshot(string|int $width = 1440, string|int $heigh
$this->iSaveScreenshot();
}

/**
* Get before step scope.
*
* @return \Behat\Behat\Hook\Scope\BeforeStepScope
* The before step scope.
*/
public function getBeforeStepScope(): BeforeStepScope {
return $this->beforeStepScope;
}

/**
* Get current timestamp.
*
* @return int
* Current timestamp.
*/
public function getCurrentTime(): int {
return time();
}

/**
* Save screenshot data into a file.
*
Expand Down Expand Up @@ -269,8 +289,8 @@ protected function makeFileName(string $ext, string $filename = NULL, bool $fail
$filename .= '.{ext}';
}

$feature = $this->beforeStepScope->getFeature();
$step = $this->beforeStepScope->getStep();
$feature = $this->getBeforeStepScope()->getFeature();
$step = $this->getBeforeStepScope()->getStep();

try {
$url = $this->getSession()->getCurrentUrl();
Expand All @@ -285,7 +305,7 @@ protected function makeFileName(string $ext, string $filename = NULL, bool $fail
'step_line' => $step->getLine(),
'feature_file' => $feature->getFile(),
'url' => $url,
'time' => time(),
'time' => $this->getCurrentTime(),
'fail_prefix' => $this->failPrefix,
];

Expand Down
65 changes: 65 additions & 0 deletions tests/phpunit/Unit/BehatScreenshotExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatScreenshot\Tests\Unit;

use DrevOps\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer;
use DrevOps\BehatScreenshotExtension\ServiceContainer\BehatScreenshotExtension;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Test BehatScreenshotExtension.
*/
#[CoversClass(BehatScreenshotExtension::class)]
class BehatScreenshotExtensionTest extends TestCase {

public function testGetConfigKey(): void {
$extension = new BehatScreenshotExtension();
$this->assertEquals('drevops_screenshot', $extension->getConfigKey());
}

public function testLoad(): void {
$container = new ContainerBuilder();
$config = [
'dir' => '%paths.base%/screenshots',
'fail' => TRUE,
'fail_prefix' => 'failed_',
'purge' => FALSE,
'filenamePattern' => '{datetime:U}.{feature_file}.feature_{step_line}.{ext}',
'filenamePatternFailed' => '{datetime:U}.{fail_prefix}{feature_file}.feature_{step_line}.{ext}',
];

$extension = new BehatScreenshotExtension();
$extension->load($container, $config);

$this->assertTrue($container->hasDefinition('drevops_screenshot.screenshot_context_initializer'));

$definition = $container->getDefinition('drevops_screenshot.screenshot_context_initializer');
$this->assertEquals(ScreenshotContextInitializer::class, $definition->getClass());
$this->assertEquals(
[
$config['dir'],
$config['fail'],
$config['fail_prefix'],
$config['purge'],
$config['filenamePattern'],
$config['filenamePatternFailed'],
],
$definition->getArguments()
);
}

public function testConfigure(): void {
$builder = new ArrayNodeDefinition('root');

$extension = new BehatScreenshotExtension();
$extension->configure($builder);

$this->assertCount(6, $builder->getChildNodeDefinitions());
}

}
Loading

0 comments on commit 7bd4a09

Please sign in to comment.