Completed
Pull Request — master (#12)
by Helpful
02:23
created

ExternalLinksTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class ExternalLinksTest extends SapphireTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	protected static $fixture_file = 'ExternalLinksTest.yml';
6
7
	protected $extraDataObjects = array(
8
		'ExternalLinksTest_Page'
9
	);
10
11
	public function setUpOnce() {
12
		if (class_exists('Phockito')) {
13
			Phockito::include_hamcrest(false);
14
		}
15
16
		parent::setUpOnce();
17
	}
18
19
	public function setUp() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
		parent::setUp();
21
22
		Injector::nest();
23
24
		// Check dependencies
25
		if (!class_exists('Phockito')) {
26
			$this->skipTest = true;
27
			return $this->markTestSkipped("These tests need the Phockito module installed to run");
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
		}
29
30
		// Mock link checker
31
		$checker = Phockito::mock('LinkChecker');
32
		Phockito::when($checker)
33
			->checkLink('http://www.working.com')
34
			->return(200);
35
36
		Phockito::when($checker)
37
			->checkLink('http://www.broken.com/url/thing') // 404 on working site
38
			->return(404);
39
40
		Phockito::when($checker)
41
			->checkLink('http://www.broken.com') // 403 on working site
42
			->return(403);
43
44
		Phockito::when($checker)
45
			->checkLink('http://www.nodomain.com') // no ping
46
			->return(0);
47
48
		Phockito::when($checker)
49
			->checkLink('/internal/link')
50
			->return(null);
51
52
		Phockito::when($checker)
53
			->checkLink('[sitetree_link,id=9999]')
54
			->return(null);
55
56
		Phockito::when($checker)
57
			->checkLink('home')
58
			->return(null);
59
60
		Phockito::when($checker)
61
			->checkLink('broken-internal')
62
			->return(null);
63
64
		Phockito::when($checker)
65
			->checkLink('[sitetree_link,id=1]')
66
			->return(null);
67
68
		Phockito::when($checker)
69
			->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
70
			->return(404);
71
72
		Injector::inst()->registerService($checker, 'LinkChecker');
73
	}
74
75
	public function tearDown() {
76
		Injector::unnest();
77
		parent::tearDown();
78
	}
79
80
	public function testLinks() {
81
		// Run link checker
82
		$task = CheckExternalLinksTask::create();
83
		$task->setSilent(true); // Be quiet during the test!
84
		$task->runLinksCheck();
85
86
		// Get all links checked
87
		$status = BrokenExternalPageTrackStatus::get_latest();
88
		$this->assertEquals('Completed', $status->Status);
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<BrokenExternalPageTrackStatus>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
		$this->assertEquals(5, $status->TotalPages);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
		$this->assertEquals(5, $status->CompletedPages);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
92
		// Check all pages have had the correct HTML adjusted
93
		for($i = 1; $i <= 5; $i++) {
94
			$page = $this->objFromFixture('ExternalLinksTest_Page', 'page'.$i);
95
			$this->assertNotEmpty($page->Content);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
			$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
				$page->ExpectedContent,
98
				$page->Content,
99
				"Assert that the content of page{$i} has been updated"
100
			);
101
		}
102
103
		// Check that the correct report of broken links is generated
104
		$links = $status
105
			->BrokenLinks()
106
			->sort('Link');
107
108
		$this->assertEquals(4, $links->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
		$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
			array(
111
				'http://www.broken.com',
112
				'http://www.broken.com/url/thing',
113
				'http://www.broken.com/url/thing',
114
				'http://www.nodomain.com'
115
			),
116
			array_values($links->map('ID', 'Link')->toArray())
117
		);
118
119
		// Check response codes are correct
120
		$expected = array(
121
			'http://www.broken.com' => 403,
122
			'http://www.broken.com/url/thing' => 404,
123
			'http://www.nodomain.com' => 0
124
		);
125
		$actual = $links->map('Link', 'HTTPCode')->toArray();
126
		$this->assertEquals($expected, $actual);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
		// Check response descriptions are correct
129
		i18n::set_locale('en_NZ');
130
		$expected = array(
131
			'http://www.broken.com' => '403 (Forbidden)',
132
			'http://www.broken.com/url/thing' => '404 (Not Found)',
133
			'http://www.nodomain.com' => '0 (Server Not Available)'
134
		);
135
		$actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
136
		$this->assertEquals($expected, $actual);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ExternalLinksTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
	}
138
139
	/**
140
	 * Test that broken links appears in the reports list
141
	 */
142
	public function testReportExists() {
143
		$reports = SS_Report::get_reports();
144
		$reportNames = array();
145
		foreach($reports as $report) {
146
			$reportNames[] = $report->class;
147
		}
148
		$this->assertContains('BrokenExternalLinksReport',$reportNames,
149
			'BrokenExternalLinksReport is in reports list');
150
	}
151
}
152
153
class ExternalLinksTest_Page extends Page implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
154
	private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
155
		'ExpectedContent' => 'HTMLText'
156
	);
157
}
158