From e7991ae474e6dc7835c3c15a8eab9ebe1f752a88 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Fri, 22 Jul 2016 19:57:17 +0300 Subject: [PATCH 1/8] covers Console\Helper\Table `style is not defined` --- .../Component/Console/Tests/Helper/TableTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index d1991c50ec549..d452bcec38d7e 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -694,6 +694,16 @@ public function testColumnWiths() $this->assertEquals($expected, $this->getOutputContent($output)); } + public function testIsNotDefinedStyleException() + { + $this->setExpectedException( + 'InvalidArgumentException', + 'Style "absent" is not defined.' + ); + $table = new Table($this->getOutputStream()); + $table->setStyle('absent'); + } + protected function getOutputStream() { return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false); From 993a77f42ec3c45b9932a04fa64d1aafafc1d8b6 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Fri, 22 Jul 2016 20:03:01 +0300 Subject: [PATCH 2/8] move common code logic --- .../Component/Console/Helper/Table.php | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 7f2dd1b2380a8..7a44966abc86e 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -131,13 +131,7 @@ public static function getStyleDefinition($name) */ public function setStyle($name) { - if ($name instanceof TableStyle) { - $this->style = $name; - } elseif (isset(self::$styles[$name])) { - $this->style = self::$styles[$name]; - } else { - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); - } + $this->style = $this->findStyle($name); return $this; } @@ -164,13 +158,7 @@ public function setColumnStyle($columnIndex, $name) { $columnIndex = intval($columnIndex); - if ($name instanceof TableStyle) { - $this->columnStyles[$columnIndex] = $name; - } elseif (isset(self::$styles[$name])) { - $this->columnStyles[$columnIndex] = self::$styles[$name]; - } else { - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); - } + $this->columnStyles[$columnIndex] = $this->findStyle($name); return $this; } @@ -701,4 +689,17 @@ private static function initStyles() 'symfony-style-guide' => $styleGuide, ); } + + private function findStyle($name) + { + if ($name instanceof TableStyle) { + return $name; + } + + if (isset(self::$styles[$name])) { + return self::$styles[$name]; + } + + throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + } } From c3647eca176567bb74bb2c6a6e863418c865069f Mon Sep 17 00:00:00 2001 From: ReenExe Date: Fri, 22 Jul 2016 20:08:20 +0300 Subject: [PATCH 3/8] test before code -> covers `Table::getStyleDefinition` --- src/Symfony/Component/Console/Tests/Helper/TableTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index d452bcec38d7e..54b69aa218ac3 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -704,6 +704,15 @@ public function testIsNotDefinedStyleException() $table->setStyle('absent'); } + public function testGetStyleDefinition() + { + $this->setExpectedException( + 'InvalidArgumentException', + 'Style "absent" is not defined.' + ); + Table::getStyleDefinition('absent'); + } + protected function getOutputStream() { return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false); From db2010cfbb346b272ea8a4c6566457a27ae046cb Mon Sep 17 00:00:00 2001 From: ReenExe Date: Fri, 22 Jul 2016 20:09:52 +0300 Subject: [PATCH 4/8] fix `notice` (Undefined index: `key`) --- src/Symfony/Component/Console/Helper/Table.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 7a44966abc86e..d5cdfe34945d7 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -115,11 +115,11 @@ public static function getStyleDefinition($name) self::$styles = self::initStyles(); } - if (!self::$styles[$name]) { - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + if (isset(self::$styles[$name])) { + return self::$styles[$name]; } - return self::$styles[$name]; + throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); } /** From f1e68d089c1653f8b9316eac81750f8be36af870 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Mon, 25 Jul 2016 20:02:37 +0300 Subject: [PATCH 5/8] use `@expectedException` and rename private method (after review #19406) --- src/Symfony/Component/Console/Helper/Table.php | 12 ++++++------ .../Component/Console/Tests/Helper/TableTest.php | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index d5cdfe34945d7..bbda1c75061da 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -131,7 +131,7 @@ public static function getStyleDefinition($name) */ public function setStyle($name) { - $this->style = $this->findStyle($name); + $this->style = $this->resolveStyle($name); return $this; } @@ -158,7 +158,7 @@ public function setColumnStyle($columnIndex, $name) { $columnIndex = intval($columnIndex); - $this->columnStyles[$columnIndex] = $this->findStyle($name); + $this->columnStyles[$columnIndex] = $this->resolveStyle($name); return $this; } @@ -690,16 +690,16 @@ private static function initStyles() ); } - private function findStyle($name) + private function resolveStyle($name) { if ($name instanceof TableStyle) { return $name; } - if (isset(self::$styles[$name])) { - return self::$styles[$name]; + if (empty(self::$styles[$name])) { + throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); } - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + return self::$styles[$name]; } } diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index 54b69aa218ac3..fd09e3b3a8689 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -694,22 +694,22 @@ public function testColumnWiths() $this->assertEquals($expected, $this->getOutputContent($output)); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Style "absent" is not defined. + */ public function testIsNotDefinedStyleException() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Style "absent" is not defined.' - ); $table = new Table($this->getOutputStream()); $table->setStyle('absent'); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Style "absent" is not defined. + */ public function testGetStyleDefinition() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Style "absent" is not defined.' - ); Table::getStyleDefinition('absent'); } From 04283ac844136be41986016744387467a860d923 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Wed, 27 Jul 2016 12:12:55 +0300 Subject: [PATCH 6/8] same condition style --- src/Symfony/Component/Console/Helper/Table.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index bbda1c75061da..c1fa9192d884c 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -115,11 +115,11 @@ public static function getStyleDefinition($name) self::$styles = self::initStyles(); } - if (isset(self::$styles[$name])) { - return self::$styles[$name]; + if (empty(self::$styles[$name])) { + throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); } - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + return self::$styles[$name]; } /** From 99fe32bbd1ddb250891f2469e341716480e8baf6 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Thu, 28 Jul 2016 11:17:57 +0300 Subject: [PATCH 7/8] @expectedException from Symfony --- src/Symfony/Component/Console/Tests/Helper/TableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index fd09e3b3a8689..9eb34543772a8 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -695,7 +695,7 @@ public function testColumnWiths() } /** - * @expectedException \InvalidArgumentException + * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException * @expectedExceptionMessage Style "absent" is not defined. */ public function testIsNotDefinedStyleException() @@ -705,7 +705,7 @@ public function testIsNotDefinedStyleException() } /** - * @expectedException \InvalidArgumentException + * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException * @expectedExceptionMessage Style "absent" is not defined. */ public function testGetStyleDefinition() From 9b2e4d404f335192c2de41ed20a38459514a62cb Mon Sep 17 00:00:00 2001 From: ReenExe Date: Thu, 28 Jul 2016 16:07:25 +0300 Subject: [PATCH 8/8] prefer `isset` --- src/Symfony/Component/Console/Helper/Table.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index c1fa9192d884c..1434562dfea69 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -115,11 +115,11 @@ public static function getStyleDefinition($name) self::$styles = self::initStyles(); } - if (empty(self::$styles[$name])) { - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + if (isset(self::$styles[$name])) { + return self::$styles[$name]; } - return self::$styles[$name]; + throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); } /** @@ -696,10 +696,10 @@ private function resolveStyle($name) return $name; } - if (empty(self::$styles[$name])) { - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + if (isset(self::$styles[$name])) { + return self::$styles[$name]; } - return self::$styles[$name]; + throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); } }