Passed
Push — 1 ( 67274c )
by Robbie
02:29
created

CwpControllerExtensionTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 132
Duplicated Lines 32.58 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 43
loc 132
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testRequiresLoginForNonTest() 16 16 1
A testRequiresLoginForTest() 22 22 2
B testRedirectsSSLToDomain() 0 26 1
A testRedirectsSSLToCurrentDomain() 0 23 1
B testRequiresLoginForLiveWhenEnabled() 0 32 2

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
class CwpControllerExtensionTest extends SapphireTest {
4
5
	function testRedirectsSSLToDomain() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
6
		Session::set("loggedInAs", null);
7
8
		$ctrl = new Controller();
9
		$req = new SS_HTTPRequest('GET', '/');
10
		$dataModel = new DataModel();
11
12
		Config::inst()->nest();
13
14
		$directorClass = $this->getMockClass('Director', array('forceSSL', 'is_https'));
15
		Injector::inst()->registerNamedService('Director', new $directorClass);
0 ignored issues
show
Deprecated Code introduced by
The function Injector::registerNamedService() has been deprecated: since 4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

15
		/** @scrutinizer ignore-deprecated */ Injector::inst()->registerNamedService('Director', new $directorClass);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
16
17
		// Expecting this to call forceSSL to forcedomain.org.
18
		$directorClass::staticExpects($this->any())
19
			->method('is_https')
20
			->will($this->returnValue(false));
21
		$directorClass::staticExpects($this->once())
22
			->method('forceSSL')
23
			->with($this->anything(), $this->equalTo('forcedomain.org'));
24
25
		Config::inst()->update('CwpControllerExtension', 'ssl_redirection_enabled', true);
26
		Config::inst()->update('CwpControllerExtension', 'ssl_redirection_force_domain', 'forcedomain.org');
27
		$response = $ctrl->handleRequest($req, $dataModel);
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
28
29
		Injector::inst()->unregisterAllObjects();
30
		Config::inst()->unnest();
31
	}
32
33
	function testRedirectsSSLToCurrentDomain() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
		Session::set("loggedInAs", null);
35
36
		$ctrl = new Controller();
37
		$req = new SS_HTTPRequest('GET', '/');
38
		$dataModel = new DataModel();
39
40
		$directorClass = $this->getMockClass('Director', array('forceSSL', 'is_https'));
41
		Injector::inst()->registerNamedService('Director', new $directorClass);
0 ignored issues
show
Deprecated Code introduced by
The function Injector::registerNamedService() has been deprecated: since 4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
		/** @scrutinizer ignore-deprecated */ Injector::inst()->registerNamedService('Director', new $directorClass);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
43
		// Expecting this to call forceSSL to current domain.
44
		$directorClass::staticExpects($this->any())
45
			->method('is_https')
46
			->will($this->returnValue(false));
47
		$directorClass::staticExpects($this->once())
48
			->method('forceSSL')
49
			->with($this->anything());
50
51
		Config::inst()->update('CwpControllerExtension', 'ssl_redirection_enabled', true);
52
		Config::inst()->update('CwpControllerExtension', 'ssl_redirection_force_domain', false);
53
		$response = $ctrl->handleRequest($req, $dataModel);
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
54
55
		Injector::inst()->unregisterAllObjects();
56
	}
57
58 View Code Duplication
	function testRequiresLoginForTest() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
59
		Session::set("loggedInAs", null);
60
61
		$ctrl = new Controller();
62
		$req = new SS_HTTPRequest('GET', '/');
63
		$dataModel = new DataModel();
64
65
		$directorClass = $this->getMockClass('Director', array('isTest'));
66
		Injector::inst()->registerNamedService('Director', new $directorClass);
0 ignored issues
show
Deprecated Code introduced by
The function Injector::registerNamedService() has been deprecated: since 4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

66
		/** @scrutinizer ignore-deprecated */ Injector::inst()->registerNamedService('Director', new $directorClass);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
67
68
		$directorClass::staticExpects($this->any())
69
			->method('isTest')
70
			->will($this->returnValue(true));
71
72
		try {
73
			$response = $ctrl->handleRequest($req, $dataModel);
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
74
		} catch (Exception $e) {
75
			$this->assertEquals($e->getResponse()->getStatusCode(), '401', 'Forces BasicAuth on test');
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on Exception. It seems like you code against a sub-type of Exception such as SS_HTTPResponse_Exception or Zend_Exception. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
			$this->assertEquals($e->/** @scrutinizer ignore-call */ getResponse()->getStatusCode(), '401', 'Forces BasicAuth on test');
Loading history...
76
77
			// We need to pop manually, since throwing an SS_HTTPResponse_Exception in onBeforeInit hijacks
78
			// the normal Controller control flow and confuses TestRunner (as they share global controller stack).
79
			$ctrl->popCurrent();
80
		}
81
82
	}
83
84 View Code Duplication
	function testRequiresLoginForNonTest() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
85
		Session::set("loggedInAs", null);
86
87
		$ctrl = new Controller();
88
		$req = new SS_HTTPRequest('GET', '/');
89
		$dataModel = new DataModel();
90
91
		$directorClass = $this->getMockClass('Director', array('isTest'));
92
		Injector::inst()->registerNamedService('Director', new $directorClass);
0 ignored issues
show
Deprecated Code introduced by
The function Injector::registerNamedService() has been deprecated: since 4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
		/** @scrutinizer ignore-deprecated */ Injector::inst()->registerNamedService('Director', new $directorClass);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
93
94
		$directorClass::staticExpects($this->any())
95
			->method('isTest')
96
			->will($this->returnValue(false));
97
98
		$response = $ctrl->handleRequest($req, $dataModel);
99
		$this->assertEquals($response->getStatusCode(), '200', 'Does not force BasicAuth on live');
100
101
	}
102
103
	function testRequiresLoginForLiveWhenEnabled() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
104
		Session::set("loggedInAs", null);
105
106
		$ctrl = new Controller();
107
		$req = new SS_HTTPRequest('GET', '/');
108
		$dataModel = new DataModel();
109
110
		Config::inst()->nest();
111
112
		Config::inst()->update('CwpControllerExtension', 'live_basicauth_enabled', true);
113
		$directorClass = $this->getMockClass('Director', array('isTest', 'isLive'));
114
		Injector::inst()->registerNamedService('Director', new $directorClass);
0 ignored issues
show
Deprecated Code introduced by
The function Injector::registerNamedService() has been deprecated: since 4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
		/** @scrutinizer ignore-deprecated */ Injector::inst()->registerNamedService('Director', new $directorClass);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
115
116
		$directorClass::staticExpects($this->any())
117
			->method('isTest')
118
			->will($this->returnValue(false));
119
120
		$directorClass::staticExpects($this->any())
121
			->method('isLive')
122
			->will($this->returnValue(true));
123
124
		try {
125
			$response = $ctrl->handleRequest($req, $dataModel);
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
126
		} catch (Exception $e) {
127
			$this->assertEquals($e->getResponse()->getStatusCode(), '401', 'Forces BasicAuth on live (optionally)');
128
129
			// We need to pop manually, since throwing an SS_HTTPResponse_Exception in onBeforeInit hijacks
130
			// the normal Controller control flow and confuses TestRunner (as they share global controller stack).
131
			$ctrl->popCurrent();
132
		}
133
134
		Config::inst()->unnest();
135
	}
136
137
}
138
139