diff --git a/README.md b/README.md index f2da14e..a4eef7a 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 ----- diff --git a/spec/Bex/Behat/Context/TestRunnerContextSpec.php b/spec/Bex/Behat/Context/TestRunnerContextSpec.php index d64b04c..7c16a45 100644 --- a/spec/Bex/Behat/Context/TestRunnerContextSpec.php +++ b/spec/Bex/Behat/Context/TestRunnerContextSpec.php @@ -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); @@ -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(); @@ -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(); @@ -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) diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 1fcb376..50fa866 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -60,25 +60,31 @@ 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. @@ -86,15 +92,19 @@ class TestRunnerContext implements SnippetAcceptingContext * @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); } /** @@ -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); + } } /** @@ -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 = []; } /** @@ -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, @@ -179,10 +204,9 @@ 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; } /** @@ -190,21 +214,19 @@ public function iHaveTheConfiguration(PyStringNode $config) */ 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; } /** @@ -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; } /**