Completed
Push — master ( e4c3c6...3f33e1 )
by Sam
03:51
created

PageTest::testUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
 * This file contains only the PageTest class.
4
 */
5
6
namespace Tests\Xtools;
7
8
use PHPUnit_Framework_TestCase;
9
10
/**
11
 * Tests for the Page class.
12
 */
13
class PageTest extends PHPUnit_Framework_TestCase
14
{
15
16
    /**
17
     * A page has a title and an HTML display title.
18
     */
19
    public function testTitles()
20
    {
21
        $project = new Project('TestProject');
22
        $pageRepo = $this->getMock(PagesRepository::class, ['getPageInfo']);
23
        $data = [
24
            [$project, 'Test_Page_1', true, ['title' => 'Test_Page_1']],
25
            [$project, 'Test_Page_2', true, ['title' => 'Test_Page_2', 'displaytitle' => '<em>Test</em> page 2']],
26
        ];
27
        $pageRepo->method('getPageInfo')->will($this->returnValueMap($data));
28
29
        // Page with no display title.
30
        $page = new Page($project, 'Test_Page_1');
31
        $page->setRepository($pageRepo);
32
        $this->assertEquals('Test_Page_1', $page->getTitle());
33
        $this->assertEquals('Test_Page_1', $page->getDisplayTitle());
34
35
        // Page with a display title.
36
        $page = new Page($project, 'Test_Page_2');
37
        $page->setRepository($pageRepo);
38
        $this->assertEquals('Test_Page_2', $page->getTitle());
39
        $this->assertEquals('<em>Test</em> page 2', $page->getDisplayTitle());
40
    }
41
42
    /**
43
     * A page either exists or doesn't.
44
     */
45
    public function testExists()
46
    {
47
        $pageRepo = $this->getMock(PagesRepository::class, ['getPageInfo']);
48
        $project = new Project('TestProject');
49
        // Mock data (last element of each array is the return value).
50
        $data = [
51
            [$project, 'Existing_page', true, []],
52
            [$project, 'Missing_page', true, ['missing' => '']],
53
        ];
54
        $pageRepo //->expects($this->exactly(2))
1 ignored issue
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
            ->method('getPageInfo')
56
            ->will($this->returnValueMap($data));
57
58
        // Existing page.
59
        $page1 = new Page($project, 'Existing_page');
60
        $page1->setRepository($pageRepo);
61
        $this->assertTrue($page1->exists());
62
63
        // Missing page.
64
        $page2 = new Page($project, 'Missing_page');
65
        $page2->setRepository($pageRepo);
66
        $this->assertFalse($page2->exists());
67
    }
68
69
    /**
70
     * A page has an integer ID on a given project.
71
     */
72 View Code Duplication
    public function testId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $pageRepo = $this->getMock(PagesRepository::class, ['getPageInfo']);
75
        $pageRepo->expects($this->once())
76
            ->method('getPageInfo')
77
            ->willReturn(['pageid' => '42']);
78
79
        $page = new Page(new Project('TestProject'), 'Test_Page');
80
        $page->setRepository($pageRepo);
81
        $this->assertEquals(42, $page->getId());
82
    }
83
84
    /**
85
     * A page has a URL.
86
     */
87 View Code Duplication
    public function testUrls()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $pageRepo = $this->getMock(PagesRepository::class, ['getPageInfo']);
90
        $pageRepo->expects($this->once())
91
            ->method('getPageInfo')
92
            ->willReturn(['fullurl' => 'https://example.org/Page']);
93
94
        $page = new Page(new Project('exampleWiki'), 'Page');
95
        $page->setRepository($pageRepo);
96
        $this->assertEquals('https://example.org/Page', $page->getUrl());
97
    }
98
99
    /**
100
     * A list of a single user's edits on this page can be retrieved, along with the count of
101
     * these revisions, and the total bytes added and removed.
102
     * @TODO this is not finished yet
103
     */
104
    public function testUsersEdits()
105
    {
106
        $pageRepo = $this->getMock(PagesRepository::class, ['getRevisions']);
107
        $pageRepo
108
            ->method('getRevisions')
109
            ->with()
110
            ->willReturn([
111
                [
112
                    'id' => '1',
113
                    'timestamp' => '20170505100000',
114
                    'length_change' => '1',
115
                    'comment' => 'One'
116
                ],
117
            ]);
118
119
        $page = new Page(new Project('exampleWiki'), 'Page');
120
        $page->setRepository($pageRepo);
121
        $user = new User('Testuser');
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
        //$this->assertCount(3, $page->getRevisions($user)->getCount());
1 ignored issue
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
    }
124
}
125