Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
Maxime Marinel committed Jul 4, 2016
1 parent bf9327a commit eedfee8
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
vendor
composer.lock
4 changes: 4 additions & 0 deletions Lib/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function getConfigs($config_name = null)
return $this->configs;
}

if (!isset($this->configs[$config_name])) {
throw new UndefinedConfigException(sprintf('Config "%s" doesn\'t exist', $config_name));
}

return array(
$this->configs[$config_name],
);
Expand Down
7 changes: 7 additions & 0 deletions Lib/UndefinedConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Dw\Bundle\ConfiguratorBundle\Lib;

class UndefinedConfigException extends \InvalidArgumentException
{
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## ConfiguratorBundle

[![Build Status](https://travis-ci.org/bourvill/ConfiguratorBundle.svg?branch=master)](https://travis-ci.org/bourvill/ConfiguratorBundle)


@Todo write doc :)
134 changes: 129 additions & 5 deletions Tests/Lib/ConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,32 @@ public function setUp()
$this->objectManager = \Mockery::mock('\Doctrine\Common\Persistence\ObjectManager');
}

public function testGetAllConfigs()
{
$this->generateConfig();

$configurator = new Configurator($this->objectManager);

$this->assertEquals(array(), $configurator->getConfigs());
}

public function testAddConfig()
{
$this->generateConfig();
$configName = $this->faker->word;
$configs = array(
$configs = array(
'site_title' => array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
'type' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
'options' => array(
'choices' => array(
'choices' => array(
'Mode 1' => 1,
),
'required' => false,
),
),
);

$configurator = new Configurator($this->objectManager);
$configurator = new Configurator($this->objectManager);
$configInterface = \Mockery::mock('\Dw\Bundle\ConfiguratorBundle\Lib\ConfigInterface');

$this->assertEquals(array(), $configurator->getConfigs());
Expand All @@ -45,13 +54,128 @@ public function testAddConfig()
$this->assertEquals(array($configName => $configs), $configurator->getConfigs());
}

/**
* @expectedException \Dw\Bundle\ConfiguratorBundle\Lib\UndefinedConfigException
*/
public function testGetUndefinedConfigs()
{
$this->generateConfig();

$configurator = new Configurator($this->objectManager);

$this->assertEquals(array(), $configurator->getConfigs('test'));
}

public function testGetConfigsWithValidArg()
{
$this->generateConfig();
$configName = $this->faker->word;
$configs = array(
'site_title' => array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
'options' => array(
'choices' => array(
'Mode 1' => 1,
),
'required' => false,
),
),
);

$configurator = new Configurator($this->objectManager);
$configInterface = \Mockery::mock('\Dw\Bundle\ConfiguratorBundle\Lib\ConfigInterface');

$this->assertEquals(array(), $configurator->getConfigs());

$configInterface->shouldReceive('getName')->once()->andReturn($configName);
$configInterface->shouldReceive('getConfigs')->once()->andReturn($configs);

$configurator->addConfig($configInterface);

$this->assertEquals(array($configs), $configurator->getConfigs($configName));
}

/**
* @expectedException \Dw\Bundle\ConfiguratorBundle\Lib\UndefinedParamException
*/
public function testGetParamWithInvalidArgument()
{
$this->generateConfig();

$configurator = new Configurator($this->objectManager);

$configurator->getParam('wrongparam');
}

public function testGetParam()
{
$this->generateConfig(array('valid_param' => 'data'));

$configurator = new Configurator($this->objectManager);

$this->assertEquals('data', $configurator->getParam('valid_param'));
}

public function testGet()
{
$this->generateConfig(array('valid_param' => 'data'));

$configurator = new Configurator($this->objectManager);

$this->assertEquals('data', $configurator->get('valid_param'));
}

public function testGetParams()
{
$this->generateConfig(array('valid_param' => 'data'));

$configurator = new Configurator($this->objectManager);

$this->assertEquals(array('valid_param' => 'data'), $configurator->getParams());
}

public function testUpdateWithNoChange()
{
$this->generateConfig(array('valid_param' => 'data'));

$this->objectManager->shouldReceive('createQueryBuilder')->never();
$configurator = new Configurator($this->objectManager);

$configurator->update(array('valid_param' => 'data'));
}

public function testUpdateApplyChange()
{
$this->generateConfig(array('valid_param' => 'data'));

$qb = \Mockery::mock('Stub');
$qb->shouldReceive('expr->literal')->once();
$qb->shouldReceive('update->set->where->setParameter->getQuery->execute')->once();
$this->objectManager->shouldReceive('createQueryBuilder')->once()->andReturn($qb);
$this->objectManager->shouldReceive('update');

$configurator = new Configurator($this->objectManager);

$configurator->update(array('valid_param' => 'data1'));
}

public function tearDown()
{
\Mockery::close();
}

public function generateConfig($return = array())
{
$this->objectManager->shouldReceive('getRepository->findAll')->once()->andReturn($return);
$datas = array();

foreach ($return as $configParam => $configValue) {
$mockConfig = \Mockery::mock('Config');
$mockConfig->shouldReceive('getParam')->andReturn($configParam)->once();
$mockConfig->shouldReceive('getParamValue')->andReturn($configValue)->once();

$datas[] = $mockConfig;
}

$this->objectManager->shouldReceive('getRepository->findAll')->once()->andReturn($datas);
}
}
15 changes: 15 additions & 0 deletions Tests/Twig/ConfiguratorExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ public function testGetName()
$this->assertEquals('dw_configurator', $extension->getName());
}

public function testGetFunctions()
{
$extension = new ConfiguratorExtension($this->configurator->reveal());

$functions = $extension->getFunctions();
$this->assertEquals(1, count($functions));

$functionParam = $functions[0];
$functionParamCallable = $functionParam->getCallable();

$this->assertEquals('param', $functionParam->getName());
$this->assertEquals(get_class($extension), get_class($functionParamCallable[0]));
$this->assertEquals('getParam', $functionParamCallable[1]);
}

public function testGetParam()
{
$extension = new ConfiguratorExtension($this->configurator->reveal());
Expand Down

0 comments on commit eedfee8

Please sign in to comment.