Bart\GitHook\GitHookController::createFromScriptName PHP Method

createFromScriptName() public static method

public static createFromScriptName ( string $invokedScript ) : GitHookController
$invokedScript string PHP SCRIPT_NAME e.g. hooks/post-recieve.d/bart-hook-runner
return GitHookController
    public static function createFromScriptName($invokedScript)
    {
        // Use directory name (e.g. hooks); the realpath of the invoked script is likely a symlink
        $dirOfScript = dirname($invokedScript);
        /** @var \Bart\Shell $shell */
        $shell = Diesel::create('\\Bart\\Shell');
        // e.g. /var/lib/gitosis/puppet.git/hooks/post-receive.d
        $fullPathToDir = $shell->realpath($dirOfScript);
        // Can always safely assume that project name immediately precedes hooks dir
        // ...for both local and upstreams repos
        $hooksPos = strpos($fullPathToDir, '.git/hooks');
        if ($hooksPos === false) {
            throw new GitHookException("Could not infer project from path {$fullPathToDir}");
        }
        $pathToRepo = substr($fullPathToDir, 0, $hooksPos);
        $projectName = basename($pathToRepo);
        // Conventionally assume that name of hooks directory matches hook name itself
        // e.g. hooks/pre-receive.d/
        $hookName = substr($fullPathToDir, $hooksPos + strlen('.git/hooks/'));
        $hookName = substr($hookName, 0, strpos($hookName, '.'));
        return new self("{$pathToRepo}.git", $projectName, $hookName);
    }

Usage Example

 /**
  * @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();
 }