Last active
June 11, 2021 08:05
-
-
Save macintoshplus/01d9e4b4ae0c668be7b292994a206836 to your computer and use it in GitHub Desktop.
Behat Hooks : require Symfony plugin and Symfony Page Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @copyright Macintoshplus (c) 2020 | |
* Added by : Macintoshplus at 10/04/2020 21:58 | |
*/ | |
declare(strict_types=1); | |
namespace App\Tests\Behat\Hook; | |
use Behat\Behat\Context\Context; | |
use Doctrine\Common\DataFixtures\Purger\ORMPurger; | |
use Doctrine\ORM\EntityManagerInterface; | |
class DoctrineContext implements Context | |
{ | |
/** @var EntityManagerInterface */ | |
private $entityManager; | |
public function __construct(EntityManagerInterface $entityManager) | |
{ | |
$this->entityManager = $entityManager; | |
} | |
/** | |
* @BeforeScenario | |
*/ | |
public function purgeDatabase() | |
{ | |
$this->entityManager->getConnection()->getConfiguration()->setSQLLogger(null); | |
$purger = new ORMPurger($this->entityManager); | |
$purger->purge(); | |
$this->entityManager->clear(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @copyright Macintoshplus (c) 2020 | |
* Added by : Macintoshplus at 09/04/2020 21:58 | |
*/ | |
declare(strict_types=1); | |
namespace App\Tests\Behat\Hook; | |
use Behat\Behat\Context\Context; | |
use Symfony\Component\Process\Process; | |
final class LoadFixturesContext implements Context | |
{ | |
/** | |
* @BeforeScenario | |
*/ | |
public function loadFixture() | |
{ | |
$cmd = new Process([\PHP_BINARY, 'bin/console', 'doctrine:fixtures:load', '-n']); | |
$cmd->run(); | |
if ($cmd->getExitCode() !== 0) { | |
dump($cmd->getOutput(), $cmd->getErrorOutput()); | |
throw new \Exception('Unable to load fixtures'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @copyright Macintoshplus (c) 2020 | |
* Added by : Macintoshplus at 09/04/2020 21:32 | |
*/ | |
declare(strict_types=1); | |
namespace App\Tests\Behat\Hook; | |
use Behat\Behat\Context\Context; | |
use Behat\Behat\Hook\Scope\AfterStepScope; | |
use App\Tests\Behat\Page\ScreenShotElement; | |
final class ScreenshotOnFailureContext implements Context | |
{ | |
/** | |
* @var string | |
*/ | |
private $screenshotDirectory; | |
/** | |
* @var ScreenShotElement | |
*/ | |
private $screenShotElement; | |
public function __construct(ScreenShotElement $screenShotElement, string $screenshotDirectory) | |
{ | |
$this->screenshotDirectory = $screenshotDirectory; | |
$this->screenShotElement = $screenShotElement; | |
} | |
/** | |
* @AfterStep | |
*/ | |
public function takeScreenshot(AfterStepScope $event) | |
{ | |
if ($event->getTestResult()->isPassed()) { | |
return; | |
} | |
$this->screenShotElement->takeScreenshot($this->screenshotDirectory, $event->getStep()->getText()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @copyright Macintoshplus (c) 2020 | |
* Added by : Macintoshplus at 09/04/2020 21:40 | |
*/ | |
declare(strict_types=1); | |
namespace App\Tests\Behat\Page; | |
use Behat\Mink\Exception\UnsupportedDriverActionException; | |
use FriendsOfBehat\PageObjectExtension\Element\Element; | |
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; | |
use Symfony\Component\Filesystem\Filesystem; | |
final class ScreenShotElement extends Element | |
{ | |
public function takeScreenshot(string $screenshotDirectory, ?string $filename = null) | |
{ | |
try { | |
if ($this->getSession()->isStarted() === false) { | |
return; | |
} | |
$filesystem = new Filesystem(); | |
$filesystem->mkdir($screenshotDirectory); | |
$screenshots = $this->getSession()->getScreenshot(); | |
$filename = $filename ?? uniqid(); | |
$filename = str_replace([' ', ':', '\\', '/', '<', '>', '"', "'"], '_', $filename); | |
$screenshotsName = $filename.'.png'; | |
$filesystem->dumpFile($screenshotDirectory . '/' . $screenshotsName, $screenshots); | |
} catch (IOExceptionInterface $exception) { | |
throw new \Exception('An error occurred while creating your directory at ' . $exception->getPath()); | |
} catch (UnsupportedDriverActionException $e) { | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment