Completed
Push — develop ( cfc9ab...e7bb49 )
by Stéphane
02:08
created

Parser::testParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the beebot package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2016
8
 * @author    Stephane HULARD <[email protected]>
9
 */
10
11
namespace Bee4\RobotsTxt\Test\Units;
12
13
use mageekguy\atoum;
14
use Bee4\RobotsTxt\Parser as SUT;
15
use Bee4\RobotsTxt as LUT;
16
17
/**
18
 * Parser unit test
19
 */
20
class Parser extends atoum
21
{
22
    public function testEmptyStringContent()
23
    {
24
        $this
25
            ->given($rules = SUT::parse(""))
26
            ->when($rule = $rules->get(LUT\Rules::DEFAULT_UA))
27
            ->then
28
                ->object($rules)
29
                    ->isInstanceOf('Bee4\RobotsTxt\Rules')
30
                ->object($rule)
31
                    ->isInstanceOf('Bee4\RobotsTxt\Rule');
32
    }
33
34
    public function testEmptyContentInstance()
35
    {
36
        $this
37
            ->given(
38
                $content = new LUT\Content(""),
39
                $rules = SUT::parse($content)
40
            )
41
            ->when($rule = $rules->get(LUT\Rules::DEFAULT_UA))
42
            ->then
43
                ->object($rules)
44
                    ->isInstanceOf('Bee4\RobotsTxt\Rules')
45
                ->object($rule)
46
                    ->isInstanceOf('Bee4\RobotsTxt\Rule');
47
    }
48
49
    public function testInvalidContentException()
50
    {
51
        $this->exception(function () {
52
            SUT::parse(new \stdClass);
53
        })
54
            ->isInstanceOf('Bee4\RobotsTxt\Exception\InvalidContentException');
55
    }
56
57
    public function testDuplicateRuleException()
58
    {
59
        $robot = <<<ROBOTS
60
User-Agent: *
61
Allow: /
62
63
User-Agent: *
64
Disallow: /toto
65
ROBOTS;
66
67
        $this
68
            ->given($content = new LUT\Content($robot))
69
            ->exception(function () use ($content) {
70
                SUT::parse($content);
71
            })
72
                ->isInstanceOf('Bee4\RobotsTxt\Exception\DuplicateRuleException');
73
    }
74
75
    public function testParse()
76
    {
77
        $robot = <<<ROBOTS
78
User-Agent: *
79
Allow: /
80
81
User-Agent: Google
82
Disallow: /toto
83
ROBOTS;
84
85
        $this
86
            ->given($rules = SUT::parse($robot))
87
            ->then
88
                ->sizeOf($rules)
89
                    ->isEqualTo(2)
90
                ->boolean($rules->match('*', '/page'))
91
                    ->isTrue()
92
                ->boolean($rules->match('Google', '/page'))
93
                    ->isTrue()
94
                ->boolean($rules->match('Google', '/toto/page'))
95
                    ->isFalse();
96
    }
97
}
98