commit 3dc9a915d26a8ec2cd29801a28b61615ae7bcb25 Author: Joel Pittet Date: Fri Aug 1 18:38:52 2014 -0700 add twig tests diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index c09d3e0..c806982 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -19,6 +19,19 @@ class AttributeTest extends UnitTestCase { /** + * The mock container. + * + * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject + */ + protected $container; + + public function setUp() { + $this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') + ->setMethods(array('get')) + ->getMock(); + } + + /** * Tests the constructor of the attribute class. */ public function testConstructor() { @@ -104,7 +117,7 @@ public function testRemoveClasses() { } /** - * Tests removing class attributes with the AttributeArray helper method. + * Tests removing class attributes with the Attribute helper methods. * @covers ::removeClass() * @covers ::addClass() */ @@ -122,6 +135,62 @@ public function testChainAddRemoveClasses() { } /** + * Tests the twig calls to the Attribute. + * @dataProvider providerTestAttributeClassHelpers + * + * @covers ::removeClass() + * @covers ::addClass() + */ + public function testTwigAddRemoveClasses($template, $expected, $seed_attributes = array()) { + $loader = new \Twig_Loader_String(); + $twig = new \Twig_Environment($loader); + $data = array('attributes' => new Attribute($seed_attributes)); + $result = $twig->render($template, $data); + $this->assertEquals($expected, $result); + } + + /** + * Provides tests data for testEscaping + * + * @return array + * An array of test data each containing of a twig template string, + * a resulting string of classes and an optional array of attributes. + */ + public function providerTestAttributeClassHelpers() { + return array( + array("{{ attributes.class }}", ''), + array("{{ attributes.addClass('everest').class }}", 'everest'), + array("{{ attributes.addClass(['k2', 'kangchenjunga']).class }}", 'k2 kangchenjunga'), + array("{{ attributes.addClass('lhotse', 'makalu', 'cho-oyu').class }}", 'lhotse makalu cho-oyu'), + array( + "{{ attributes.addClass('nanga-parbat').class }}", + 'dhaulagiri manaslu nanga-parbat', + array('class' => array('dhaulagiri', 'manaslu')), + ), + array( + "{{ attributes.removeClass('annapurna').class }}", + 'gasherbrum-i', + array('class' => array('annapurna', 'gasherbrum-i')), + ), + array( + "{{ attributes.removeClass(['broad peak']).class }}", + 'gasherbrum-ii', + array('class' => array('broad peak', 'gasherbrum-ii')), + ), + array( + "{{ attributes.removeClass('gyachung-kang', 'shishapangma').class }}", + '', + array('class' => array('shishapangma', 'gyachung-kang')), + ), + array( + "{{ attributes.removeClass('nuptse').addClass('annapurna-ii').class }}", + 'himalchuli annapurna-ii', + array('class' => array('himalchuli', 'nuptse')), + ), + ); + } + + /** * Tests iterating on the values of the attribute. */ public function testIterate() { @@ -142,6 +211,29 @@ public function testIterate() { } /** + * Sets up a mock expectation for the container get() method. + * + * @param string $service_name + * The service name to expect for the get() method. + * @param mixed $return + * The value to return from the mocked container get() method. + */ + protected function setMockContainerService($service_name, $return = NULL) { + $expects = $this->container->expects($this->once()) + ->method('get') + ->with($service_name); + + if (isset($return)) { + $expects->will($this->returnValue($return)); + } + else { + $expects->will($this->returnValue(TRUE)); + } + + \Drupal::setContainer($this->container); + } + + /** * Tests printing of an attribute. */ public function testPrint() {