Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from elvetemedve/feature/make-working-dir-confi…
Browse files Browse the repository at this point in the history
…gurable

Make the working directory configurable
  • Loading branch information
elvetemedve authored Apr 7, 2020
2 parents c3dfbb9 + e814e30 commit 4026204
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ default:
- Bex\Behat\Context\TestRunnerContext
```
You can configure the test web browser to be used for opening the pages, like this:
You can configure the test web browser to be used for opening the pages, like this (optional):
```yml
default:
suites:
Expand All @@ -40,6 +40,16 @@ default:
browserCommand: %paths.base%/bin/phantomjs --webdriver=4444
```
You can configure the working directory like this (optional):
```yml
default:
suites:
default:
contexts:
- Bex\Behat\Context\TestRunnerContext:
workingDirectory: path/to/your/working/dir # if not provided then a temporary working dir is autogenerated
```
Usage
-----
Expand Down
7 changes: 4 additions & 3 deletions spec/Bex/Behat/Context/TestRunnerContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestRunnerContextSpec extends ObjectBehavior
{
function let(Filesystem $filesystem, ProcessFactory $processFactory, Process $behatProcess)
{
$this->beConstructedWith('bin/phantomjs', $filesystem, $processFactory);
$this->beConstructedWith('bin/phantomjs', $filesystem, $processFactory, null);
$this->initFilesystemDouble($filesystem);
$this->initProcessFactoryDouble($processFactory, $behatProcess);

Expand Down Expand Up @@ -52,7 +52,7 @@ function it_creates_temporary_working_directory(Filesystem $filesystem)
function it_removes_temporary_working_directory(Filesystem $filesystem)
{
$tempDir = sys_get_temp_dir() .'/behat-test-runner';

$filesystem->remove(Argument::containingString($tempDir))->shouldBeCalled();

$this->createWorkingDirectory();
Expand Down Expand Up @@ -152,7 +152,7 @@ function it_does_not_run_browser_when_browser_binary_is_not_set(
Argument::any()
)->willReturn($browserProcess);

$this->beConstructedWith(null, $filesystem, $processFactory);
$this->beConstructedWith(null, $filesystem, $processFactory, null);

$webServerProcess->start()->shouldBeCalled();
$browserProcess->start()->shouldNotBeCalled();
Expand Down Expand Up @@ -187,6 +187,7 @@ private function initFilesystemDouble($filesystem)
{
$filesystem->remove(Argument::type('string'))->willReturn(null);
$filesystem->mkdir(Argument::type('string'), Argument::type('int'))->willReturn(null);
$filesystem->exists(Argument::type('string'))->willReturn(false);
}

private function initProcessFactoryDouble(ProcessFactory $processFactory, Process $behatProcess)
Expand Down
80 changes: 52 additions & 28 deletions src/TestRunnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,51 @@
class TestRunnerContext implements SnippetAcceptingContext
{
/** @var Filesystem $filesystem */
private $filesystem;
protected $filesystem;

/** @var string $workingDirectory Place for generated tests */
private $workingDirectory;
protected $workingDirectory;

/** @var string $documentRoot Root directory of the web server */
private $documentRoot;
protected $documentRoot;

/** @var Process[] $processes Active processes */
private $processes = [];
protected $processes = [];

/** @var Process $behatProcess Active behat process */
private $behatProcess;
protected $behatProcess;

/** @var string $browserCommand */
private $browserCommand;
protected $browserCommand;

/** @var ProcessFactory $processFactory */
private $processFactory;
protected $processFactory;

/** @var string[] List of created files */
protected $files = [];

/** @var bool */
protected $isAutogeneratedWorkingDirectory;

/**
* TestRunnerContext constructor.
*
* @param string|null $browserCommand Shell command which executes the tester browser
* @param Filesystem|null $fileSystem
* @param ProcessFactory|null $processFactory
* @param string|null $workingDirectory
*/
public function __construct(
$browserCommand = null,
Filesystem $fileSystem = null,
ProcessFactory $processFactory = null
ProcessFactory $processFactory = null,
$workingDirectory = null
) {
$this->browserCommand = $browserCommand;
$this->filesystem = $fileSystem ?: new Filesystem();
$this->processFactory = $processFactory ?: new ProcessFactory();
$this->workingDirectory = $workingDirectory;
$this->isAutogeneratedWorkingDirectory = ($workingDirectory === null);
}

/**
Expand All @@ -120,12 +130,21 @@ public function afterRunTests(AfterScenarioScope $scope)
*/
public function createWorkingDirectory()
{
$this->workingDirectory = tempnam(sys_get_temp_dir(), 'behat-test-runner');
$this->filesystem->remove($this->workingDirectory);
$this->filesystem->mkdir($this->workingDirectory . '/features/bootstrap', 0770);
if (empty($this->workingDirectory)) {
$this->workingDirectory = tempnam(sys_get_temp_dir(), 'behat-test-runner');
}

$featuresDirectory = $this->workingDirectory . '/features/bootstrap';

if (!$this->filesystem->exists($featuresDirectory)) {
$this->filesystem->mkdir($this->workingDirectory . '/features/bootstrap', 0770);
}

$this->documentRoot = $this->workingDirectory .'/document_root';
$this->filesystem->mkdir($this->documentRoot, 0770);

if (!$this->filesystem->exists($this->documentRoot)) {
$this->filesystem->mkdir($this->documentRoot, 0770);
}
}

/**
Expand All @@ -141,7 +160,13 @@ public function getWorkingDirectory()
*/
public function clearWorkingDirectory()
{
$this->filesystem->remove($this->workingDirectory);
if ($this->isAutogeneratedWorkingDirectory) {
$this->filesystem->remove($this->workingDirectory);
} else {
$this->filesystem->remove($this->files);
}

$this->files = [];
}

/**
Expand All @@ -164,7 +189,7 @@ public function destroyProcesses()
*/
public function printTesterOutputOnFailure($scope)
{
if (!$scope->getTestResult()->isPassed()) {
if ($this->behatProcess !== null && !$scope->getTestResult()->isPassed()) {
$outputFile = sys_get_temp_dir() . '/behat-test-runner.out';
$this->filesystem->dumpFile(
$outputFile,
Expand All @@ -179,32 +204,29 @@ public function printTesterOutputOnFailure($scope)
*/
public function iHaveTheConfiguration(PyStringNode $config)
{
$this->filesystem->dumpFile(
$this->workingDirectory.'/behat.yml',
$config->getRaw()
);
$file = $this->workingDirectory.'/behat.yml';
$this->filesystem->dumpFile($file, $config->getRaw());
$this->files[] = $file;
}

/**
* @Given I have the feature:
*/
public function iHaveTheFeature(PyStringNode $content)
{
$this->filesystem->dumpFile(
$this->workingDirectory.'/features/feature.feature',
$content->getRaw()
);
$file = $this->workingDirectory . '/features/feature.feature';
$this->filesystem->dumpFile($file, $content->getRaw());
$this->files[] = $file;
}

/**
* @Given I have the context:
*/
public function iHaveTheContext(PyStringNode $definition)
{
$this->filesystem->dumpFile(
$this->workingDirectory.'/features/bootstrap/FeatureContext.php',
$definition->getRaw()
);
$file = $this->workingDirectory.'/features/bootstrap/FeatureContext.php';
$this->filesystem->dumpFile($file, $definition->getRaw());
$this->files[] = $file;
}

/**
Expand All @@ -223,7 +245,9 @@ public function iRunBehat($parameters = '', $phpParameters = '')
*/
public function iHaveTheFileInDocumentRoot($filename, PyStringNode $content)
{
$this->filesystem->dumpFile($this->documentRoot .'/'. $filename, $content);
$file = $this->documentRoot .'/'. $filename;
$this->filesystem->dumpFile($file, $content);
$this->files[] = $file;
}

/**
Expand Down

0 comments on commit 4026204

Please sign in to comment.