Example #1
0
 /**
  * Register a mock shell instance with Diesel
  */
 protected function registerMockShell($mockShell = null)
 {
     if (!$mockShell) {
         $mockShell = $this->getMock('Bart\\Shell');
     }
     Diesel::registerInstantiator('Bart\\Shell', $mockShell, true);
 }
Example #2
0
 /**
  * Register a git stub with Diesel
  * @return \Bart\Git git stub that was registered
  */
 public function getGitStub()
 {
     // mock git and method get change id to return $repo
     $gitStub = $this->getMock('\\Bart\\Git', array(), array(), '', false);
     Diesel::registerInstantiator('Bart\\Git', function () use($gitStub) {
         return $gitStub;
     });
     return $gitStub;
 }
Example #3
0
 /**
  * Register our mock soap instance with local Diesel
  */
 private function register_soap_with_diesel()
 {
     $phpu = $this;
     Diesel::registerInstantiator('\\SoapClient', function ($wsdl, $options) use($phpu) {
         $phpu->assertEquals($phpu->opts['wsdl'], $wsdl, 'wsdl');
         $phpu->assertEquals($phpu->opts['key'], $options['key'], 'opts');
         return $phpu->soap;
     });
 }
Example #4
0
 /**
  * Configure Job for injection of stub Curl with $json
  * @param string $url Expected Jenkins URL
  * @param JSON $json
  */
 private function configure_diesel($url, $json)
 {
     $mock_curl = $this->getMock('\\Bart\\Curl', array(), array(), '', false);
     $mock_curl->expects($this->once())->method('get')->with($this->equalTo(''), $this->equalTo(array()))->will($this->returnValue(array('content' => $json)));
     $phpu = $this;
     Diesel::registerInstantiator('Bart\\Curl', function ($urlParam, $portParam) use($phpu, $url, $mock_curl) {
         $phpu->assertEquals($url, $urlParam, 'url');
         $phpu->assertEquals(8080, $portParam, 'port');
         return $mock_curl;
     });
 }
Example #5
0
 private function configure_for($conf, $is_healthy, $job_name)
 {
     $mock_job = $this->getMock('\\Bart\\Jenkins\\Job', array(), array(), '', false);
     $mock_job->expects($this->once())->method('is_healthy')->will($this->returnValue($is_healthy));
     $gitStub = $this->getGitStub();
     $phpu = $this;
     Diesel::registerInstantiator('Bart\\Jenkins\\Job', function ($host, $jobNameParam) use($phpu, $conf, $job_name, $mock_job) {
         $phpu->assertEquals($job_name, $jobNameParam, 'Jenkins job name');
         $phpu->assertEquals($conf['jenkins']['host'], $host, 'Jenkins host');
         return $mock_job;
     });
     return array('stl' => new StopTheLineJenkins($conf, '', 'Gorg'), 'git' => $gitStub);
 }
Example #6
0
 /**
  * @integrationTest
  */
 public function testRawFileContentsReal()
 {
     // Let's make sure the command we're running is legit
     $expectedContents = trim(shell_exec('git show HEAD:composer.json'));
     // Replicate the actual shell invocation that would take place
     Diesel::registerInstantiator('Bart\\Shell\\Command', function () {
         $shell = new Shell();
         $args = func_get_args();
         return call_user_func_array([$shell, 'command'], $args);
     });
     $commit = new Commit(new GitRoot(BART_DIR . '/.git'), 'HEAD');
     $actualContents = trim($commit->rawFileContents('composer.json'));
     $this->assertEquals($expectedContents, $actualContents, 'Raw file contents');
 }
 public function testConstructor()
 {
     $conf = array();
     // mock git and method get_change_id to return $repo
     $mock_git = $this->getMock('\\Bart\\Git', array(), array(), '', false);
     $mock_git->expects($this->once())->method('get_change_id')->will($this->returnValue('grinder'));
     $phpu = $this;
     Diesel::registerInstantiator('Bart\\Git', function ($gitDir) use($mock_git, $phpu) {
         $phpu->assertEquals('.git', $gitDir, 'Expected constructor to get git dir');
         return $mock_git;
     });
     $hook = new TestGitHookAction($conf, '.git', 'grinder');
     $hook->run($this);
 }
Example #8
0
 /**
  * Basic, shared setup for the Build_In_Jenkins hook
  *
  * @param array $conf Configurations for the hook
  * @param string $commit_msg The commit message for the hook
  * @param string $job_name The name of the job to be built
  * @return array The git hook and the git stub
  */
 private function configure_for(array $conf, $commit_msg, $job_name)
 {
     $hash = 'HEAD';
     $info = array('author' => self::$author, 'subject' => '', 'message' => $commit_msg);
     $mock_git = $this->getGitStub();
     $mock_git->expects($this->once())->method('get_pretty_email')->with($this->equalTo($hash))->will($this->returnValue($info));
     $phpu = $this;
     $mock_job = $this->getMock('\\Bart\\Jenkins\\Job', array(), array(), '', false);
     Diesel::registerInstantiator('Bart\\Jenkins\\Job', function ($host, $name) use($phpu, $conf, $job_name, $mock_job) {
         $phpu->assertEquals($job_name, $name, 'Jenkins job name did not match');
         $phpu->assertEquals($conf['jenkins']['host'], $host, 'Expected host to match conf');
         return $mock_job;
     });
     return array('j' => new BuildInJenkins($conf, '', self::$repo), 'git' => $mock_git, 'job' => $mock_job);
 }
