|
| 1 | +<?php |
| 2 | + |
| 3 | +use mageekguy\atoum\asserter; |
| 4 | +use Behat\Behat\Context\Context; |
| 5 | +use Behat\Gherkin\Node\PyStringNode; |
| 6 | +use Symfony\Component\Process\Process; |
| 7 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test workflow totally copied from https://github.com/Behat/WebApiExtension/blob/master/features/bootstrap/FeatureContext.php |
| 11 | + */ |
| 12 | +class FeatureContext implements Context |
| 13 | +{ |
| 14 | + private $phpBin; |
| 15 | + |
| 16 | + private $process; |
| 17 | + |
| 18 | + private $workingDir; |
| 19 | + |
| 20 | + private $asserter; |
| 21 | + |
| 22 | + private $moco; |
| 23 | + |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + $this->asserter = new asserter\generator(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @BeforeSuite |
| 31 | + * @AfterSuite |
| 32 | + */ |
| 33 | + public static function cleanTestFolders() |
| 34 | + { |
| 35 | + $dir = self::workingDir(); |
| 36 | + |
| 37 | + if (is_dir($dir)) { |
| 38 | + self::clearDirectory($dir); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @BeforeScenario |
| 44 | + */ |
| 45 | + public function prepareScenario() |
| 46 | + { |
| 47 | + $dir = self::workingDir() . DIRECTORY_SEPARATOR . md5(microtime() * rand(0, 10000)); |
| 48 | + mkdir($dir . '/features/bootstrap', 0777, true); |
| 49 | + |
| 50 | + $phpFinder = new PhpExecutableFinder(); |
| 51 | + |
| 52 | + if (false === $php = $phpFinder->find()) { |
| 53 | + throw new \RuntimeException('Unable to find the PHP executable.'); |
| 54 | + } |
| 55 | + |
| 56 | + $this->workingDir = $dir; |
| 57 | + $this->phpBin = $php; |
| 58 | + $this->process = new Process(null); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @Given /^a file named "(?P<filename>[^"]*)" with:$/ |
| 63 | + */ |
| 64 | + public function aFileNamedWith($filename, PyStringNode $fileContent) |
| 65 | + { |
| 66 | + $content = strtr((string) $fileContent, array("'''" => '"""')); |
| 67 | + $this->createFile($this->workingDir . '/' . $filename, $content); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @Given I start moco |
| 72 | + */ |
| 73 | + public function iStartMoco() |
| 74 | + { |
| 75 | + $fixturesFile = sprintf('%s/features/fixtures.yml', $this->workingDir); |
| 76 | + file_put_contents($fixturesFile, '[]'); |
| 77 | + $this->moco = new Process(sprintf('bin/moco start -p 9999 -c %s', $fixturesFile)); |
| 78 | + $this->moco->start(); |
| 79 | + sleep(2); // Let moco start |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @When /^I run behat "(?P<arguments>[^"]*)"$/ |
| 84 | + */ |
| 85 | + public function iRunBehat($arguments) |
| 86 | + { |
| 87 | + $argumentsString = strtr($arguments, array('\'' => '"')); |
| 88 | + $this->process->setWorkingDirectory($this->workingDir); |
| 89 | + $this->process->setCommandLine(sprintf( |
| 90 | + '%s %s %s %s', |
| 91 | + $this->phpBin, |
| 92 | + escapeshellarg(BEHAT_BIN_PATH), |
| 93 | + $argumentsString, |
| 94 | + strtr('--no-colors', array('\'' => '"', '"' => '\"')) |
| 95 | + )); |
| 96 | + $this->process->start(); |
| 97 | + $this->process->wait(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @Then /^it should (fail|pass) with:$/ |
| 102 | + */ |
| 103 | + public function itShouldTerminateWithStatusAndContent($exitStatus, PyStringNode $string) |
| 104 | + { |
| 105 | +// echo sprintf('%s/features/fixtures.yml', $this->workingDir); |
| 106 | + |
| 107 | +// var_dump($this->moco->getOutput()); |
| 108 | +// var_dump($this->moco->getErrorOutput()); |
| 109 | +// var_dump($this->getOutput());exit; |
| 110 | + if ('fail' === $exitStatus) { |
| 111 | + $this->asserter->variable($this->getExitCode())->isEqualTo(1); |
| 112 | + } elseif ('pass' === $exitStatus) { |
| 113 | + $this->asserter->variable($this->getExitCode())->isEqualTo(0); |
| 114 | + } else { |
| 115 | + throw new \LogicException('Accepts only "fail" or "pass"'); |
| 116 | + } |
| 117 | + $this->asserter->string($this->getOutput())->contains((string) $string); |
| 118 | + } |
| 119 | + |
| 120 | + private function getExitCode() |
| 121 | + { |
| 122 | + return $this->process->getExitCode(); |
| 123 | + } |
| 124 | + |
| 125 | + private function getOutput() |
| 126 | + { |
| 127 | + $output = $this->process->getErrorOutput() . $this->process->getOutput(); |
| 128 | + |
| 129 | + // Normalize the line endings in the output |
| 130 | + if ("\n" !== PHP_EOL) { |
| 131 | + $output = str_replace(PHP_EOL, "\n", $output); |
| 132 | + } |
| 133 | + |
| 134 | + return trim(preg_replace("/ +$/m", '', $output)); |
| 135 | + } |
| 136 | + |
| 137 | + private function createFile($filename, $content) |
| 138 | + { |
| 139 | + $path = dirname($filename); |
| 140 | + |
| 141 | + if (!is_dir($path)) { |
| 142 | + mkdir($path, 0777, true); |
| 143 | + } |
| 144 | + |
| 145 | + file_put_contents($filename, $content); |
| 146 | + } |
| 147 | + |
| 148 | + public static function workingDir() |
| 149 | + { |
| 150 | + return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'json-api-behat'; |
| 151 | + } |
| 152 | + |
| 153 | + private static function clearDirectory($path) |
| 154 | + { |
| 155 | + $files = scandir($path); |
| 156 | + array_shift($files); |
| 157 | + array_shift($files); |
| 158 | + |
| 159 | + foreach ($files as $file) { |
| 160 | + $file = $path . DIRECTORY_SEPARATOR . $file; |
| 161 | + if (is_dir($file)) { |
| 162 | + self::clearDirectory($file); |
| 163 | + } else { |
| 164 | + unlink($file); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + rmdir($path); |
| 169 | + } |
| 170 | +} |
0 commit comments