php-censor/tests/PHPCensor/Plugin/Util/ExecutorTest.php

166 lines
5.4 KiB
PHP
Raw Normal View History

2013-12-06 13:52:47 +01:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2013-12-06 13:52:47 +01:00
2016-07-19 20:28:11 +02:00
namespace Tests\PHPCensor\Plugin\Util;
2013-12-06 13:52:47 +01:00
2016-07-19 20:28:11 +02:00
use PHPCensor\Plugin\Util\Executor;
use Prophecy\Argument;
2013-12-06 13:52:47 +01:00
class ExecutorTest extends \PHPUnit_Framework_TestCase
2013-12-06 13:52:47 +01:00
{
/**
* @var Executor
*/
protected $testedExecutor;
protected $mockBuildLogger;
protected $mockFactory;
protected $mockStore;
2013-12-06 13:52:47 +01:00
protected function setUp()
{
parent::setUp();
2016-07-19 20:28:11 +02:00
$this->mockBuildLogger = $this->prophesize('\PHPCensor\Logging\BuildLogger');
$this->mockFactory = $this->prophesize('\PHPCensor\Plugin\Util\Factory');
$this->mockStore = $this->prophesize('\PHPCensor\Store\BuildStore');
$this->testedExecutor = new Executor(
$this->mockFactory->reveal(),
$this->mockBuildLogger->reveal(),
$this->mockStore->reveal()
);
2013-12-06 13:52:47 +01:00
}
2016-07-21 19:02:11 +02:00
public function testExecutePlugin_AssumesNamespaceIfNoneGiven()
2013-12-06 13:52:47 +01:00
{
2016-04-21 06:58:09 +02:00
$options = [];
2013-12-06 13:52:47 +01:00
$pluginName = 'PhpUnit';
2016-07-19 20:28:11 +02:00
$pluginNamespace = 'PHPCensor\\Plugin\\';
2013-12-06 13:52:47 +01:00
$this->mockFactory->buildPlugin($pluginNamespace . $pluginName, $options)
->shouldBeCalledTimes(1)
2016-07-19 20:28:11 +02:00
->willReturn($this->prophesize('PHPCensor\Plugin')->reveal());
2013-12-06 13:52:47 +01:00
$this->testedExecutor->executePlugin($pluginName, $options);
}
public function testExecutePlugin_KeepsCalledNameSpace()
{
2016-04-21 06:58:09 +02:00
$options = [];
$pluginClass = $this->getFakePluginClassName('ExamplePluginFull');
2013-12-06 13:52:47 +01:00
$this->mockFactory->buildPlugin($pluginClass, $options)
2013-12-06 13:52:47 +01:00
->shouldBeCalledTimes(1)
2016-07-19 20:28:11 +02:00
->willReturn($this->prophesize('PHPCensor\Plugin')->reveal());
2013-12-06 13:52:47 +01:00
$this->testedExecutor->executePlugin($pluginClass, $options);
2013-12-06 13:52:47 +01:00
}
public function testExecutePlugin_CallsExecuteOnFactoryBuildPlugin()
{
2016-04-21 06:58:09 +02:00
$options = [];
2013-12-06 13:52:47 +01:00
$pluginName = 'PhpUnit';
2016-07-19 20:28:11 +02:00
$build = new \PHPCensor\Model\Build();
2013-12-06 13:52:47 +01:00
2016-07-19 20:28:11 +02:00
$mockPlugin = $this->prophesize('PHPCensor\Plugin');
2013-12-06 13:52:47 +01:00
$mockPlugin->execute()->shouldBeCalledTimes(1);
$this->mockFactory->buildPlugin(Argument::any(), Argument::any())->willReturn($mockPlugin->reveal());
2016-07-19 20:28:11 +02:00
$this->mockFactory->getResourceFor('PHPCensor\Model\Build')->willReturn($build);
2013-12-06 13:52:47 +01:00
$this->testedExecutor->executePlugin($pluginName, $options);
}
public function testExecutePlugin_ReturnsPluginSuccess()
{
2016-04-21 06:58:09 +02:00
$options = [];
2013-12-06 13:52:47 +01:00
$pluginName = 'PhpUnit';
$expectedReturnValue = true;
2016-07-19 20:28:11 +02:00
$mockPlugin = $this->prophesize('PHPCensor\Plugin');
2013-12-06 13:52:47 +01:00
$mockPlugin->execute()->willReturn($expectedReturnValue);
$this->mockFactory->buildPlugin(Argument::any(), Argument::any())->willReturn($mockPlugin->reveal());
2013-12-06 13:52:47 +01:00
$returnValue = $this->testedExecutor->executePlugin($pluginName, $options);
$this->assertEquals($expectedReturnValue, $returnValue);
}
2013-12-06 14:04:27 +01:00
public function testExecutePlugin_LogsFailureForNonExistentClasses()
{
2016-04-21 06:58:09 +02:00
$options = [];
2013-12-06 14:04:27 +01:00
$pluginName = 'DOESNTEXIST';
$this->mockBuildLogger->logFailure('Plugin does not exist: ' . $pluginName)->shouldBeCalledTimes(1);
$this->testedExecutor->executePlugin($pluginName, $options);
}
public function testExecutePlugin_LogsFailureWhenExceptionsAreThrownByPlugin()
{
2016-04-21 06:58:09 +02:00
$options = [];
2013-12-06 14:04:27 +01:00
$pluginName = 'PhpUnit';
$expectedException = new \RuntimeException("Generic Error");
2016-07-19 20:28:11 +02:00
$mockPlugin = $this->prophesize('PHPCensor\Plugin');
2013-12-06 14:04:27 +01:00
$mockPlugin->execute()->willThrow($expectedException);
$this->mockFactory->buildPlugin(Argument::any(), Argument::any())->willReturn($mockPlugin->reveal());
2013-12-06 14:04:27 +01:00
2014-12-08 16:04:44 +01:00
$this->mockBuildLogger->logFailure('Exception: ' . $expectedException->getMessage(), $expectedException)
2013-12-06 14:04:27 +01:00
->shouldBeCalledTimes(1);
$this->testedExecutor->executePlugin($pluginName, $options);
}
2013-12-06 14:14:19 +01:00
public function testExecutePlugins_CallsEachPluginForStage()
{
2016-04-21 06:58:09 +02:00
$phpUnitPluginOptions = [];
$behatPluginOptions = [];
2016-07-19 20:28:11 +02:00
$build = new \PHPCensor\Model\Build();
2013-12-06 14:14:19 +01:00
2016-04-21 09:46:38 +02:00
$config = [
'stageOne' => [
2013-12-06 14:14:19 +01:00
'PhpUnit' => $phpUnitPluginOptions,
2016-04-21 09:46:38 +02:00
'Behat' => $behatPluginOptions,
]
];
2013-12-06 14:14:19 +01:00
2016-07-19 20:28:11 +02:00
$pluginNamespace = 'PHPCensor\\Plugin\\';
2013-12-06 14:14:19 +01:00
2016-07-19 20:28:11 +02:00
$mockPhpUnitPlugin = $this->prophesize('PHPCensor\Plugin');
2013-12-06 14:14:19 +01:00
$mockPhpUnitPlugin->execute()->shouldBeCalledTimes(1)->willReturn(true);
$this->mockFactory->buildPlugin($pluginNamespace . 'PhpUnit', $phpUnitPluginOptions)
->willReturn($mockPhpUnitPlugin->reveal());
2016-07-19 20:28:11 +02:00
$this->mockFactory->getResourceFor('PHPCensor\Model\Build')->willReturn($build);
2013-12-06 14:14:19 +01:00
2016-07-19 20:28:11 +02:00
$mockBehatPlugin = $this->prophesize('PHPCensor\Plugin');
2013-12-06 14:14:19 +01:00
$mockBehatPlugin->execute()->shouldBeCalledTimes(1)->willReturn(true);
$this->mockFactory->buildPlugin($pluginNamespace . 'Behat', $behatPluginOptions)
->willReturn($mockBehatPlugin->reveal());
$this->testedExecutor->executePlugins($config, 'stageOne');
}
protected function getFakePluginClassName($pluginName)
{
2016-07-19 20:28:11 +02:00
$pluginNamespace = '\\Tests\\PHPCensor\\Plugin\\Util\\Fake\\';
return $pluginNamespace . $pluginName;
}
2013-12-06 13:52:47 +01:00
}