diff --git a/composer.json b/composer.json index 26a7b04b..ccae979b 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "require-dev": { - "phpunit/PHPUnit": "~4.0", + "phpunit/PHPUnit": "~4|~5", "satooshi/php-coveralls": "~0.6", "fabpot/php-cs-fixer": "1.10.*", diff --git a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php index f1e00b06..fdd099d1 100644 --- a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php +++ b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php @@ -13,6 +13,7 @@ */ class Columns extends AbstractHelper implements ServiceLocatorAwareInterface { + /** @var \Zend\I18n\Translator\Translator|null|false */ private $translator; const STYLE_BOLD = 'cellvalue = \'\' + cellvalue + \'\';'; @@ -35,12 +36,8 @@ private function translate($message) } if (null === $this->translator) { - if ($this->getServiceLocator() - ->getServiceLocator() - ->has('translator')) { - $this->translator = $this->getServiceLocator() - ->getServiceLocator() - ->get('translator'); + if ($this->getServiceLocator()->has('translator')) { + $this->translator = $this->getServiceLocator()->get('translator'); } else { $this->translator = false; @@ -82,8 +79,20 @@ public function __invoke(array $columns) $options['formatter'] = (string) $formatter; } - if ($column->getType() instanceof Type\Number) { - $options['align'] = (string) 'right'; + $alignAlreadyDefined = false; + if ($column->hasStyles()) { + foreach ($column->getStyles() as $style) { + /** @var \ZfcDatagrid\Column\Style\Align $style */ + if (get_class($style) == 'ZfcDatagrid\Column\Style\Align') { + $options['align'] = $style->getAlignment(); + $alignAlreadyDefined = true; + break; + } + } + } + + if (!$alignAlreadyDefined && $column->getType() instanceof Type\Number) { + $options['align'] = Column\Style\Align::$RIGHT; } /* @@ -270,7 +279,7 @@ private function getStyles(Column\AbstractColumn $col) break; case 'ZfcDatagrid\Column\Style\Align': - $styleString = 'cellvalue = \'\' + cellvalue + \'\';'; + // do NOTHING! we have to add the align style in the gridcell and not in a span! break; default: diff --git a/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php b/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php index ebc456e5..9ba37adb 100644 --- a/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php +++ b/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php @@ -12,6 +12,7 @@ */ class ColumnsTest extends PHPUnit_Framework_TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\View\HelperPluginManager */ private $sm; /** @@ -41,6 +42,7 @@ public function testServiceLocator() $helper = new Helper\Columns(); $helper->setServiceLocator($this->sm); + $this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorInterface', $helper->getServiceLocator()); $this->assertSame($this->sm, $helper->getServiceLocator()); } @@ -223,4 +225,52 @@ public function testStyleByValueNotSupported() $this->setExpectedException('Exception', 'Currently not supported filter operation: "' . Filter::IN . '"'); $result = $helper($cols); } + + public function testTranslate() + { + /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\ServiceManager\ServiceManager $sm */ + $sm = $this->getMock('Zend\ServiceManager\ServiceManager', null); + + $helper = new Helper\Columns(); + $helper->setServiceLocator($sm); + + $reflection = new \ReflectionClass($helper); + $method = $reflection->getMethod('translate'); + $method->setAccessible(true); + + $result = $method->invokeArgs($helper, ['test']); + + $this->assertEquals('test', $result); + } + + public function testTranslateWithMockedTranslator() + { + /** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\ServiceManager\ServiceManager $sm */ + $sm = $this->getMock('Zend\ServiceManager\ServiceManager', null); + + $translator = $this->getMockBuilder('Zend\I18n\Translator\Translator') + ->disableOriginalConstructor() + ->setMethods(['translate']) + ->getMock(); + + // Configure the stub. + $translator->method('translate') + ->will($this->returnValueMap([ + ['test', 'default', null, 'translate'] + ])); + + $sm->setService('translator', $translator); + $helper = new Helper\Columns(); + $helper->setServiceLocator($sm); + + $reflection = new \ReflectionClass($helper); + $method = $reflection->getMethod('translate'); + $method->setAccessible(true); + + $result = $method->invokeArgs($helper, ['test']); + + $this->assertEquals('translate', $result); + } + + }