Skip to content
Extraits de code Groupes Projets
Valider 2c087e9c rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Fixing unit tests, includes environment creation/destruction to UnitTestCase...

Fixing unit tests, includes environment creation/destruction to UnitTestCase base class + ability to override environment in UnitTestCase.
parent 3661b2c7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 100 ajouts et 57 suppressions
......@@ -22,8 +22,16 @@ use Piwik\Piwik;
*/
class Environment
{
/**
* @var string
*/
private $environment;
/**
* @var array
*/
private $definitions;
/**
* @var Container
*/
......@@ -39,9 +47,10 @@ class Environment
*/
private $pluginList;
public function __construct($environment)
public function __construct($environment, array $definitions = array())
{
$this->environment = $environment;
$this->definitions = $definitions;
}
public function init()
......@@ -70,8 +79,9 @@ class Environment
{
$pluginList = $this->getPluginListCached();
$settings = $this->getGlobalSettingsCached();
$definitions = array_merge(StaticContainer::getDefinitons(), $this->definitions);
$containerFactory = new ContainerFactory($pluginList, $settings, $this->environment, StaticContainer::getDefinitons());
$containerFactory = new ContainerFactory($pluginList, $settings, $this->environment, $definitions);
return $containerFactory->create();
}
......
......@@ -10,8 +10,6 @@ use Piwik\SettingsServer;
class IniFileChainFactory
{
static $instance = null;
/**
* TODO should not be a static eventually
*
......@@ -21,11 +19,7 @@ class IniFileChainFactory
*/
public static function get($pathGlobal = null, $pathLocal = null, $pathCommon = null)
{
if (self::$instance) {
return self::$instance;
}
self::$instance = new IniFileChain();
$instance = new IniFileChain();
$inTrackerRequest = SettingsServer::isTrackerApiRequest();
......@@ -40,7 +34,7 @@ class IniFileChainFactory
}
try {
self::$instance->reload(array($pathGlobal, $pathCommon), $pathLocal);
$instance->reload(array($pathGlobal, $pathCommon), $pathLocal);
} catch (IniReadingException $e) {
// TODO why a different behavior here? This needs a comment
if ($inTrackerRequest) {
......@@ -48,11 +42,6 @@ class IniFileChainFactory
}
}
return self::$instance;
}
public static function unsetInstance()
{
self::$instance = null;
return $instance;
}
}
......@@ -7,10 +7,12 @@
*/
namespace Piwik\Tests\Framework\TestCase;
use Piwik\Application\Environment;
use Piwik\Container\StaticContainer;
use Piwik\EventDispatcher;
use Piwik\Tests\Framework\Mock\File;
/**
* Base class for Unit tests.
*
......@@ -18,16 +20,43 @@ use Piwik\Tests\Framework\Mock\File;
*/
abstract class UnitTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var Environment
*/
private $environment;
public function setUp()
{
parent::setUp();
$this->environment = new Environment('test', $this->provideContainerConfig());
$this->environment->init();
File::reset();
EventDispatcher::getInstance()->clearAllObservers();
}
public function tearDown()
{
parent::tearDown();
File::reset();
StaticContainer::clearContainer();
// make sure the global container exists for the next test case that is executed (since logging can be done
// before a test sets up an environment)
$nextTestEnviornment = new Environment('test');
$nextTestEnviornment->init();
parent::tearDown();
}
/**
* TODO
*
* @return array
*/
protected function provideContainerConfig()
{
return array();
}
}
......@@ -18,6 +18,7 @@ use Piwik\Plugin;
use Piwik\Plugin\Manager;
use Piwik\EventDispatcher;
use Piwik\Tests\Framework\Mock\TestConfig;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
use Piwik\Tests\Unit\AssetManager\PluginManagerMock;
use Piwik\Tests\Unit\AssetManager\PluginMock;
use Piwik\Tests\Unit\AssetManager\ThemeMock;
......@@ -26,7 +27,7 @@ use Piwik\Tests\Unit\AssetManager\UIAssetCacheBusterMock;
/**
* @group AssetManagerTest
*/
class AssetManagerTest extends PHPUnit_Framework_TestCase
class AssetManagerTest extends UnitTestCase
{
// todo Theme->rewriteAssetPathIfOverridesFound is not tested
......@@ -65,6 +66,8 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();
$this->activateMergedAssets();
$this->setUpCacheBuster();
......@@ -81,7 +84,15 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase
public function tearDown()
{
$this->assetManager->removeMergedAssets();
Manager::unsetInstance();
parent::tearDown();
}
protected function provideContainerConfig()
{
return array(
'Piwik\Plugin\Manager' => \DI\object('Piwik\Tests\Unit\AssetManager\PluginManagerMock')
);
}
private function activateMergedAssets()
......@@ -121,8 +132,7 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase
private function setUpPluginManager()
{
$this->pluginManager = PluginManagerMock::getInstance();
Manager::setSingletonInstance($this->pluginManager);
$this->pluginManager = Manager::getInstance();
EventDispatcher::unsetInstance(); // EventDispatcher stores a reference to Plugin Manager
}
......
......@@ -51,11 +51,12 @@ namespace Piwik\Tests\Unit\Columns
use Piwik\Plugin\Manager;
use Piwik\Plugins\Test\Columns\DimensionTest;
use Piwik\Plugins\Test\FakeActionDimension;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group Core
*/
class ColumnDimensionTest extends \PHPUnit_Framework_TestCase
class ColumnDimensionTest extends UnitTestCase
{
/**
* @var FakeActionDimension
......@@ -64,6 +65,8 @@ namespace Piwik\Tests\Unit\Columns
public function setUp()
{
parent::setUp();
Config::unsetInstance();
Manager::getInstance()->unloadPlugins();
......@@ -75,7 +78,6 @@ namespace Piwik\Tests\Unit\Columns
public function tearDown()
{
Config::unsetInstance();
Manager::unsetInstance();
parent::tearDown();
}
......
......@@ -9,7 +9,8 @@
namespace Piwik\Tests\Unit\Container;
use DI\Definition\ValueDefinition;
use Piwik\Config\IniFileChain;
use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Application\Kernel\GlobalSettingsProvider\IniSettingsProvider;
use Piwik\Container\IniConfigDefinitionSource;
class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
......@@ -29,7 +30,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
*/
public function getDefinition_withUnknownConfigSection_shouldReturnEmptyArray()
{
$definitionSource = new IniConfigDefinitionSource(new IniFileChain());
$definitionSource = new IniConfigDefinitionSource(new IniSettingsProvider());
/** @var ValueDefinition $definition */
$definition = $definitionSource->getDefinition('ini.foo');
......@@ -44,7 +45,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
*/
public function getDefinition_withUnknownConfigSectionAndKey_shouldReturnNull()
{
$definitionSource = new IniConfigDefinitionSource(new IniFileChain());
$definitionSource = new IniConfigDefinitionSource(new IniSettingsProvider());
$this->assertNull($definitionSource->getDefinition('ini.foo.bar'));
}
......@@ -54,7 +55,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
*/
public function getDefinition_withUnknownConfigKey_shouldReturnNull()
{
$definitionSource = new IniConfigDefinitionSource(new IniFileChain());
$definitionSource = new IniConfigDefinitionSource(new IniSettingsProvider());
$this->assertNull($definitionSource->getDefinition('ini.General.foo'));
}
......@@ -66,7 +67,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
{
$config = $this->createConfig();
$config->expects($this->once())
->method('get')
->method('getSection')
->with('General')
->willReturn(array('foo' => 'bar'));
......@@ -88,7 +89,7 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
{
$config = $this->createConfig();
$config->expects($this->once())
->method('get')
->method('getSection')
->with('General')
->willReturn(array('foo' => 'bar'));
......@@ -103,10 +104,10 @@ class IniConfigDefinitionSourceTest extends \PHPUnit_Framework_TestCase
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|IniFileChain
* @return \PHPUnit_Framework_MockObject_MockObject|GlobalSettingsProvider
*/
private function createConfig()
{
return $this->getMock('Piwik\Config\IniFileChain', array(), array(), '', false);
return $this->getMock('Piwik\Application\Kernel\GlobalSettingsProvider', array(), array(), '', false);
}
}
......@@ -30,6 +30,8 @@ class AddSegmentByLabelMappingTest extends UnitTestCase
public function setUp()
{
parent::setUp();
$this->table = new DataTable();
$this->addRow(array('label' => 1));
$this->addRow(array('label' => ''));
......
......@@ -36,6 +36,8 @@ class AddSegmentBySegmentValueTest extends UnitTestCase
public function setUp()
{
parent::setUp();
$this->report = new GetCity();
$this->table = new DataTable();
$this->addRowWithMetadata(array('test' => '1'));
......
......@@ -13,14 +13,14 @@ use Piwik\DataTable;
use Piwik\DataTable\Filter\PivotByDimension;
use Piwik\DataTable\Row;
use Piwik\Plugin\Manager as PluginManager;
use PHPUnit_Framework_TestCase;
use Exception;
use Piwik\Tests\Framework\Mock\TestConfig;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
/**
* @group DataTableTest
*/
class PivotByDimensionTest extends PHPUnit_Framework_TestCase
class PivotByDimensionTest extends UnitTestCase
{
/**
* The number of segment tables that have been created. Used when injecting API results to make sure each
......@@ -40,6 +40,8 @@ class PivotByDimensionTest extends PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();
$self = $this;
$proxyMock = $this->getMock('stdClass', array('call'));
......@@ -63,8 +65,9 @@ class PivotByDimensionTest extends PHPUnit_Framework_TestCase
public function tearDown()
{
PluginManager::unsetInstance();
Proxy::unsetInstance();
parent::tearDown();
}
/**
......
......@@ -12,6 +12,7 @@ namespace Piwik\Plugins\Test;
use Piwik\Plugin\Dimension\ActionDimension;
use Piwik\Plugin\Segment;
use Piwik\Plugin\Manager;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
class FakeActionDimension extends ActionDimension
{
......@@ -45,7 +46,7 @@ class FakeActionDimension extends ActionDimension
/**
* @group Core
*/
class Plugin_ActionDimensionTest extends \PHPUnit_Framework_TestCase
class Plugin_ActionDimensionTest extends UnitTestCase
{
/**
* @var FakeActionDimension
......@@ -54,18 +55,14 @@ class Plugin_ActionDimensionTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();
Manager::getInstance()->unloadPlugins();
Manager::getInstance()->doNotLoadAlwaysActivatedPlugins();
$this->dimension = new FakeActionDimension();
}
public function tearDown()
{
Manager::unsetInstance();
parent::tearDown();
}
public function test_install_shouldNotReturnAnything_IfColumnTypeNotSpecified()
{
$this->dimension->set('columnType', '');
......
......@@ -12,6 +12,7 @@ namespace Piwik\Plugins\Test;
use Piwik\Plugin\Dimension\ConversionDimension;
use Piwik\Plugin\Segment;
use Piwik\Plugin\Manager;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
class FakeConversionDimension extends ConversionDimension
{
......@@ -45,7 +46,7 @@ class FakeConversionDimension extends ConversionDimension
/**
* @group Core
*/
class Plugin_ConversionDimensionTest extends \PHPUnit_Framework_TestCase
class Plugin_ConversionDimensionTest extends UnitTestCase
{
/**
* @var FakeConversionDimension
......@@ -54,18 +55,14 @@ class Plugin_ConversionDimensionTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();
Manager::getInstance()->unloadPlugins();
Manager::getInstance()->doNotLoadAlwaysActivatedPlugins();
$this->dimension = new FakeConversionDimension();
}
public function tearDown()
{
Manager::unsetInstance();
parent::tearDown();
}
public function test_install_shouldNotReturnAnything_IfColumnTypeNotSpecified()
{
$this->dimension->set('columnType', '');
......
......@@ -12,6 +12,7 @@ namespace Piwik\Plugins\Test;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Plugin\Segment;
use Piwik\Plugin\Manager;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
......@@ -61,7 +62,7 @@ class FakeConversionVisitDimension extends FakeVisitDimension
/**
* @group Core
*/
class Plugin_VisitDimensionTest extends \PHPUnit_Framework_TestCase
class Plugin_VisitDimensionTest extends UnitTestCase
{
/**
* @var FakeVisitDimension
......@@ -75,6 +76,8 @@ class Plugin_VisitDimensionTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();
Manager::getInstance()->unloadPlugins();
Manager::getInstance()->doNotLoadAlwaysActivatedPlugins();
......@@ -82,12 +85,6 @@ class Plugin_VisitDimensionTest extends \PHPUnit_Framework_TestCase
$this->conversionDimension = new FakeConversionVisitDimension();
}
public function tearDown()
{
Manager::unsetInstance();
parent::tearDown();
}
public function test_install_shouldNotReturnAnything_IfColumnTypeNotSpecified()
{
$this->dimension->set('columnType', '');
......
; <?php exit; ?> DO NOT REMOVE THIS LINE
; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.
[Development]
disable_merged_assets = 0
[Category]
test = "&amp;6^ geagea'''&quot;;;&amp;"
test2 = "&amp;6^ geagea'''&quot;;;&amp;"
[PluginsInstalled]
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter