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

ProjectTest::testBasicMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Tests\Xtools;
4
5
use PHPUnit_Framework_TestCase;
6
7
class ProjectTest extends PHPUnit_Framework_TestCase
8
{
9
10
    /**
11
     * A project has its own domain name, database name, URL, script path, and article path.
12
     */
13
    public function testBasicMetadata()
14
    {
15
        $projectRepo = $this->getMock(ProjectRepository::class);
16
        $projectRepo->expects($this->once())
17
            ->method('getOne')
18
            ->willReturn(['url' => 'https://test.example.org', 'dbname' => 'test_wiki']);
19
20
        $project = new Project('testWiki');
21
        $project->setRepository($projectRepo);
22
        $this->assertEquals('test.example.org', $project->getDomain());
23
        $this->assertEquals('test_wiki', $project->getDatabaseName());
24
        $this->assertEquals('https://test.example.org/', $project->getUrl());
25
        $this->assertEquals('/w', $project->getScriptPath());
26
        $this->assertEquals('/wiki/', $project->getArticlePath());
27
        $this->assertTrue($project->exists());
28
    }
29
30
    /**
31
     * A project has a set of namespaces, comprising integer IDs and string titles.
32
     */
33 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...
34
    {
35
        $projectRepo = $this->getMock(ProjectRepository::class);
36
        $projectRepo->expects($this->once())
37
            ->method('getMetadata')
38
            ->willReturn(['namespaces' => [0 => 'Article', 1 => 'Article_talk']]);
39
40
        $project = new Project('testWiki');
41
        $project->setRepository($projectRepo);
42
        $this->assertCount(2, $project->getNamespaces());
43
    }
44
45
    /**
46
     * Xtools can be run in single-wiki mode, where there is only one project.
47
     */
48 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...
49
    {
50
        $projectRepo = new ProjectRepository();
51
        $projectRepo->setSingleMetadata([
52
            'url' => 'https://example.org/a-wiki/',
53
            'dbname' => 'example_wiki',
54
        ]);
55
        $project = new Project('disregarded_wiki_name');
56
        $project->setRepository($projectRepo);
57
        $this->assertEquals('example_wiki', $project->getDatabaseName());
58
        $this->assertEquals('https://example.org/a-wiki/', $project->getUrl());
59
    }
60
61
    /**
62
     * A project is considered to exist if it has at least a domain name.
63
     */
64
    public function testExists()
65
    {
66
        $projectRepo = $this->getMock(ProjectRepository::class);
67
        $projectRepo->expects($this->once())
68
            ->method('getOne')
69
            ->willReturn([]);
70
71
        $project = new Project('testWiki');
72
        $project->setRepository($projectRepo);
73
        $this->assertFalse($project->exists());
74
    }
75
76
    /**
77
     * @dataProvider optedInProvider
78
     */
79
    public function testOptedIn($optedInProjects, $dbname, $hasOptedIn)
80
    {
81
        $projectRepo = $this->getMock(ProjectRepository::class);
82
        $projectRepo->expects($this->once())
83
            ->method('optedIn')
84
            ->willReturn($optedInProjects);
85
        $projectRepo->expects($this->once())
86
            ->method('getOne')
87
            ->willReturn(['dbname' => $dbname]);
88
            
89
        $project = new Project($dbname);
90
        $project->setRepository($projectRepo);
91
        
92
        // Check that the user has opted in or not.
93
        $user = new User('TestUser');
94
        $this->assertEquals($hasOptedIn, $project->userHasOptedIn($user));
95
    }
96
97
    public function optedInProvider()
98
    {
99
        $optedInProjects = ['project1'];
100
        return [
101
            [$optedInProjects, 'project1', true],
102
            [$optedInProjects, 'project2', false],
103
            [$optedInProjects, 'project3', false],
104
        ];
105
    }
106
}
107