Completed
Push — develop ( 183c36...cfc9ab )
by Stéphane
02:42
created

Rules::testAddDefaultRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.3142
cc 1
eloc 17
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\Rules as SUT;
15
use Bee4\RobotsTxt as LUT;
16
17
/**
18
 * Rules unit test
19
 */
20
class Rules extends atoum
21
{
22
    public function testDefaultUa()
23
    {
24
        $this
25
            ->given(
26
                $sut = new SUT(),
27
                $ua = SUT::DEFAULT_UA
28
            )
29
            ->when($rule = $sut->get('Invalid UA'))
30
            ->then
31
                ->integer(count($sut))
32
                    ->isEqualTo(1)
33
                ->object($rule)
34
                    ->isInstanceOf('Bee4\RobotsTxt\Rule')
35
                ->string($rule->getUserAgent())
36
                    ->isEqualTo($ua)
37
                ->boolean($sut->match($ua, '/url'))
38
                    ->isTrue();
39
    }
40
41
    public function testAddDefaultRule()
42
    {
43
        $this
44
            ->given(
45
                $sut = new SUT(),
46
                $ua = SUT::DEFAULT_UA,
47
                $rule = new LUT\Rule($ua)
48
            )
49
            ->when(
50
                $sut->add($rule),
51
                $get = $sut->get($ua)
52
            )
53
            ->then
54
                ->integer(count($sut))
55
                    ->isEqualTo(1)
56
                ->object($get)
57
                    ->isInstanceOf('Bee4\RobotsTxt\Rule')
58
                    ->isEqualTo($rule)
59
                ->boolean($sut->match($ua, '/url'))
60
                    ->isTrue();
61
    }
62
63
    public function testAddRule()
64
    {
65
        $this
66
            ->given(
67
                $sut = new SUT(),
68
                $ua = 'Google-Bot',
69
                $rule = new LUT\Rule($ua),
70
                $rule->allow('/url'),
71
                $rule->disallow('/private')
72
            )
73
            ->when(
74
                $sut->add($rule),
75
                $get = $sut->get($ua)
76
            )
77
            ->then
78
                ->integer(count($sut))
79
                    ->isEqualTo(2)
80
                ->object($get)
81
                    ->isInstanceOf('Bee4\RobotsTxt\Rule')
82
                    ->isEqualTo($rule)
83
                ->boolean($sut->match($ua, '/url'))
84
                    ->isTrue()
85
                ->boolean($sut->match($ua, '/private'))
86
                    ->isFalse();
87
    }
88
89
    public function testDuplicateRuleException()
90
    {
91
        $this
92
            ->given(
93
                $sut = new SUT(),
94
                $ua = 'Google-Bot',
95
                $rule = new LUT\Rule($ua),
96
                $sut->add($rule)
97
            )
98
            ->exception(function () use ($sut, $rule) {
99
                $sut->add($rule);
100
            })
101
                ->isInstanceOf('Bee4\RobotsTxt\Exception\DuplicateRuleException')
102
            ->then
103
                ->object($this->exception->getRule())
104
                    ->isEqualTo($rule);
105
    }
106
}
107