Completed
Push — master ( ce8ec5...fc399f )
by Sam
03:02
created

ProjectTest::testNoRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * This file contains only the ProjectTest class.
4
 */
5
6
namespace Tests\Xtools;
7
8
use PHPUnit_Framework_TestCase;
9
use Xtools\Project;
10
use Xtools\ProjectRepository;
11
use Xtools\User;
12
13
/**
14
 * Tests for the Project class.
15
 */
16
class ProjectTest extends PHPUnit_Framework_TestCase
17
{
18
19
    /**
20
     * A project has its own domain name, database name, URL, script path, and article path.
21
     */
22
    public function testBasicMetadata()
23
    {
24
        $projectRepo = $this->getMock(ProjectRepository::class);
25
        $projectRepo->expects($this->once())
26
            ->method('getOne')
27
            ->willReturn(['url' => 'https://test.example.org', 'dbname' => 'test_wiki']);
28
29
        $project = new Project('testWiki');
30
        $project->setRepository($projectRepo);
31
        $this->assertEquals('test.example.org', $project->getDomain());
32
        $this->assertEquals('test_wiki', $project->getDatabaseName());
33
        $this->assertEquals('https://test.example.org/', $project->getUrl());
34
        $this->assertEquals('/w', $project->getScriptPath());
35
        $this->assertEquals('/wiki/$1', $project->getArticlePath());
36
        $this->assertTrue($project->exists());
37
    }
38
39
    /**
40
     * Make sure there's an error when trying to get project metadata without a Repository.
41
     * @expectedException \Exception
42
     * @expectedExceptionMessage Repository for Xtools\Project must be set before using.
43
     */
44
    public function testNoRepository() {
45
        $project2 = new Project('test.example.wiki');
46
        $project2->getTitle();
47
    }
48
49
    /**
50
     * A project has a set of namespaces, comprising integer IDs and string titles.
51
     */
52 View Code Duplication
    public function testNamespaces()
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...
53
    {
54
        $projectRepo = $this->getMock(ProjectRepository::class);
55
        $projectRepo->expects($this->once())
56
            ->method('getMetadata')
57
            ->willReturn(['namespaces' => [0 => 'Article', 1 => 'Article_talk']]);
58
59
        $project = new Project('testWiki');
60
        $project->setRepository($projectRepo);
61
        $this->assertCount(2, $project->getNamespaces());
62
    }
63
64
    /**
65
     * XTools can be run in single-wiki mode, where there is only one project.
66
     */
67 View Code Duplication
    public function testSingleWiki()
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...
68
    {
69
        $projectRepo = new ProjectRepository();
70
        $projectRepo->setSingleMetadata([
71
            'url' => 'https://example.org/a-wiki/',
72
            'dbname' => 'example_wiki',
73
        ]);
74
        $project = new Project('disregarded_wiki_name');
75
        $project->setRepository($projectRepo);
76
        $this->assertEquals('example_wiki', $project->getDatabaseName());
77
        $this->assertEquals('https://example.org/a-wiki/', $project->getUrl());
78
    }
79
80
    /**
81
     * A project is considered to exist if it has at least a domain name.
82
     */
83
    public function testExists()
84
    {
85
        $projectRepo = $this->getMock(ProjectRepository::class);
86
        $projectRepo->expects($this->once())
87
            ->method('getOne')
88
            ->willReturn([]);
89
90
        $project = new Project('testWiki');
91
        $project->setRepository($projectRepo);
92
        $this->assertFalse($project->exists());
93
    }
94
95
    /**
96
     * A user or a whole project can opt in to displaying restricted statistics.
97
     * @dataProvider optedInProvider
98
     * @param string[] $optedInProjects List of projects.
99
     * @param string $dbname The database name.
100
     * @param bool $hasOptedIn The result to check against.
101
     */
102
    public function testOptedIn($optedInProjects, $dbname, $hasOptedIn)
103
    {
104
        $projectRepo = $this->getMock(ProjectRepository::class);
105
        $projectRepo->expects($this->once())
106
            ->method('optedIn')
107
            ->willReturn($optedInProjects);
108
        $projectRepo->expects($this->once())
109
            ->method('getOne')
110
            ->willReturn(['dbname' => $dbname]);
111
            
112
        $project = new Project($dbname);
113
        $project->setRepository($projectRepo);
114
        
115
        // Check that the user has opted in or not.
116
        $user = new User('TestUser');
117
        $this->assertEquals($hasOptedIn, $project->userHasOptedIn($user));
118
    }
119
120
    /**
121
     * Data for self::testOptedIn().
122
     * @return array
123
     */
124
    public function optedInProvider()
125
    {
126
        $optedInProjects = ['project1'];
127
        return [
128
            [$optedInProjects, 'project1', true],
129
            [$optedInProjects, 'project2', false],
130
            [$optedInProjects, 'project3', false],
131
        ];
132
    }
133
}
134