Source of file PipelineTest.php
Size: 6,146 Bytes - Last Modified: 2021-12-23T10:29:15+00:00
/var/www/docs.ssmods.com/process/src/tests/PipelineTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 | <?php abstract class PipelineTest extends DeploynautTest { public function setUp() { parent::setUp(); Injector::inst()->load(array( 'DeploynautLogFile' => 'PipelineTest_MockLog', 'ConfirmationMessagingService' => 'PipelineTest_RecordingMessageSender', 'DNDeployment' => 'PipelineTest_DNDeployment', 'DNDataTransfer' => 'PipelineTest_DNDataTransfer' )); $this->clearLog(); } /** * Gets the config data from the test pipeline config file * * @return array * @throws Exception */ protected function getPipelineConfig() { require_once 'thirdparty/spyc/spyc.php'; $path = __DIR__ . '/PipelineTest_Config.yml'; if(!file_exists($path)) { throw new Exception(sprintf('YAML configuration for pipeline not found at path "%s"', $path)); } return Spyc::YAMLLoad($path); } /** * Reset logs of scaffolded services */ protected function clearLog() { PipelineTest_MockLog::clear(); PipelineTest_RecordingMessageSender::clear(); } protected function assertHasLog($message) { $this->assertTrue(PipelineTest_MockLog::has_message($message), "Assert log \"$message\""); } protected function assertNotLogged($message) { $this->assertFalse(PipelineTest_MockLog::has_message($message), "Assert not logged \"$message\""); } public function assertSentMessage($message, $recipient) { $this->assertTrue( PipelineTest_RecordingMessageSender::has_message($message, $recipient), "Assert message \"$message\" sent to \"$recipient\"" ); } public function assertNotSentMessage($message, $recipient) { $this->assertFalse( PipelineTest_RecordingMessageSender::has_message($message, $recipient), "Assert message \"$message\" not sent to \"$recipient\"" ); } } /** * Dummy logging service */ class PipelineTest_MockLog extends DeploynautLogFile implements TestOnly { protected static $messages = array(); public function __construct($logFile) {} public static function clear() { self::$messages = array(); } public static function get_messages() { return self::$messages; } public function write($message) { self::log($message); } public static function log($message) { self::$messages[] = $message; } public function exists() { return true; } public function content() { return implode(PHP_EOL, self::$messages); } /** * Check if any message contains the given string * * @param string $test * @return boolean True if this message is contained in any of the given messages */ public static function has_message($test) { foreach(self::$messages as $message) { if(strpos($message, $test) !== false) return true; } return false; } } class PipelineTest_RecordingMessageSender extends EmailMessagingService implements TestOnly { protected static $messages = array(); public static function clear() { self::$messages = array(); } public static function get_messages() { return self::$messages; } public static function has_message($test, $recipient = null) { foreach(self::$messages as $message) { if($message[1] !== $test) continue; if($recipient && $message[0] === $recipient) return true; } return false; } protected function sendViaEmail($source, $from, $to, $subject, $body) { self::$messages[] = array($to, $body); $source->log("Sent message to $to (subject: $subject)"); return true; } } /** * Dummy deployment service */ class PipelineTest_DNDeployment extends DNDeployment implements TestOnly { public function __construct($record = null, $isSingleton = false, $model = null) { // Set the fields data. if(!$record) { $record = array( 'ID' => 0, 'ClassName' => 'DNDeployment', 'RecordClassName' => 'DNDeployment' ); } parent::__construct($record, $isSingleton, $model); $this->class = 'DNDeployment'; } protected function enqueueDeployment() { // Mock behaviour of enqueue without actually enqueuing anything $environment = $this->Environment(); $project = $environment->Project(); $log = $this->log(); $log->write(sprintf('Deploying "%s" to "%s"', $this->SHA, $environment->getFullName())); if(!$this->DeployerID) { $this->DeployerID = Member::currentUserID(); } if($this->DeployerID) { $deployer = $this->Deployer(); $message = sprintf( 'Deploy to %s:%s initiated by %s (%s)', $project->Name, $environment->Name, $deployer->getName(), $deployer->Email ); $log->write($message); } return 'dummytoken'; } public function getSHA() { $sha = parent::getField('SHA'); return $sha ?: '9ae502821345ab39b04d46ce6bb822ccdd7f7414'; } public function start() { $this->Status = 'Queued'; $this->write(); } public function markFinished() { $this->Status = 'Finished'; $this->write(); } public function markFailed() { $this->Status = 'Failed'; $this->write(); } public function ResqueStatus() { // Just lie, remembering to map 'Finished' to 'Complete' return $this->Status === 'Finished' ? 'Complete' : $this->Status; } } class PipelineTest_DNDataTransfer extends DNDataTransfer implements TestOnly { public function __construct($record = null, $isSingleton = false, $model = null) { // Set the fields data. if(!$record) { $record = array( 'ID' => 0, 'ClassName' => 'DNDataTransfer', 'RecordClassName' => 'DNDataTransfer' ); } parent::__construct($record, $isSingleton, $model); $this->class = 'DNDataTransfer'; } public function start() { $this->Status = 'Queued'; } public function markFinished() { $this->Status = 'Finished'; $this->write(); } public function markFailed() { $this->Status = 'Failed'; $this->write(); } public function ResqueStatus() { // Just lie, remembering to map 'Finished' to 'Complete' return $this->Status === 'Finished' ? 'Complete' : $this->Status; } } class PipelineTest_Project extends DNProject implements TestOnly { public function repoExists() { return false; } } class PipelineTest_Environment extends DNEnvironment implements TestOnly { /** * Use the demo backend * * @var string */ function Backend() { return new DemoDeploymentBackend; } } |