Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility layer for cucumber/gherkin #253

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
32 changes: 28 additions & 4 deletions src/Behat/Gherkin/Loader/CucumberGherkinLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Behat\Gherkin\Loader;

use Behat\Gherkin\Cache\CacheInterface;
use Behat\Gherkin\Cucumber\BackgroundNodeMapper;
use Behat\Gherkin\Cucumber\ExampleTableNodeMapper;
use Behat\Gherkin\Cucumber\FeatureNodeMapper;
Expand All @@ -27,6 +28,11 @@ final class CucumberGherkinLoader extends AbstractFileLoader
*/
private $parser;

/**
* @var ?CacheInterface
*/
protected $cache;
ciaranmcnulty marked this conversation as resolved.
Show resolved Hide resolved

public function __construct()
{
$tagMapper = new TagMapper();
Expand Down Expand Up @@ -74,6 +80,16 @@ public static function isAvailable() : bool
return class_exists(GherkinParser::class);
}

/**
* Sets cache layer.
*
* @param CacheInterface $cache Cache layer
*/
public function setCache(CacheInterface $cache)
{
$this->cache = $cache;
}

/**
* Loads features from provided resource.
*
Expand All @@ -83,18 +99,26 @@ public static function isAvailable() : bool
*/
public function load($resource)
{
$features = [];
$path = $this->findAbsolutePath($resource);

if ($this->cache && $this->cache->isFresh($path, filemtime($path))) {
return [$this->cache->read($path)];
}

$envelopes = $this->parser->parseString($this->findAbsolutePath($resource), file_get_contents($resource));
$envelopes = $this->parser->parseString($path, file_get_contents($path));
foreach ($envelopes as $envelope) {
if ($envelope->gherkinDocument) {
if ($feature = $this->mapper->map($envelope->gherkinDocument)) {
$features[] = $feature;
break;
}
}
}

return $features;
if ($this->cache) {
$this->cache->write($path, $feature);
}

return [$feature];
}

}