From ddb9c658b58bdaa5cb4d635fc4f199b428b1d10d Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Mon, 11 Mar 2013 22:52:23 +0100 Subject: [PATCH 1/4] Add tests for sensitive case --- src/Prompt/Confirm.php | 9 +++++++-- test/Adapater/AbstractAdapterTest.php | 12 ++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Prompt/Confirm.php b/src/Prompt/Confirm.php index 65a8a05..7ec79ea 100644 --- a/src/Prompt/Confirm.php +++ b/src/Prompt/Confirm.php @@ -67,8 +67,13 @@ public function __construct( * @return bool */ public function show() - { - $response = parent::show() === $this->yesChar; + { + $char = parent::show(); + if($this->ignoreCase) { + $response = strtolower($char) === strtolower($this->yesChar); + } else { + $response = $char === $this->yesChar; + } return $this->lastResponse = $response; } diff --git a/test/Adapater/AbstractAdapterTest.php b/test/Adapater/AbstractAdapterTest.php index 76d131a..24f3aa7 100644 --- a/test/Adapater/AbstractAdapterTest.php +++ b/test/Adapater/AbstractAdapterTest.php @@ -92,20 +92,12 @@ public function testReadCharWithMask() $char = $this->adapter->readChar('ar'); $this->assertEquals($char, 'a'); } - + public function testReadCharWithMaskInsensitiveCase() { fwrite($this->adapter->stream, 'bAr'); $char = $this->adapter->readChar('ar'); - $this->assertEquals($char, 'A'); - } - - public function testReadCharWithNoReturn() - { - fwrite($this->adapter->stream, 'bar'); - - $char = $this->adapter->readChar('foo'); - $this->assertEquals($char, ''); + $this->assertEquals($char, 'r'); } } From d721e23a8444dd5a5b38ad8fc4922e750d56cc44 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Mon, 11 Mar 2013 22:52:47 +0100 Subject: [PATCH 2/4] Add tests on prompt char & confirm --- test/Prompt/CharTest.php | 31 ++++++++++- test/Prompt/ConfirmTest.php | 108 ++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 test/Prompt/ConfirmTest.php diff --git a/test/Prompt/CharTest.php b/test/Prompt/CharTest.php index 77e5926..2d11224 100644 --- a/test/Prompt/CharTest.php +++ b/test/Prompt/CharTest.php @@ -49,14 +49,14 @@ public function testCanPromptChar() public function testCanPromptCharWithCharNotInDefaultMask() { - fwrite($this->adapter->stream, 'zywa'); + fwrite($this->adapter->stream, '*zywa'); $char = new Char(); $char->setEcho(false); $char->setConsole($this->adapter); ob_start(); $response = $char->show(); - $text = ob_get_clean(); + ob_get_clean(); $this->assertEquals('z', $response); } @@ -73,4 +73,31 @@ public function testCanPromptCharWithNewQuestionAndMask() $this->assertEquals($text, "Give a number\n"); $this->assertEquals('1', $response); } + + public function testCanPromptCharWithIgnoreCaseByDefault() + { + fwrite($this->adapter->stream, 'FOObar'); + + $char = new Char(); + $char->setEcho(false); + $char->setConsole($this->adapter); + ob_start(); + $response = $char->show(); + ob_get_clean(); + $this->assertEquals('F', $response); + } + + public function testCanPromptCharWithoutIgnoreCase() + { + fwrite($this->adapter->stream, 'FOObar'); + + $char = new Char(); + $char->setEcho(false); + $char->setConsole($this->adapter); + $char->setIgnoreCase(false); + ob_start(); + $response = $char->show(); + ob_get_clean(); + $this->assertEquals('b', $response); + } } diff --git a/test/Prompt/ConfirmTest.php b/test/Prompt/ConfirmTest.php new file mode 100644 index 0000000..a1df137 --- /dev/null +++ b/test/Prompt/ConfirmTest.php @@ -0,0 +1,108 @@ +adapter = new ConsoleAdapter(); + $this->adapter->stream = fopen('php://memory', 'w+'); + } + + public function tearDown() + { + fclose($this->adapter->stream); + } + + public function testCanPromptConfirm() + { + fwrite($this->adapter->stream, 'y'); + + $confirm = new Confirm("ZF2 is the better framework ?"); + $confirm->setEcho(false); + $confirm->setConsole($this->adapter); + ob_start(); + $response = $confirm->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertTrue($response); + } + + public function testCanPromptConfirmWithDefaultIgnoreCase() + { + fwrite($this->adapter->stream, 'Y'); + + $confirm = new Confirm("ZF2 is the better framework ?"); + $confirm->setEcho(false); + $confirm->setConsole($this->adapter); + ob_start(); + $response = $confirm->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertTrue($response); + } + + public function testCanPromptConfirmWithoutIgnoreCase() + { + fwrite($this->adapter->stream, 'Yn'); + + $confirm = new Confirm("ZF2 is the better framework ?"); + $confirm->setEcho(false); + $confirm->setConsole($this->adapter); + $confirm->setIgnoreCase(false); + ob_start(); + $response = $confirm->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertFalse($response); + } + + public function testCanPromptConfirmWithYesNoCharChanged() + { + fwrite($this->adapter->stream, 'on0'); + + $confirm = new Confirm("ZF2 is the better framework ?", "1", "0"); + $confirm->setEcho(false); + $confirm->setConsole($this->adapter); + ob_start(); + $response = $confirm->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertFalse($response); + } + + public function testCanPromptConfirmWithYesNoCharChangedWithSetter() + { + fwrite($this->adapter->stream, 'oaB'); + + $confirm = new Confirm("ZF2 is the better framework ?", "1", "0"); + $confirm->setYesChar("A"); + $confirm->setNoChar("B"); + $confirm->setEcho(false); + $confirm->setConsole($this->adapter); + ob_start(); + $response = $confirm->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertTrue($response); + } +} From 0cc7c3de85575ab0264fb0a63dfbc21aff96fc98 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Tue, 12 Mar 2013 08:10:54 +0100 Subject: [PATCH 3/4] Fix wording --- test/Prompt/ConfirmTest.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Prompt/ConfirmTest.php b/test/Prompt/ConfirmTest.php index a1df137..92aa384 100644 --- a/test/Prompt/ConfirmTest.php +++ b/test/Prompt/ConfirmTest.php @@ -37,13 +37,13 @@ public function testCanPromptConfirm() { fwrite($this->adapter->stream, 'y'); - $confirm = new Confirm("ZF2 is the better framework ?"); + $confirm = new Confirm("Is ZF2 the best framework ?"); $confirm->setEcho(false); $confirm->setConsole($this->adapter); ob_start(); $response = $confirm->show(); $text = ob_get_clean(); - $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertTrue($response); } @@ -51,13 +51,13 @@ public function testCanPromptConfirmWithDefaultIgnoreCase() { fwrite($this->adapter->stream, 'Y'); - $confirm = new Confirm("ZF2 is the better framework ?"); + $confirm = new Confirm("Is ZF2 the best framework ?"); $confirm->setEcho(false); $confirm->setConsole($this->adapter); ob_start(); $response = $confirm->show(); $text = ob_get_clean(); - $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertTrue($response); } @@ -65,14 +65,14 @@ public function testCanPromptConfirmWithoutIgnoreCase() { fwrite($this->adapter->stream, 'Yn'); - $confirm = new Confirm("ZF2 is the better framework ?"); + $confirm = new Confirm("Is ZF2 the best framework ?"); $confirm->setEcho(false); $confirm->setConsole($this->adapter); $confirm->setIgnoreCase(false); ob_start(); $response = $confirm->show(); $text = ob_get_clean(); - $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertFalse($response); } @@ -80,13 +80,13 @@ public function testCanPromptConfirmWithYesNoCharChanged() { fwrite($this->adapter->stream, 'on0'); - $confirm = new Confirm("ZF2 is the better framework ?", "1", "0"); + $confirm = new Confirm("Is ZF2 the best framework ?", "1", "0"); $confirm->setEcho(false); $confirm->setConsole($this->adapter); ob_start(); $response = $confirm->show(); $text = ob_get_clean(); - $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertFalse($response); } @@ -94,7 +94,7 @@ public function testCanPromptConfirmWithYesNoCharChangedWithSetter() { fwrite($this->adapter->stream, 'oaB'); - $confirm = new Confirm("ZF2 is the better framework ?", "1", "0"); + $confirm = new Confirm("Is ZF2 the best framework ?", "1", "0"); $confirm->setYesChar("A"); $confirm->setNoChar("B"); $confirm->setEcho(false); @@ -102,7 +102,7 @@ public function testCanPromptConfirmWithYesNoCharChangedWithSetter() ob_start(); $response = $confirm->show(); $text = ob_get_clean(); - $this->assertEquals($text, "ZF2 is the better framework ?\n"); + $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertTrue($response); } } From f05077e401ccc946394e4d87b6a025d43e8373db Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 12 Mar 2013 12:22:25 -0500 Subject: [PATCH 4/4] [zendframework/zf2#4005] CS fixes - trailing whitespace --- src/Prompt/Char.php | 2 +- src/Prompt/Confirm.php | 2 +- test/Adapater/AbstractAdapterTest.php | 2 +- test/Prompt/CharTest.php | 8 ++++---- test/Prompt/ConfirmTest.php | 10 +++++----- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Prompt/Char.php b/src/Prompt/Char.php index 55c2434..648ff2c 100644 --- a/src/Prompt/Char.php +++ b/src/Prompt/Char.php @@ -88,7 +88,7 @@ public function show() $mask = array_unique($mask); // remove duplicates $mask = implode("", $mask); // convert back to string } - + /** * Read char from console */ diff --git a/src/Prompt/Confirm.php b/src/Prompt/Confirm.php index 7ec79ea..83f949a 100644 --- a/src/Prompt/Confirm.php +++ b/src/Prompt/Confirm.php @@ -67,7 +67,7 @@ public function __construct( * @return bool */ public function show() - { + { $char = parent::show(); if($this->ignoreCase) { $response = strtolower($char) === strtolower($this->yesChar); diff --git a/test/Adapater/AbstractAdapterTest.php b/test/Adapater/AbstractAdapterTest.php index 24f3aa7..21ba5ae 100644 --- a/test/Adapater/AbstractAdapterTest.php +++ b/test/Adapater/AbstractAdapterTest.php @@ -92,7 +92,7 @@ public function testReadCharWithMask() $char = $this->adapter->readChar('ar'); $this->assertEquals($char, 'a'); } - + public function testReadCharWithMaskInsensitiveCase() { fwrite($this->adapter->stream, 'bAr'); diff --git a/test/Prompt/CharTest.php b/test/Prompt/CharTest.php index 2d11224..ffcac9d 100644 --- a/test/Prompt/CharTest.php +++ b/test/Prompt/CharTest.php @@ -73,11 +73,11 @@ public function testCanPromptCharWithNewQuestionAndMask() $this->assertEquals($text, "Give a number\n"); $this->assertEquals('1', $response); } - + public function testCanPromptCharWithIgnoreCaseByDefault() { fwrite($this->adapter->stream, 'FOObar'); - + $char = new Char(); $char->setEcho(false); $char->setConsole($this->adapter); @@ -86,11 +86,11 @@ public function testCanPromptCharWithIgnoreCaseByDefault() ob_get_clean(); $this->assertEquals('F', $response); } - + public function testCanPromptCharWithoutIgnoreCase() { fwrite($this->adapter->stream, 'FOObar'); - + $char = new Char(); $char->setEcho(false); $char->setConsole($this->adapter); diff --git a/test/Prompt/ConfirmTest.php b/test/Prompt/ConfirmTest.php index 92aa384..b9afd05 100644 --- a/test/Prompt/ConfirmTest.php +++ b/test/Prompt/ConfirmTest.php @@ -32,7 +32,7 @@ public function tearDown() { fclose($this->adapter->stream); } - + public function testCanPromptConfirm() { fwrite($this->adapter->stream, 'y'); @@ -46,7 +46,7 @@ public function testCanPromptConfirm() $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertTrue($response); } - + public function testCanPromptConfirmWithDefaultIgnoreCase() { fwrite($this->adapter->stream, 'Y'); @@ -60,7 +60,7 @@ public function testCanPromptConfirmWithDefaultIgnoreCase() $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertTrue($response); } - + public function testCanPromptConfirmWithoutIgnoreCase() { fwrite($this->adapter->stream, 'Yn'); @@ -75,7 +75,7 @@ public function testCanPromptConfirmWithoutIgnoreCase() $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertFalse($response); } - + public function testCanPromptConfirmWithYesNoCharChanged() { fwrite($this->adapter->stream, 'on0'); @@ -89,7 +89,7 @@ public function testCanPromptConfirmWithYesNoCharChanged() $this->assertEquals($text, "Is ZF2 the best framework ?\n"); $this->assertFalse($response); } - + public function testCanPromptConfirmWithYesNoCharChangedWithSetter() { fwrite($this->adapter->stream, 'oaB');