From ebfc7a865908efd5fc97cbbd4ac31258c4a9e5b3 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Fri, 26 Jul 2013 20:01:08 +0200 Subject: [PATCH] Completed UnitTests for Columns --- .../Column/Action/AbstractActionTest.php | 140 ++++++++++++++ .../Column/Action/ButtonTest.php | 33 ++++ .../Column/Action/IconTest.php | 35 ++++ tests/ZfcDatagridTest/Column/ActionTest.php | 43 +++++ tests/ZfcDatagridTest/Column/IconTest.php | 57 ++++++ tests/ZfcDatagridTest/Column/ImageTest.php | 22 +++ .../Column/Style/ColorTest.php | 74 ++++++++ .../Column/Type/DateTimeTest.php | 171 ++++++++++++++++++ .../Column/Type/PhpArrayTest.php | 46 +++++ .../Column/Type/StringTest.php | 19 ++ 10 files changed, 640 insertions(+) create mode 100644 tests/ZfcDatagridTest/Column/Action/AbstractActionTest.php create mode 100644 tests/ZfcDatagridTest/Column/Action/ButtonTest.php create mode 100644 tests/ZfcDatagridTest/Column/Action/IconTest.php create mode 100644 tests/ZfcDatagridTest/Column/ActionTest.php create mode 100644 tests/ZfcDatagridTest/Column/IconTest.php create mode 100644 tests/ZfcDatagridTest/Column/ImageTest.php create mode 100644 tests/ZfcDatagridTest/Column/Style/ColorTest.php create mode 100644 tests/ZfcDatagridTest/Column/Type/DateTimeTest.php create mode 100644 tests/ZfcDatagridTest/Column/Type/PhpArrayTest.php create mode 100644 tests/ZfcDatagridTest/Column/Type/StringTest.php diff --git a/tests/ZfcDatagridTest/Column/Action/AbstractActionTest.php b/tests/ZfcDatagridTest/Column/Action/AbstractActionTest.php new file mode 100644 index 00000000..8ac36975 --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Action/AbstractActionTest.php @@ -0,0 +1,140 @@ +column = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); + $this->column->setUniqueId('colName'); + } + + public function testLink () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->assertEquals('#', $action->getLink()); + + $action->setLink('/my/page/is/cool'); + $this->assertEquals('/my/page/is/cool', $action->getLink()); + } + + public function testAttributes () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->assertEquals(array(), $action->getAttributes()); + + $this->assertEquals('', $action->getAttribute('something')); + + $action->setAttribute('class', 'error'); + $this->assertCount(1, $action->getAttributes()); + $this->assertEquals(array( + 'class' => 'error' + ), $action->getAttributes()); + + $this->assertEquals('error', $action->getAttribute('class')); + } + + public function testTitle () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->assertEquals('', $action->getTitle()); + + $action->setTitle('This is my action'); + $this->assertEquals('This is my action', $action->getTitle()); + } + + public function testAddClass () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->assertEquals('', $action->getAttribute('class')); + + $action->addClass('cssClass'); + $this->assertEquals('cssClass', $action->getAttribute('class')); + + $action->addClass('cssClass2'); + $this->assertEquals('cssClass cssClass2', $action->getAttribute('class')); + } + + public function testShowOnValue () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->assertCount(0, $action->getShowOnValues()); + + $this->assertFalse($action->hasShowOnValues()); + $action->addShowOnValue($this->column, '23', Filter::EQUAL); + $this->assertTrue($action->hasShowOnValues()); + } + + public function testIsDisplayed () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->assertTrue($action->isDisplayed(array( + $this->column->getUniqueId() => '23' + ))); + + // EQUAL + $action->addShowOnValue($this->column, '23', Filter::EQUAL); + + $this->assertTrue($action->isDisplayed(array( + $this->column->getUniqueId() => '23' + ))); + + $this->assertFalse($action->isDisplayed(array( + $this->column->getUniqueId() => '33' + ))); + } + + public function testIsDisplayedNotEqual () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $action->addShowOnValue($this->column, '23', Filter::NOT_EQUAL); + + $this->assertTrue($action->isDisplayed(array( + $this->column->getUniqueId() => '32' + ))); + + $this->assertFalse($action->isDisplayed(array( + $this->column->getUniqueId() => '23' + ))); + } + + public function testIsDisplayedException () + { + /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ + $action = $this->getMockForAbstractClass('ZfcDatagrid\Column\Action\AbstractAction'); + + $this->setExpectedException('Exception'); + $action->addShowOnValue($this->column, '23', Filter::BETWEEN); + $action->isDisplayed(array( + $this->column->getUniqueId() => '32' + )); + } +} diff --git a/tests/ZfcDatagridTest/Column/Action/ButtonTest.php b/tests/ZfcDatagridTest/Column/Action/ButtonTest.php new file mode 100644 index 00000000..715f29c1 --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Action/ButtonTest.php @@ -0,0 +1,33 @@ +assertEquals(array( + 'class' => 'btn' + ), $button->getAttributes()); + } + + public function testLabel () + { + $button = new Button(); + + $button->setLabel('My label'); + $this->assertEquals('My label', $button->getLabel()); + + $html = 'My label'; + $this->assertEquals($html, $button->toHtml()); + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/Column/Action/IconTest.php b/tests/ZfcDatagridTest/Column/Action/IconTest.php new file mode 100644 index 00000000..8e1347eb --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Action/IconTest.php @@ -0,0 +1,35 @@ +assertEquals(array(), $icon->getAttributes()); + } + + public function testIcon () + { + $icon = new Icon(); + $icon->setIconClass('icon-add'); + + $this->assertEquals(array( + 'class' => 'icon-add' + ), $icon->getAttributes()); + + $icon->setIconLink('/images/icon/add.png'); + + $html = ''; + $this->assertEquals($html, $icon->toHtml()); + } +} diff --git a/tests/ZfcDatagridTest/Column/ActionTest.php b/tests/ZfcDatagridTest/Column/ActionTest.php new file mode 100644 index 00000000..5fb96dbe --- /dev/null +++ b/tests/ZfcDatagridTest/Column/ActionTest.php @@ -0,0 +1,43 @@ +assertEquals('action', $column->getUniqueId()); + $this->assertEquals('Actions', $column->getLabel()); + $this->assertFalse($column->isUserSortEnabled()); + $this->assertFalse($column->isUserFilterEnabled()); + $this->assertFalse($column->isRowClickEnabled()); + } + + public function testAddAction () + { + $column = new Column\Action(); + + $this->assertCount(0, $column->getActions()); + + $action = $this->getMock('ZfcDatagrid\Column\Action\Button'); + $column->addAction($action); + + $this->assertCount(1, $column->getActions()); + + $action2 = $this->getMock('ZfcDatagrid\Column\Action\Button'); + $column->addAction($action2); + $action3 = $this->getMock('ZfcDatagrid\Column\Action\Button'); + $column->addAction($action3); + + $this->assertCount(3, $column->getActions()); + } +} diff --git a/tests/ZfcDatagridTest/Column/IconTest.php b/tests/ZfcDatagridTest/Column/IconTest.php new file mode 100644 index 00000000..51d92773 --- /dev/null +++ b/tests/ZfcDatagridTest/Column/IconTest.php @@ -0,0 +1,57 @@ +assertEquals('icon', $col->getUniqueId()); + $this->assertFalse($col->isUserSortEnabled()); + $this->assertFalse($col->isUserFilterEnabled()); + $this->assertEquals(5, $col->getWidth()); + } + + public function testIconClass () + { + $col = new Icon(); + + $this->assertFalse($col->hasIconClass()); + + $col->setIconClass('icon-add'); + $this->assertEquals('icon-add', $col->getIconClass()); + $this->assertTrue($col->hasIconClass()); + } + + public function testIconLink () + { + $col = new Icon(); + + $this->assertFalse($col->hasIconLink()); + + $col->setIconLink('/images/21/add.png'); + $this->assertEquals('/images/21/add.png', $col->getIconLink()); + $this->assertTrue($col->hasIconLink()); + } + + public function testTitle () + { + $col = new Icon(); + + $this->assertFalse($col->hasTitle()); + $this->assertEquals('', $col->getTitle()); + + $col->setTitle('blubb'); + $this->assertEquals('blubb', $col->getTitle()); + $this->assertTrue($col->hasTitle()); + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/Column/ImageTest.php b/tests/ZfcDatagridTest/Column/ImageTest.php new file mode 100644 index 00000000..e5bfa28d --- /dev/null +++ b/tests/ZfcDatagridTest/Column/ImageTest.php @@ -0,0 +1,22 @@ +assertEquals('image', $col->getUniqueId()); + $this->assertFalse($col->isUserSortEnabled()); + $this->assertFalse($col->isUserFilterEnabled()); + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/Column/Style/ColorTest.php b/tests/ZfcDatagridTest/Column/Style/ColorTest.php new file mode 100644 index 00000000..6d79f617 --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Style/ColorTest.php @@ -0,0 +1,74 @@ +assertEquals(255, $style->getRed()); + $this->assertEquals(0, $style->getGreen()); + $this->assertEquals(0, $style->getBlue()); + $this->assertEquals('ff0000', $style->getRgbHexString()); + + $style = new Color(Color::$GREEN); + $this->assertEquals(0, $style->getRed()); + $this->assertEquals(255, $style->getGreen()); + $this->assertEquals(0, $style->getBlue()); + $this->assertEquals('00ff00', $style->getRgbHexString()); + + $style = new Color(Color::$BLUE); + $this->assertEquals(0, $style->getRed()); + $this->assertEquals(0, $style->getGreen()); + $this->assertEquals(255, $style->getBlue()); + $this->assertEquals('0000ff', $style->getRgbHexString()); + + $style = new Color(50, 70, 30); + $this->assertEquals(50, $style->getRed()); + $this->assertEquals(70, $style->getGreen()); + $this->assertEquals(30, $style->getBlue()); + $this->assertEquals('32461e', $style->getRgbHexString()); + } + + public function testSetRgb(){ + $style = new Color(0, 0, 0); + + $style->setRgb(20, 10, 5); + $this->assertEquals(20, $style->getRed()); + $this->assertEquals(10, $style->getGreen()); + $this->assertEquals(5, $style->getBlue()); + + $style->setRed(33); + $this->assertEquals(33, $style->getRed()); + + $style->setGreen(44); + $this->assertEquals(44, $style->getGreen()); + + $style->setBlue(55); + $this->assertEquals(55, $style->getBlue()); + } + + public function testRgbHexString(){ + $style = new Color(0, 0, 0); + + $this->assertEquals('000000', $style->getRgbHexString()); + + $style->setRed(5); + $this->assertEquals('050000', $style->getRgbHexString()); + + $style->setGreen(6); + $this->assertEquals('050600', $style->getRgbHexString()); + + $style->setBlue(7); + $this->assertEquals('050607', $style->getRgbHexString()); + } +} diff --git a/tests/ZfcDatagridTest/Column/Type/DateTimeTest.php b/tests/ZfcDatagridTest/Column/Type/DateTimeTest.php new file mode 100644 index 00000000..a868d414 --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Type/DateTimeTest.php @@ -0,0 +1,171 @@ +setLocale('de_AT'); + $type->setSourceTimezone('UTC'); + $type->setOutputTimezone('UTC'); + $this->datetimeAT = $type; + + $type = new Type\DateTime(); + $type->setLocale('en_US'); + $type->setSourceTimezone('UTC'); + $type->setOutputTimezone('UTC'); + $this->datetimeEN = $type; + } + + public function testConstruct () + { + $type = new Type\DateTime(); + + $this->assertEquals('Y-m-d H:i:s', $type->getSourceDateTimeFormat()); + $this->assertEquals(IntlDateFormatter::MEDIUM, $type->getOutputDateType()); + $this->assertEquals(IntlDateFormatter::NONE, $type->getOutputTimeType()); + $this->assertEquals(Locale::getDefault(), $type->getLocale()); + + $this->assertEquals('UTC', $type->getSourceTimezone()); + $this->assertEquals(date_default_timezone_get(), $type->getOutputTimezone()); + + $this->assertEquals(Filter::GREATER_EQUAL, $type->getFilterDefaultOperation()); + } + + public function testTypeName () + { + $type = new Type\DateTime(); + + $this->assertEquals('dateTime', $type->getTypeName()); + } + + public function testSourceDateTimeFormat () + { + $type = new Type\DateTime(); + + $type->setSourceDateTimeFormat('Y-m-d'); + $this->assertEquals('Y-m-d', $type->getSourceDateTimeFormat()); + } + + public function testOutputDateType () + { + $type = new Type\DateTime(); + + $type->setOutputDateType(IntlDateFormatter::FULL); + $this->assertEquals(IntlDateFormatter::FULL, $type->getOutputDateType()); + } + + public function testOutputTimeType () + { + $type = new Type\DateTime(); + + $type->setOutputTimeType(IntlDateFormatter::SHORT); + $this->assertEquals(IntlDateFormatter::SHORT, $type->getOutputTimeType()); + } + + public function testLocale () + { + $type = new Type\DateTime(); + + $type->setLocale('de_AT'); + $this->assertEquals('de_AT', $type->getLocale()); + } + + public function testSourceTimezone () + { + $type = new Type\DateTime(); + + $type->setSourceTimezone('Europe/Vienna'); + $this->assertEquals('Europe/Vienna', $type->getSourceTimezone()); + } + + public function testOutputTimezone () + { + $type = new Type\DateTime(); + + $type->setOutputTimezone('Europe/Vaduz'); + $this->assertEquals('Europe/Vaduz', $type->getOutputTimezone()); + } + + public function testOutputPattern () + { + $type = new Type\DateTime(); + + $type->setOutputPattern('yyyymmdd hh:mm:ss z'); + $this->assertEquals('yyyymmdd hh:mm:ss z', $type->getOutputPattern()); + } + + /** + * Convert the user value to a filter value + */ + public function testFilterValueAT () + { + $type = clone $this->datetimeAT; + $this->assertEquals('2013-01-10 00:00:00', $type->getFilterValue('10.01.2013')); + + $type->setOutputTimeType(IntlDateFormatter::SHORT); + $this->assertEquals('2013-01-10 10:00:00', $type->getFilterValue('10.01.2013 10:00')); + } + + /** + * Convert the user value to a filter value + */ + public function testFilterValueEN () + { + $type = clone $this->datetimeEN; + $this->assertEquals('2013-01-10 00:00:00', $type->getFilterValue('Jan 10, 2013')); + + $type->setOutputTimeType(IntlDateFormatter::SHORT); + + $this->assertEquals('2013-01-10 10:00:00', $type->getFilterValue('Jan 10, 2013 10:00 AM')); + } + + + /** + * Convert the database value to a display value + */ + public function testUserValueAT () + { + $type = clone $this->datetimeAT; + $this->assertEquals('10.01.2013', $type->getUserValue('2013-01-10 00:00:00')); + + $type->setOutputTimeType(IntlDateFormatter::SHORT); + $this->assertEquals('10.01.2013 10:00', $type->getFilterValue('2013-01-10 00:00:000')); + } + + /** + * Convert the database value to a display value + */ + public function testUserValueEN () + { + $type = clone $this->datetimeEN; + $this->assertEquals('Jan 10, 2013', $type->getUserValue('2013-01-10 00:00:00')); + + $type->setOutputTimeType(IntlDateFormatter::SHORT); + $this->assertEquals('Jan 10, 2013 10:00 AM', $type->getFilterValue('2013-01-10 10:00:000')); + } +} diff --git a/tests/ZfcDatagridTest/Column/Type/PhpArrayTest.php b/tests/ZfcDatagridTest/Column/Type/PhpArrayTest.php new file mode 100644 index 00000000..fc2002b8 --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Type/PhpArrayTest.php @@ -0,0 +1,46 @@ +assertEquals('array', $type->getTypeName()); + } + + public function testUserValue () + { + $type = new Type\PhpArray(); + + $value = '1,2,3'; + $this->assertEquals(array( + 1, + 2, + 3 + ), $type->getUserValue($value)); + + $value = ''; + $this->assertEquals(array(), $type->getUserValue($value)); + + $value = array( + 1, + 2, + 3 + ); + $this->assertEquals(array( + 1, + 2, + 3 + ), $type->getUserValue($value)); + } +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/Column/Type/StringTest.php b/tests/ZfcDatagridTest/Column/Type/StringTest.php new file mode 100644 index 00000000..135f390c --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Type/StringTest.php @@ -0,0 +1,19 @@ +assertEquals('string', $type->getTypeName()); + } +} \ No newline at end of file