Completed
Push — master ( faf631...885177 )
by Konstantin
11s
created

GherkinFileLoaderTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 102
wmc 6
lcom 1
cbo 7
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testSupports() 0 10 1
A testLoad() 0 12 1
A testParsingUncachedFeature() 0 16 1
A setUp() 0 8 1
A testParsingCachedFeature() 0 21 1
A testBasePath() 0 21 1
1
<?php
2
3
namespace Tests\Behat\Gherkin\Loader;
4
5
use Behat\Gherkin\Keywords\CucumberKeywords;
6
use Behat\Gherkin\Lexer;
7
use Behat\Gherkin\Loader\GherkinFileLoader;
8
use Behat\Gherkin\Parser;
9
10
class GherkinFileLoaderTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var GherkinFileLoader
14
     */
15
    private $loader;
16
    private $featuresPath;
17
18
    public function testSupports()
19
    {
20
        $this->assertFalse($this->loader->supports('non-existent path'));
21
        $this->assertFalse($this->loader->supports('non-existent path:2'));
22
23
        $this->assertFalse($this->loader->supports(__DIR__));
24
        $this->assertFalse($this->loader->supports(__DIR__ . ':d'));
25
        $this->assertFalse($this->loader->supports(__FILE__));
26
        $this->assertTrue($this->loader->supports(__DIR__ . '/../Fixtures/features/pystring.feature'));
27
    }
28
29
    public function testLoad()
30
    {
31
        $features = $this->loader->load($this->featuresPath . '/pystring.feature');
32
        $this->assertEquals(1, count($features));
33
        $this->assertEquals('A py string feature', $features[0]->getTitle());
34
        $this->assertEquals($this->featuresPath . DIRECTORY_SEPARATOR . 'pystring.feature', $features[0]->getFile());
35
36
        $features = $this->loader->load($this->featuresPath . '/multiline_name.feature');
37
        $this->assertEquals(1, count($features));
38
        $this->assertEquals('multiline', $features[0]->getTitle());
39
        $this->assertEquals($this->featuresPath . DIRECTORY_SEPARATOR . 'multiline_name.feature', $features[0]->getFile());
40
    }
41
42
    public function testParsingUncachedFeature()
43
    {
44
        $cache = $this->getMockBuilder('Behat\Gherkin\Cache\CacheInterface')->getMock();
45
        $this->loader->setCache($cache);
46
47
        $cache->expects($this->once())
48
            ->method('isFresh')
49
            ->with($path = $this->featuresPath . DIRECTORY_SEPARATOR . 'pystring.feature', filemtime($path))
50
            ->will($this->returnValue(false));
51
52
        $cache->expects($this->once())
53
            ->method('write');
54
55
        $features = $this->loader->load($this->featuresPath . '/pystring.feature');
56
        $this->assertEquals(1, count($features));
57
    }
58
59
    public function testParsingCachedFeature()
60
    {
61
        $cache = $this->getMockBuilder('Behat\Gherkin\Cache\CacheInterface')->getMock();
62
        $this->loader->setCache($cache);
63
64
        $cache->expects($this->once())
65
            ->method('isFresh')
66
            ->with($path = $this->featuresPath . DIRECTORY_SEPARATOR . 'pystring.feature', filemtime($path))
67
            ->will($this->returnValue(true));
68
69
        $cache->expects($this->once())
70
            ->method('read')
71
            ->with($path)
72
            ->will($this->returnValue('cache'));
73
74
        $cache->expects($this->never())
75
            ->method('write');
76
77
        $features = $this->loader->load($this->featuresPath . '/pystring.feature');
78
        $this->assertEquals('cache', $features[0]);
79
    }
80
81
    public function testBasePath()
82
    {
83
        $this->assertFalse($this->loader->supports('features'));
84
        $this->assertFalse($this->loader->supports('tables.feature'));
85
86
        $this->loader->setBasePath($this->featuresPath . '/../');
87
        $this->assertFalse($this->loader->supports('features'));
88
        $this->assertFalse($this->loader->supports('tables.feature'));
89
        $this->assertTrue($this->loader->supports('features/tables.feature'));
90
91
        $features = $this->loader->load('features/pystring.feature');
92
        $this->assertEquals(1, count($features));
93
        $this->assertEquals('A py string feature', $features[0]->getTitle());
94
        $this->assertEquals('features' . DIRECTORY_SEPARATOR . 'pystring.feature', $features[0]->getFile());
95
96
        $this->loader->setBasePath($this->featuresPath);
97
        $features = $this->loader->load('multiline_name.feature');
98
        $this->assertEquals(1, count($features));
99
        $this->assertEquals('multiline', $features[0]->getTitle());
100
        $this->assertEquals('multiline_name.feature', $features[0]->getFile());
101
    }
102
103
    protected function setUp()
104
    {
105
        $keywords = new CucumberKeywords(__DIR__ . '/../Fixtures/i18n.yml');
106
        $parser = new Parser(new Lexer($keywords));
107
        $this->loader = new GherkinFileLoader($parser);
108
109
        $this->featuresPath = realpath(__DIR__ . '/../Fixtures/features');
110
    }
111
}
112