public function testCheckUnitTests()
 {
     $this->if($builder = new \mock\mageekguy\atoum\scripts\builder(uniqid(), $adapter = new atoum\test\adapter()))->and($builder->disableUnitTestChecking())->then->boolean($builder->unitTestCheckingIsEnabled())->isFalse()->boolean($builder->checkUnitTests())->isTrue()->if($builder->enableUnitTestChecking())->then->exception(function () use($builder) {
         $builder->checkUnitTests();
     })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Unable to check unit tests, working directory is undefined')->if->mockGenerator->shunt('__construct')->and($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs())->and($this->calling($vcs)->exportRepository = function () {
     })->and($builder->setVcs($vcs))->and($php = new \mock\mageekguy\atoum\php())->and($this->calling($php)->run = $php)->and($builder->setPhp($php))->and($builder->setWorkingDirectory($workingDirectory = uniqid()))->and($builder->setUnitTestRunnerScript($unitTestRunnerScript = uniqid()))->and($builder->setReportTitle($reportTitle = uniqid()))->and($builder->addRunnerConfigurationFile($runnerConfigurationFile = uniqid()))->and($score = new \mock\mageekguy\atoum\score())->and($this->calling($score)->getFailNumber = 0)->and($this->calling($score)->getExceptionNumber = 0)->and($this->calling($score)->getErrorNumber = 0)->and($adapter->sys_get_temp_dir = $tempDirectory = uniqid())->and($adapter->tempnam = $scoreFile = uniqid())->and($adapter->file_get_contents = $scoreFileContents = uniqid())->and($adapter->unserialize = $score)->and($adapter->unlink = true)->and($this->calling($builder)->writeErrorInErrorsDirectory = function () {
     })->then->boolean($builder->checkUnitTests())->isTrue()->mock($vcs)->call('setWorkingDirectory')->withArguments($workingDirectory)->once()->call('exportRepository')->once()->adapter($adapter)->call('sys_get_temp_dir')->once()->call('tempnam')->withArguments($tempDirectory, '')->once()->call('file_get_contents')->withArguments($scoreFile)->once()->call('unserialize')->withArguments($scoreFileContents)->once()->call('unlink')->withArguments($scoreFile)->once()->mock($score)->call('getFailNumber')->once()->call('getExceptionNumber')->once()->call('getErrorNumber')->once()->mock($php)->call('reset')->before($this->mock($php)->call('run'))->once()->call('addOption')->withArguments('-f', $workingDirectory . \DIRECTORY_SEPARATOR . $unitTestRunnerScript)->before($this->mock($php)->call('run'))->once()->call('addArgument')->withArguments('-ncc')->before($this->mock($php)->call('run'))->once()->call('addArgument')->withArguments('-d', $workingDirectory . \DIRECTORY_SEPARATOR . 'tests' . \DIRECTORY_SEPARATOR . 'units' . \DIRECTORY_SEPARATOR . 'classes')->before($this->mock($php)->call('run'))->once()->call('addArgument')->withArguments('-p', $php->getBinaryPath())->before($this->mock($php)->call('run'))->once()->call('addArgument')->withArguments('-sf', $scoreFile)->before($this->mock($php)->call('run'))->once()->call('addArgument')->withArguments('-c', $runnerConfigurationFile)->before($this->mock($php)->call('run'))->once()->if($this->calling($php)->getExitCode = 127)->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to find \'' . $php->getBinaryPath() . '\' or it is not executable')->once()->if($this->calling($php)->getExitCode = $exitCode = rand(1, 125))->and($this->calling($php)->getStdErr = $stdErr = uniqid())->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments($php . ' failed with exit code \'' . $exitCode . '\': ' . $stdErr)->once()->if($this->calling($php)->getExitCode = $exitCode = rand(128, PHP_INT_MAX))->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments($php . ' failed with exit code \'' . $exitCode . '\': ' . $stdErr)->once()->if($this->calling($php)->getExitCode = 0)->and($this->calling($php)->getStdErr = '')->then->boolean($builder->checkUnitTests())->isTrue()->if($this->calling($php)->getStdErr = $stdErrContents = uniqid())->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments($stdErrContents)->once()->if($this->calling($php)->getStdErr = '')->and($adapter->file_get_contents = false)->and($this->resetMock($builder))->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to read score from file \'' . $scoreFile . '\'')->once()->if($adapter->file_get_contents = $scoreFileContents)->and($adapter->unserialize = false)->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to unserialize score from file \'' . $scoreFile . '\'')->once()->if($adapter->unserialize = uniqid())->then->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Contents of file \'' . $scoreFile . '\' is not a score')->once()->if($adapter->unserialize = $score)->and($adapter->unlink = false)->then->exception(function () use($builder) {
         $builder->checkUnitTests();
     })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to delete score file \'' . $scoreFile . '\'')->if($adapter->unlink = true)->and($this->calling($score)->getFailNumber = rand(1, PHP_INT_MAX))->then->boolean($builder->checkUnitTests())->isFalse()->if($this->calling($score)->getFailNumber = 0)->and($this->calling($score)->getExceptionNumber = rand(1, PHP_INT_MAX))->then->boolean($builder->checkUnitTests())->isFalse()->if($this->calling($score)->getExceptionNumber = 0)->and($this->calling($score)->getErrorNumber = rand(1, PHP_INT_MAX))->then->boolean($builder->checkUnitTests())->isFalse();
 }
