Completed
Pull Request — master (#7)
by John
04:58
created

AbstractCommandTestCase::getExpectedResponseText()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
/**
4
 * This file is part of graze/dynamark3-client.
5
 *
6
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/dynamark3-client/blob/master/LICENSE.md
12
 * @link https://github.com/graze/dynamark3-client
13
 */
14
15
namespace Graze\Dynamark3Client\Test;
16
17
use \Graze\TelnetClient\PromptMatcher;
18
use \Graze\TelnetClient\TelnetResponseInterface;
19
use \Graze\Dynamark3Client\Dynamark3Constants;
20
use \Graze\Dynamark3Client\Command\CommandInterface;
21
use \Mockery as m;
22
23
abstract class AbstractCommandTestCase extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * Returns the Command to be tested
27
     *
28
     * @return \Graze\Dynamark3Client\Command\CommandInterface
29
     */
30
    abstract protected function getCommand();
31
32
    /**
33
     * An example response from the Dynamark3 server, including line endings
34
     *
35
     * @return string
36
     */
37
    abstract protected function getRawTelnetResponse();
38
39
    /**
40
     * The expected response text once Command has parsed the TelnetResponse
41
     *
42
     * @return string
43
     */
44
    abstract protected function getExpectedResponseText();
45
46
    /**
47
     * @return void
48
     */
49
    public function testCommandSuccess()
50
    {
51
        $command = $this->getCommand();
52
53
        $this->assertInstanceOf(CommandInterface::class, $command);
54
55
        $telnetResponse = $this->buildTelnetResponse(
56
            $command->getPrompt() ?: Dynamark3Constants::PROMPT,
57
            $this->getRawTelnetResponse(),
58
            false
59
        );
60
61
        $response = $command->parseResponse($telnetResponse);
62
        $this->assertFalse($response->isError());
63
        $this->assertEquals($this->getExpectedResponseText(), $response->getResponseText());
64
    }
65
66
    /**
67
     * @return void
68
     */
69
    public function testCommandError()
70
    {
71
        $command = $this->getCommand();
72
73
        $telnetResponse = $this->buildTelnetResponse(
74
            Dynamark3Constants::PROMPT_ERROR,
75
            'ERROR 6' . Dynamark3Constants::LINE_ENDING,
76
            true
77
        );
78
79
        $response = $command->parseResponse($telnetResponse);
80
        $this->assertTrue($response->isError());
81
        $this->assertEquals(6, $response->getErrorCode());
82
    }
83
84
    /**
85
     * @param string $prompt
86
     * @param string $rawResponse
87
     * @param bool $isError
88
     *
89
     * @return TelnetResponseInterface
90
     */
91
    protected function buildTelnetResponse($prompt, $rawResponse, $isError)
92
    {
93
        $promptMatcher = new PromptMatcher();
94
        $bool = $promptMatcher->isMatch($prompt, $rawResponse, Dynamark3Constants::LINE_ENDING);
95
96
        if (!$bool) {
97
            throw new \Exception('prompt did not match');
98
        }
99
100
        $telnetResponse = m::mock(TelnetResponseInterface::class)
101
            ->shouldReceive('isError')
102
            ->andReturn($isError)
103
            ->shouldReceive('getResponseText')
104
            ->andReturn($promptMatcher->getResponseText())
105
            ->shouldReceive('getPromptMatches')
106
            ->andReturn($promptMatcher->getMatches())
107
            ->getMock();
108
109
        return $telnetResponse;
110
    }
111
}
112