Passed
Push — main ( 2c83d5...fb3f8a )
by Siad
05:21
created

ConsoleInputHandlerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefaultValue() 0 11 1
A testMultipleChoiceQuestion() 0 10 1
A testYesNoQuestion() 0 10 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Test\Input;
21
22
use Phing\Input\ConsoleInputHandler;
23
use Phing\Input\InputRequest;
24
use Phing\Input\MultipleChoiceInputRequest;
25
use Phing\Input\YesNoInputRequest;
26
use PHPUnit\Framework\TestCase;
27
use Symfony\Component\Console\Output\NullOutput;
28
29
class ConsoleInputHandlerTest extends TestCase
30
{
31
    public function testDefaultValue()
32
    {
33
        $inputStream = $this->createStream([' ']);
34
        $output = new NullOutput();
35
        $request = new InputRequest("Enter a value");
36
        $request->setDefaultValue('default');
37
        $handler = new ConsoleInputHandler($inputStream, $output);
38
39
        $handler->handleInput($request);
40
41
        self::assertEquals('default', $request->getInput());
42
    }
43
44
    public function testMultipleChoiceQuestion()
45
    {
46
        $inputStream = $this->createStream(['choice1']);
47
        $output = new NullOutput();
48
        $request = new MultipleChoiceInputRequest("Enter a choice", ['choice1', 'choice2']);
49
        $handler = new ConsoleInputHandler($inputStream, $output);
50
51
        $handler->handleInput($request);
52
53
        self::assertEquals('choice1', $request->getInput());
54
    }
55
56
    public function testYesNoQuestion()
57
    {
58
        $inputStream = $this->createStream(['no']);
59
        $output = new NullOutput();
60
        $request = new YesNoInputRequest("Enter a choice", ['yes', 'no']);
61
        $handler = new ConsoleInputHandler($inputStream, $output);
62
63
        $handler->handleInput($request);
64
65
        self::assertFalse($request->getInput());
66
    }
67
68
    private function createStream(array $inputs)
69
    {
70
        $stream = fopen('php://memory', 'r+', false);
71
        fwrite($stream, implode(PHP_EOL, $inputs));
72
        rewind($stream);
73
        return $stream;
74
    }
75
}
76