Example #9
0
 public function testUnderscoresSupported()
 {
     Diesel::registerInstantiator('\\Bart\\Shell', function () {
         // Actually want to see how this works with a real Shell
         return new Shell();
     });
     $this->doStuffWithTempDir(function (BaseTestCase $phpu, $dirName) {
         Configuration::configure($dirName);
         // Copy sample INI file to path Configuration will look for TestConfig INI
         copy(BART_DIR . '/test/etc/conf-parser.conf', $dirName . '/test_underscore.conf');
         $configs = new Test_Underscore_Config(false);
         $phpu->assertEquals(42, $configs->number(), 'Underscore Configs number');
         $phpu->assertEquals('Quail', $configs->wildGame(), 'Underscore Configs wildGame');
     });
 }
Example #10
0
 public function testBasicCommandExec()
 {
     $root = new GitRoot();
     $command = CommandTest::withStubbedResult($this, ['a57a266'], 0);
     Diesel::registerInstantiator('Bart\\Shell\\Command', function ($fmt, $dir, $limit, $author, $pretty) use($command) {
         // Assert that GitRoot sends expected parameters to create the Command
         $this->assertEquals('git --git-dir=%s log %s --author=%s --format:%s', $fmt, 'command arg 0');
         $this->assertEquals('.git', $dir);
         $this->assertEquals('-1', $limit, 'Limit of commits');
         $this->assertEquals('jbraynard', $author);
         $this->assertEquals('%h', $pretty, 'pretty format');
         return $command;
     });
     // Throw something together with a few args interspersed
     $result = $root->getCommandResult('log %s --author=%s --format:%s', '-1', 'jbraynard', '%h');
     $this->assertEquals('a57a266', $result->getOutput(true), 'git log');
 }
Example #11
0
 /**
  * Stub the expected configuration
  * @param bool $buildHealth
  */
 private function mockJenkinsJobWithDependencies($buildHealth = true)
 {
     $this->shmockAndDieselify('\\Bart\\Jenkins\\JenkinsConfig', function ($jConfigs) {
         $jConfigs->domain()->once()->return_value('jenkins.example.com');
         $jConfigs->port()->once()->return_value('8080');
         $jConfigs->protocol()->once()->return_value('http');
         $jConfigs->user()->once()->return_value('user');
         $jConfigs->token()->once()->return_value('token');
         $jConfigs->jobLocation()->once()->return_value('job/Base/job/Build');
     }, true);
     $mockConnection = $this->shmockAndDieselify('\\Bart\\Jenkins\\Connection', function ($connection) {
         $connection->setAuth()->once();
     }, true);
     $mockJob = $this->shmock('\\Bart\\Jenkins\\Job', function ($jobStub) use($buildHealth) {
         $jobStub->isHealthy()->once()->return_value($buildHealth);
     }, true);
     Diesel::registerInstantiator('\\Bart\\Jenkins\\Job', function ($connection) use($mockJob, $mockConnection) {
         $this->assertEquals($mockConnection, $connection, '\\Bart\\Jenkins\\Connection object');
         return $mockJob;
     });
 }
Example #12
0
 /**
  * Mocks out the Diesel Curl class
  * @param callable $configure
  * @param string $expectedProtocol
  * @param string $expectedPort
  * @throws \Bart\DieselException
  */
 private function mockCurl($configure, $expectedProtocol = 'http', $expectedPort = '8080')
 {
     $fullUrl = "{$expectedProtocol}://" . self::$fakeDomain . ":{$expectedPort}/" . self::$fakeApiPath;
     $mockCurl = $this->shmock('\\Bart\\Curl', function ($curlStub) use($configure) {
         $configure($curlStub);
     }, true);
     Diesel::registerInstantiator('\\Bart\\Curl', function ($url, $port) use($mockCurl, $fullUrl, $expectedPort) {
         $this->assertEquals($fullUrl, $url, 'Full Jenkins REST API URL');
         $this->assertEquals($expectedPort, $port, 'Port');
         return $mockCurl;
     });
 }
Example #13
0
 /**
  * @param String $remoteGerritCmd
  * @param \PHPUnit_Framework_MockObject_Stub $will
  * @return Api
  */
 private function configureApiForCmd($remoteGerritCmd, $will)
 {
     $ssh = $this->getMock('\\Bart\\SshWrapper', array(), array(), '', false);
     $ssh->expects($this->once())->method('exec')->with($this->equalTo($remoteGerritCmd))->will($will);
     $gerritConfigs = $this->gerritConfigs;
     Diesel::registerInstantiator('Bart\\Configuration\\GerritConfig', function () use($gerritConfigs) {
         return $gerritConfigs;
     });
     $phpu = $this;
     Diesel::registerInstantiator('Bart\\SshWrapper', function ($server, $port) use($ssh, $phpu) {
         $phpu->assertEquals('gerrit.example.com', $server, 'gerrit server');
         $phpu->assertEquals(29418, $port, 'gerrit ssh port');
         return $ssh;
     });
     return new Api();
 }
Example #14
0
 /**
  * @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();
 }