Completed
Push — master ( 684e0f...bce5b6 )
by
unknown
02:51
created

ProjectTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 145
Duplicated Lines 15.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 4
dl 23
loc 145
rs 10
c 3
b 0
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testBasicMetadata() 0 16 1
A testNamespaces() 11 11 1
A testSingleWiki() 12 12 1
A testExists() 0 11 1
A testNoRepository() 0 5 1
A testOptedIn() 0 17 1
A testHasPageAssessments() 0 22 1
A optedInProvider() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    {
46
        $project2 = new Project('test.example.wiki');
47
        $project2->getTitle();
48
    }
49
50
    /**
51
     * A project has a set of namespaces, comprising integer IDs and string titles.
52
     */
53 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...
54
    {
55
        $projectRepo = $this->getMock(ProjectRepository::class);
56
        $projectRepo->expects($this->once())
57
            ->method('getMetadata')
58
            ->willReturn(['namespaces' => [0 => 'Article', 1 => 'Article_talk']]);
59
60
        $project = new Project('testWiki');
61
        $project->setRepository($projectRepo);
62
        $this->assertCount(2, $project->getNamespaces());
63
    }
64
65
    /**
66
     * XTools can be run in single-wiki mode, where there is only one project.
67
     */
68 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...
69
    {
70
        $projectRepo = new ProjectRepository();
71
        $projectRepo->setSingleMetadata([
72
            'url' => 'https://example.org/a-wiki/',
73
            'dbname' => 'example_wiki',
74
        ]);
75
        $project = new Project('disregarded_wiki_name');
76
        $project->setRepository($projectRepo);
77
        $this->assertEquals('example_wiki', $project->getDatabaseName());
78
        $this->assertEquals('https://example.org/a-wiki/', $project->getUrl());
79
    }
80
81
    /**
82
     * A project is considered to exist if it has at least a domain name.
83
     */
84
    public function testExists()
85
    {
86
        $projectRepo = $this->getMock(ProjectRepository::class);
87
        $projectRepo->expects($this->once())
88
            ->method('getOne')
89
            ->willReturn([]);
90
91
        $project = new Project('testWiki');
92
        $project->setRepository($projectRepo);
93
        $this->assertFalse($project->exists());
94
    }
95
96
    /**
97
     * A user or a whole project can opt in to displaying restricted statistics.
98
     * @dataProvider optedInProvider
99
     * @param string[] $optedInProjects List of projects.
100
     * @param string $dbname The database name.
101
     * @param bool $hasOptedIn The result to check against.
102
     */
103
    public function testOptedIn($optedInProjects, $dbname, $hasOptedIn)
104
    {
105
        $projectRepo = $this->getMock(ProjectRepository::class);
106
        $projectRepo->expects($this->once())
107
            ->method('optedIn')
108
            ->willReturn($optedInProjects);
109
        $projectRepo->expects($this->once())
110
            ->method('getOne')
111
            ->willReturn(['dbname' => $dbname]);
112
113
        $project = new Project($dbname);
114
        $project->setRepository($projectRepo);
115
116
        // Check that the user has opted in or not.
117
        $user = new User('TestUser');
118
        $this->assertEquals($hasOptedIn, $project->userHasOptedIn($user));
119
    }
120
121
    /**
122
     * Whether page assessments are supported for the project
123
     */
124
    public function testHasPageAssessments()
125
    {
126
        $projectRepo = $this->getMock(ProjectRepository::class);
127
128
        $project = new Project('testWiki');
129
        $project->setRepository($projectRepo);
130
131
        // Unconfigured project
132
        $this->assertEquals(false, $project->hasPageAssessments());
133
134
        // Mock and re-test
135
        $projectRepo
136
            ->method('getAssessmentsConfig')
137
            ->with()
138
            ->willReturn([
139
                [
140
                    'wikiproject_prefix' => 'Wikipedia:WikiProject_',
141
                ],
142
            ]);
143
144
        $this->assertEquals(true, $project->hasPageAssessments());
145
    }
146
147
    /**
148
     * Data for self::testOptedIn().
149
     * @return array
150
     */
151
    public function optedInProvider()
152
    {
153
        $optedInProjects = ['project1'];
154
        return [
155
            [$optedInProjects, 'project1', true],
156
            [$optedInProjects, 'project2', false],
157
            [$optedInProjects, 'project3', false],
158
        ];
159
    }
160
}
161