Skip to content

Commit

Permalink
elvetemedve#58 Integration with Behat Cucumber Json Formatter (elvete…
Browse files Browse the repository at this point in the history
…medve#59)

* elvetemedve#58 Dispatch an event when file is uploaded.

* Class names were renamed.
  • Loading branch information
marcelovani authored Dec 1, 2022
1 parent 3df8d34 commit e4661f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Bex\Behat\ScreenshotExtension\Event;

use Symfony\Contracts\EventDispatcher\Event;

/**
* Dispatches an event when screenshots are uploaded.
*/
class ScreenshotUploadCompleteEvent extends Event
{
const NAME = 'screenshot.uploader.upload_complete';

protected $filename = '';

public function __construct(string $filename)
{
$this->filename = $filename;
}

public function getFilename()
{
return $this->filename;
}
}
15 changes: 14 additions & 1 deletion src/Bex/Behat/ScreenshotExtension/Service/ScreenshotUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface;
use Bex\Behat\ScreenshotExtension\ServiceContainer\Config;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Bex\Behat\ScreenshotExtension\Event\ScreenshotUploadCompleteEvent;

class ScreenshotUploader
{
Expand All @@ -18,14 +20,21 @@ class ScreenshotUploader
*/
private $config;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @param OutputInterface $output
* @param Config $config
* @param EventDispatcherInterface $eventDispatcher;
*/
public function __construct(OutputInterface $output, Config $config)
public function __construct(OutputInterface $output, Config $config, EventDispatcherInterface $eventDispatcher)
{
$this->output = $output;
$this->config = $config;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -36,6 +45,10 @@ public function upload($screenshot, $fileName = 'failure.png')
{
foreach ($this->config->getImageDrivers() as $imageDriver) {
$imageUrl = $imageDriver->upload($screenshot, $fileName);

// Dispatch an event for file upload.
$this->eventDispatcher->dispatch(new ScreenshotUploadCompleteEvent($imageUrl), ScreenshotUploadCompleteEvent::NAME);

$this->printImageLocation($imageUrl);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\EventDispatcher" public="true"/>

<service id="bex.screenshot_extension.config" class="Bex\Behat\ScreenshotExtension\ServiceContainer\Config" public="false" />

<service id="bex.screenshot_extension.filename_generator" class="Bex\Behat\ScreenshotExtension\Service\FilenameGenerator" public="false">
Expand All @@ -22,6 +24,7 @@
<service id="bex.screenshot_extension.screenshot_uploader" class="Bex\Behat\ScreenshotExtension\Service\ScreenshotUploader" public="false">
<argument type="service" id="bex.screenshot_extension.indented_output" />
<argument type="service" id="bex.screenshot_extension.config" />
<argument type="service" id="event_dispatcher" />
</service>

<!-- event listener -->
Expand Down

0 comments on commit e4661f2

Please sign in to comment.