Esempio n. 2
0
 public function testCheckUnitTests()
 {
     $builder = new \mock\mageekguy\atoum\scripts\builder(uniqid(), $adapter = new atoum\test\adapter());
     $builder->disableUnitTestChecking();
     $this->assert->boolean($builder->unitTestCheckingIsEnabled())->isFalse()->boolean($builder->checkUnitTests())->isTrue();
     $builder->enableUnitTestChecking();
     $this->assert->exception(function () use($builder) {
         $builder->checkUnitTests();
     })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Unable to check unit tests, working directory is undefined');
     $builder->setWorkingDirectory($workingDirectory = uniqid());
     $vcsController = new mock\controller();
     $vcsController->__construct = function () {
     };
     $vcsController->exportRepository = function () {
     };
     $builder->setVcs($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs(null, $vcsController));
     $builder->setUnitTestRunnerScript($unitTestRunnerScript = uniqid())->setPhpPath($php = uniqid())->setReportTitle($reportTitle = uniqid())->addRunnerConfigurationFile($runnerConfigurationFile = uniqid());
     $score = new \mock\mageekguy\atoum\score();
     $scoreController = $score->getMockController();
     $scoreController->getFailNumber = 0;
     $scoreController->getExceptionNumber = 0;
     $scoreController->getErrorNumber = 0;
     $adapter->sys_get_temp_dir = $tempDirectory = uniqid();
     $adapter->tempnam = $scoreFile = uniqid();
     $adapter->proc_open = function ($bin, $descriptors, &$stream) use(&$stdOut, &$stdErr, &$pipes, &$resource) {
         $pipes = array(1 => $stdOut = uniqid(), 2 => $stdErr = uniqid());
         $stream = $pipes;
         return $resource = uniqid();
     };
     $adapter->proc_get_status = array('exit_code' => 0, 'running' => true);
     $adapter->stream_get_contents = function () {
         return '';
     };
     $adapter->fclose = function () {
     };
     $adapter->proc_close = function () {
     };
     $adapter->file_get_contents = $scoreFileContents = uniqid();
     $adapter->unserialize = $score;
     $adapter->unlink = true;
     $command = escapeshellarg($php) . ' ' . escapeshellarg($workingDirectory . \DIRECTORY_SEPARATOR . $unitTestRunnerScript) . ' -drt ' . escapeshellarg($reportTitle) . ' -ncc -sf ' . escapeshellarg($scoreFile) . ' -d ' . escapeshellarg($workingDirectory . \DIRECTORY_SEPARATOR . 'tests' . \DIRECTORY_SEPARATOR . 'units' . \DIRECTORY_SEPARATOR . 'classes') . ' -p ' . escapeshellarg($php) . ' -c ' . escapeshellarg($runnerConfigurationFile);
     $builderController = $builder->getMockController();
     $builderController->writeErrorInErrorsDirectory = function () {
     };
     $this->assert->boolean($builder->checkUnitTests())->isTrue()->mock($vcs)->call('setWorkingDirectory')->withArguments($workingDirectory)->once()->mock($vcs)->call('exportRepository')->once()->adapter($adapter)->call('sys_get_temp_dir')->once()->call('tempnam')->withArguments($tempDirectory, '')->once()->call('proc_open')->withArguments($command, array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes)->once()->call('proc_get_status')->withArguments($resource)->once()->call('stream_get_contents')->withArguments($stdOut)->once()->call('fclose')->withArguments($stdOut)->once()->call('stream_get_contents')->withArguments($stdErr)->once()->call('fclose')->withArguments($stdErr)->once()->call('proc_close')->withArguments($resource)->once()->call('file_get_contents')->withArguments($scoreFile)->once()->call('unserialize')->withArguments($scoreFileContents)->once()->call('unlink')->withArguments($scoreFile)->once()->mock($score)->call('getFailNumber')->once()->call('getExceptionNumber')->once()->call('getErrorNumber')->once();
     $adapter->proc_open = false;
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to execute \'' . $command . '\'')->once();
     $adapter->proc_open = function ($bin, $descriptors, &$stream) use(&$stdOut, &$stdErr, &$pipes, &$resource) {
         $pipes = array(1 => $stdOut = uniqid(), 2 => $stdErr = uniqid());
         $stream = $pipes;
         return $resource = uniqid();
     };
     $adapter->proc_get_status = array('exitcode' => 126, 'running' => false);
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to find \'' . $php . '\' or it is not executable')->once();
     $adapter->proc_get_status = array('exitcode' => 127, 'running' => false);
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to find \'' . $php . '\' or it is not executable')->once();
     $adapter->proc_get_status = array('exitcode' => $exitCode = rand(1, 125), 'running' => false);
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Command \'' . $command . '\' failed with exit code \'' . $exitCode . '\'')->once();
     $adapter->proc_get_status = array('exitcode' => $exitCode = rand(128, PHP_INT_MAX), 'running' => false);
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Command \'' . $command . '\' failed with exit code \'' . $exitCode . '\'')->once();
     $adapter->proc_get_status = array('exit_code' => 0, 'running' => true);
     $adapter->stream_get_contents = function ($stream) use(&$stdOut, &$stdOutContents) {
         return $stream != $stdOut ? '' : ($stdOutContents = uniqid());
     };
     $this->assert->boolean($builder->checkUnitTests())->isTrue()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments($stdOutContents)->never();
     $adapter->stream_get_contents = function ($stream) use(&$stdErr, &$stdErrContents) {
         return $stream != $stdErr ? '' : ($stdErrContents = uniqid());
     };
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments($stdErrContents)->once();
     $adapter->stream_get_contents = '';
     $adapter->file_get_contents = false;
     $builder->getMockController()->resetCalls();
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to read score from file \'' . $scoreFile . '\'')->once();
     $adapter->file_get_contents = $scoreFileContents;
     $adapter->unserialize = false;
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Unable to unserialize score from file \'' . $scoreFile . '\'')->once();
     $adapter->unserialize = uniqid();
     $this->assert->boolean($builder->checkUnitTests())->isFalse()->mock($builder)->call('writeErrorInErrorsDirectory')->withArguments('Contents of file \'' . $scoreFile . '\' is not a score')->once();
     $adapter->unserialize = $score;
     $adapter->unlink = false;
     $this->assert->exception(function () use($builder) {
         $builder->checkUnitTests();
     })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to delete score file \'' . $scoreFile . '\'');
     $adapter->unlink = true;
     $scoreController->getFailNumber = rand(1, PHP_INT_MAX);
     $this->assert->boolean($builder->checkUnitTests())->isFalse();
     $scoreController->getFailNumber = 0;
     $scoreController->getExceptionNumber = rand(1, PHP_INT_MAX);
     $this->assert->boolean($builder->checkUnitTests())->isFalse();
     $scoreController->getExceptionNumber = 0;
     $scoreController->getErrorNumber = rand(1, PHP_INT_MAX);
     $this->assert->boolean($builder->checkUnitTests())->isFalse();
 }