Skip to content

Commit

Permalink
Merge pull request #228 from kokspflanze/bugfix/style_align_jqgrid
Browse files Browse the repository at this point in the history
Bugfix for style-align @ jqGrid
  • Loading branch information
ThaDafinser committed Feb 29, 2016
2 parents a3357c8 + c168d71 commit a45e4d3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",

Expand Down
27 changes: 18 additions & 9 deletions src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
class Columns extends AbstractHelper implements ServiceLocatorAwareInterface
{
/** @var \Zend\I18n\Translator\Translator|null|false */
private $translator;

const STYLE_BOLD = 'cellvalue = \'<span style="font-weight: bold;">\' + cellvalue + \'</span>\';';
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -270,7 +279,7 @@ private function getStyles(Column\AbstractColumn $col)
break;

case 'ZfcDatagrid\Column\Style\Align':
$styleString = 'cellvalue = \'<span style="text-align: ' . $style->getAlignment() . ';">\' + cellvalue + \'</span>\';';
// do NOTHING! we have to add the align style in the gridcell and not in a span!
break;

default:
Expand Down
50 changes: 50 additions & 0 deletions tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
class ColumnsTest extends PHPUnit_Framework_TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\View\HelperPluginManager */
private $sm;

/**
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);
}


}

0 comments on commit a45e4d3

Please sign in to comment.