/** * @param Commit $commit * @return bool If the commit should be skipped by the hook */ private function shouldSkip(Commit $commit, GitHookConfig $gitHookConfig) { $message = $commit->messageBody(); $isEmergency = preg_match('/^EMERGENCY/', $message) === 1; if ($isEmergency) { $to = $gitHookConfig->getEmergencyNotificationEmailAddress(); $subject = $gitHookConfig->getEmergencyNotificationSubject(); $body = $gitHookConfig->getEmergencyNotificationBody(); if (!empty($to) && !empty($subject) && !empty($body)) { GlobalFunctions::mail($to, $subject, $body); } else { $this->logger->error("Invalid mail params. Notification email not sent."); } } return $isEmergency; }
/** * @param string[] $stdInArray Array of standard input values * @param string[] $revList Array of revisions * @param string[] $validRefs Array of all valid refs * @param int $numValidRefs Number of valid refs in the standard input array that are actually in $validRefs */ private function runProcessRevisionTest(array $stdInArray, array $revList, array $validRefs, $numValidRefs, $emergency = false) { $numInputs = count($stdInArray); $numRevs = count($revList); $message = $emergency ? "EMERGENCY commit" : "This message will be ignored"; $this->shmockAndDieselify('\\Bart\\Shell', function ($shell) use($stdInArray) { $shell->realpath(self::POST_RECEIVE_PATH)->once()->return_value(self::POST_RECEIVE_REAL_PATH); $shell->std_in()->once()->return_value($stdInArray); }); $this->shmockAndDieselify('\\Bart\\Git\\GitRoot', function ($gitRoot) { $gitRoot->getCommandResult()->never(); }, true); $this->shmockAndDieselify('\\Bart\\Git', function ($git) use($revList, $numValidRefs) { if ($numValidRefs === 0) { $git->getRevList()->never(); } else { $git->getRevList(self::START_HASH, self::END_HASH)->times($numValidRefs)->return_value($revList); } }, true); // The number of runs for $gitHookConfig->getValidRefs() depend on the total number of // inputs in the standard input array and the number of revisions $stubConfig = $this->shmock('\\Bart\\GitHook\\GitHookConfig', function ($gitHookConfig) use($numInputs, $validRefs, $emergency) { $gitHookConfig->getValidRefs()->times($numInputs)->return_value($validRefs); if ($emergency) { if (!empty($validRefs)) { $gitHookConfig->getEmergencyNotificationBody()->once()->return_value('Notification body'); $gitHookConfig->getEmergencyNotificationEmailAddress()->once()->return_value('*****@*****.**'); $gitHookConfig->getEmergencyNotificationSubject()->once()->return_value('Emergency subject'); } } }, true); // Register stub mail() function to validate config is loaded and passed in as expected GlobalFunctions::register('mail', function ($to, $subject, $body) { $this->assertEquals('*****@*****.**', $to, 'Emergency email address'); $this->assertEquals('Emergency subject', $subject, 'Emergency subject'); $this->assertEquals('Notification body', $body, 'Emergency body'); }); // The number of runs for $gitCommit->message() and $postReceiveRunner->runAllActions depend on $numValidRefs $numValidCommits = $numValidRefs * $numRevs; $stubCommit = $this->shmockAndDieselify('\\Bart\\Git\\Commit', function ($gitCommit) use($numValidCommits, $message) { $gitCommit->messageSubject()->times($numValidCommits)->return_value($message); }, true); if ($emergency) { // We'll skip the hooks for any emergency commit $numValidCommits = 0; } $stubRunner = $this->shmock('\\Bart\\GitHook\\PostReceiveRunner', function ($postReceiveRunner) use($numValidCommits) { $postReceiveRunner->runAllActions()->times($numValidCommits); }, true); // Explicitly register GitHookConfig and PostReceiveRunner stubs so that we can // ...assert the constructor args are what we expect Diesel::registerInstantiator('\\Bart\\GitHook\\GitHookConfig', function ($commit) use($stubCommit, $stubConfig) { $this->assertSame($commit, $stubCommit); return $stubConfig; }); Diesel::registerInstantiator('\\Bart\\GitHook\\PostReceiveRunner', function ($commit) use($stubCommit, $stubRunner) { $this->assertSame($commit, $stubCommit); return $stubRunner; }); // Create the controller and verify the mocks $controller = GitHookController::createFromScriptName(self::POST_RECEIVE_SCRIPT); $controller->run(); }