Completed
Pull Request — master (#513)
by Helpful
04:05
created

get_messages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
abstract class PipelineTest extends DeploynautTest {
1 ignored issue
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
	public function setUp() {
6
		parent::setUp();
7
		Injector::inst()->load(array(
8
			'DeploynautLogFile' => 'PipelineTest_MockLog',
9
			'ConfirmationMessagingService' => 'PipelineTest_RecordingMessageSender',
10
			'DNDeployment' => 'PipelineTest_DNDeployment',
11
			'DNDataTransfer' => 'PipelineTest_DNDataTransfer'
12
		));
13
		$this->clearLog();
14
	}
15
16
	/**
17
	 * Gets the config data from the test pipeline config file
18
	 *
19
	 * @return array
20
	 * @throws Exception
21
	 */
22
	protected function getPipelineConfig() {
23
		require_once 'thirdparty/spyc/spyc.php';
24
		$path = __DIR__ . '/PipelineTest_Config.yml';
25
		if(!file_exists($path)) {
26
			throw new Exception(sprintf('YAML configuration for pipeline not found at path "%s"', $path));
27
		}
28
		return Spyc::YAMLLoad($path);
29
	}
30
31
	/**
32
	 * Reset logs of scaffolded services
33
	 */
34
	protected function clearLog() {
35
		PipelineTest_MockLog::clear();
36
		PipelineTest_RecordingMessageSender::clear();
37
	}
38
39
	protected function assertHasLog($message) {
40
		$this->assertTrue(PipelineTest_MockLog::has_message($message), "Assert log \"$message\"");
1 ignored issue
show
Bug introduced by
The method assertTrue() does not seem to exist on object<PipelineTest>.

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...
41
	}
42
43
	protected function assertNotLogged($message) {
44
		$this->assertFalse(PipelineTest_MockLog::has_message($message), "Assert not logged \"$message\"");
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<PipelineTest>.

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...
45
	}
46
47
	public function assertSentMessage($message, $recipient) {
48
		$this->assertTrue(
1 ignored issue
show
Bug introduced by
The method assertTrue() does not seem to exist on object<PipelineTest>.

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...
49
			PipelineTest_RecordingMessageSender::has_message($message, $recipient),
50
			"Assert message \"$message\" sent to \"$recipient\""
51
		);
52
	}
53
54
	public function assertNotSentMessage($message, $recipient) {
55
		$this->assertFalse(
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<PipelineTest>.

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...
56
			PipelineTest_RecordingMessageSender::has_message($message, $recipient),
57
			"Assert message \"$message\" not sent to \"$recipient\""
58
		);
59
	}
60
}
61
62
63
/**
64
 * Dummy logging service
65
 */
66
class PipelineTest_MockLog extends DeploynautLogFile implements TestOnly {
1 ignored issue
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...
67
68
	protected static $messages = array();
69
70
	public function __construct($logFile) {}
71
72
	public static function clear() {
73
		self::$messages = array();
74
	}
75
76
	public static function get_messages() {
77
		return self::$messages;
78
	}
79
80
	public function write($message) {
81
		self::log($message);
82
	}
83
84
	public static function log($message) {
85
		self::$messages[] = $message;
86
	}
87
88
89
	public function exists() {
90
		return true;
91
	}
92
93
	public function content() {
94
		return implode(PHP_EOL, self::$messages);
95
	}
96
97
	/**
98
	 * Check if any message contains the given string
99
	 *
100
	 * @param string $test
101
	 * @return boolean True if this message is contained in any of the given messages
102
	 */
103
	public static function has_message($test) {
104
		foreach(self::$messages as $message) {
105
			if(strpos($message, $test) !== false) return true;
106
		}
107
		return false;
108
	}
109
}
110
111
class PipelineTest_RecordingMessageSender extends EmailMessagingService implements TestOnly {
1 ignored issue
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...
112
113
	protected static $messages = array();
114
115
	public static function clear() {
116
		self::$messages = array();
117
	}
118
119
	public static function get_messages() {
120
		return self::$messages;
121
	}
122
123
	public static function has_message($test, $recipient = null) {
124
		foreach(self::$messages as $message) {
125
			if($message[1] !== $test) continue;
126
			if($recipient && $message[0] === $recipient) return true;
127
		}
128
		return false;
129
	}
130
131
	protected function sendViaEmail($source, $from, $to, $subject, $body) {
132
		self::$messages[] = array($to, $body);
133
		$source->log("Sent message to $to (subject: $subject)");
134
		return true;
135
	}
136
137
}
138
139
140
/**
141
 * Dummy deployment service
142
 */
143
class PipelineTest_DNDeployment extends DNDeployment implements TestOnly {
1 ignored issue
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...
144
145 View Code Duplication
	public function __construct($record = null, $isSingleton = false, $model = null) {
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...
146
		// Set the fields data.
147
		if(!$record) {
148
			$record = array(
149
				'ID' => 0,
150
				'ClassName' => 'DNDeployment',
151
				'RecordClassName' => 'DNDeployment'
152
			);
153
		}
154
155
		parent::__construct($record, $isSingleton, $model);
156
157
		$this->class = 'DNDeployment';
158
	}
159
160
	protected function enqueueDeployment() {
161
		// Mock behaviour of enqueue without actually enqueuing anything
162
		$environment = $this->Environment();
163
		$project = $environment->Project();
164
165
		$log = $this->log();
166
		$log->write(sprintf('Deploying "%s" to "%s"', $this->SHA, $environment->getFullName()));
167
168
		if(!$this->DeployerID) {
169
			$this->DeployerID = Member::currentUserID();
170
		}
171
172 View Code Duplication
		if($this->DeployerID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
173
			$deployer = $this->Deployer();
174
			$message = sprintf(
175
				'Deploy to %s:%s initiated by %s (%s)',
176
				$project->Name,
177
				$environment->Name,
178
				$deployer->getName(),
179
				$deployer->Email
180
			);
181
			$log->write($message);
182
		}
183
184
		return 'dummytoken';
185
	}
186
187
	public function getSHA() {
188
		$sha = parent::getField('SHA');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getField() instead of getSHA()). Are you sure this is correct? If so, you might want to change this to $this->getField().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
189
		return $sha ?: '9ae502821345ab39b04d46ce6bb822ccdd7f7414';
190
	}
191
192
	public function start() {
193
		$this->Status = 'Queued';
194
		$this->write();
195
	}
196
197
	public function markFinished() {
198
		$this->Status = 'Finished';
199
		$this->write();
200
	}
201
202
	public function markFailed() {
203
		$this->Status = 'Failed';
204
		$this->write();
205
	}
206
207
	public function ResqueStatus() {
208
		// Just lie, remembering to map 'Finished' to 'Complete'
209
		return $this->Status === 'Finished' ? 'Complete' : $this->Status;
210
	}
211
}
212
213
class PipelineTest_DNDataTransfer extends DNDataTransfer implements TestOnly {
1 ignored issue
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...
214
215 View Code Duplication
	public function __construct($record = null, $isSingleton = false, $model = null) {
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...
216
		// Set the fields data.
217
		if(!$record) {
218
			$record = array(
219
				'ID' => 0,
220
				'ClassName' => 'DNDataTransfer',
221
				'RecordClassName' => 'DNDataTransfer'
222
			);
223
		}
224
225
		parent::__construct($record, $isSingleton, $model);
226
227
		$this->class = 'DNDataTransfer';
228
	}
229
230
	public function start() {
231
		$this->Status = 'Queued';
232
	}
233
234
	public function markFinished() {
235
		$this->Status = 'Finished';
236
		$this->write();
237
	}
238
239
	public function markFailed() {
240
		$this->Status = 'Failed';
241
		$this->write();
242
	}
243
244
	public function ResqueStatus() {
245
		// Just lie, remembering to map 'Finished' to 'Complete'
246
		return $this->Status === 'Finished' ? 'Complete' : $this->Status;
247
	}
248
}
249
250
class PipelineTest_Project extends DNProject implements TestOnly {
1 ignored issue
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...
251
252
	public function repoExists() {
253
		return false;
254
	}
255
256
}
257
258
class PipelineTest_Environment extends DNEnvironment implements TestOnly {
1 ignored issue
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...
259
260
	/**
261
	 * Use the demo backend
262
	 *
263
	 * @var string
264
	 */
265
	public function Backend() {
266
		return new DemoDeploymentBackend;
267
	}
268
269
}
270