Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added config tests
  • Loading branch information
Miliooo committed Oct 21, 2013
commit fb105f3a18ee565c71c9f3b97bf91650e333e552
141 changes: 141 additions & 0 deletions Tests/DependencyInjection/FOSMessageExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace FOS\MessageBundle\Tests\DependencyInjection;

use FOS\MessageBundle\DependencyInjection\FOSMessageExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Parser;

class FOSMessageExtensionTest extends \PHPUnit_Framework_TestCase
{

/** @var ContainerBuilder */
protected $configuration;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename the property to containerBuilder. It will be more readable


/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testMessageLoadThrowsExceptionUnlessDbDriverSet()
{
$loader = new FOSMessageExtension();
$config = $this->getEmptyConfig();
unset($config['db_driver']);
$loader->load(array($config), new ContainerBuilder());
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testMessageLoadThrowsExceptionUnlessThreadClassSet()
{
$loader = new FOSMessageExtension();
$config = $this->getEmptyConfig();
unset($config['thread_class']);
$loader->load(array($config), new ContainerBuilder());
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testMessageLoadThrowsExceptionUnlessMessageClassSet()
{
$loader = new FOSMessageExtension();
$config = $this->getEmptyConfig();
unset($config['message_class']);
$loader->load(array($config), new ContainerBuilder());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid db driver "foo"
*/
public function testUnsupportedDbDriverThrowsException()
{
$loader = new FOSMessageExtension();
$config = $this->getEmptyConfig();
$config['db_driver'] = "foo";
$loader->load(array($config), new ContainerBuilder());
}

public function testFlashesAreDisabledByDefault()
{
$this->createEmptyConfiguration();
$this->assertNotHasDefinition('fos_message.flash_listener');
}

public function testUserEnablesFlashes()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSMessageExtension();
$config = $this->getEmptyConfigWithFlashesEnabled();
$loader->load(array($config), $this->configuration);

$this->assertHasDefinition('fos_message.flash_listener');
$this->assertParameter('success', 'fos_message.flash_messages_key');
}

protected function createEmptyConfiguration()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSMessageExtension();
$config = $this->getEmptyConfig();
$loader->load(array($config), $this->configuration);
$this->assertTrue($this->configuration instanceof ContainerBuilder);
}

/**
* getEmptyConfig
*
* @return array
*/
protected function getEmptyConfig()
{
$yaml = <<<EOF
db_driver: mongodb
thread_class: Acme\MyBundle\Entity\Thread
message_class: Acme\MyBundle\Entity\Message
EOF;
$parser = new Parser();

return $parser->parse($yaml);
}

protected function getEmptyConfigWithFlashesEnabled()
{
$yaml = <<<EOF
db_driver: mongodb
thread_class: Acme\MyBundle\Entity\Thread
message_class: Acme\MyBundle\Entity\Message
flash_messages:
enabled: true
EOF;
$parser = new Parser();

return $parser->parse($yaml);
}

/**
* @param mixed $value
* @param string $key
*/
private function assertParameter($value, $key)
{
$this->assertEquals($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key));
}

/**
* @param string $id
*/
private function assertHasDefinition($id)
{
$this->assertTrue(($this->configuration->hasDefinition($id) ? : $this->configuration->hasAlias($id)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the space in the shortcur ternay operator

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and you should use || instead of a ternary operator IMO

}

/**
* @param string $id
*/
private function assertNotHasDefinition($id)
{
$this->assertFalse(($this->configuration->hasDefinition($id) ? : $this->configuration->hasAlias($id)));
}
}