AbstractCommandTestCase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 102
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
getCommand() 0 1 ?
getRawTelnetResponse() 0 1 ?
getExpectedResponseText() 0 1 ?
getExpectedArgumentText() 0 1 ?
A testParseResponseSuccess() 0 16 2
A testParseResponseError() 0 14 1
A testGetArgumentText() 0 5 1
A buildTelnetResponse() 0 20 2
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
     * The expected result if $command->getArgumentText(['a', '1']) was called.
48
     *
49
     * @return string
50
     */
51
    abstract protected function getExpectedArgumentText();
52
53
    /**
54
     * @return void
55
     */
56
    public function testParseResponseSuccess()
57
    {
58
        $command = $this->getCommand();
59
60
        $this->assertInstanceOf(CommandInterface::class, $command);
61
62
        $telnetResponse = $this->buildTelnetResponse(
63
            $command->getPrompt() ?: Dynamark3Constants::PROMPT,
64
            $this->getRawTelnetResponse(),
65
            false
66
        );
67
68
        $response = $command->parseResponse($telnetResponse);
69
        $this->assertFalse($response->isError());
70
        $this->assertEquals($this->getExpectedResponseText(), $response->getResponseText());
71
    }
72
73
    /**
74
     * @return void
75
     */
76
    public function testParseResponseError()
77
    {
78
        $command = $this->getCommand();
79
80
        $telnetResponse = $this->buildTelnetResponse(
81
            Dynamark3Constants::PROMPT_ERROR,
82
            'ERROR 6' . Dynamark3Constants::LINE_ENDING,
83
            true
84
        );
85
86
        $response = $command->parseResponse($telnetResponse);
87
        $this->assertTrue($response->isError());
88
        $this->assertEquals(6, $response->getErrorCode());
89
    }
90
91
    public function testGetArgumentText()
92
    {
93
        $command = $this->getCommand();
94
        $this->assertEquals($this->getExpectedArgumentText(), $command->getArgumentText(['a', '1']));
95
    }
96
97
    /**
98
     * @param string $prompt
99
     * @param string $rawResponse
100
     * @param bool $isError
101
     *
102
     * @return TelnetResponseInterface
103
     */
104
    protected function buildTelnetResponse($prompt, $rawResponse, $isError)
105
    {
106
        $promptMatcher = new PromptMatcher();
107
        $bool = $promptMatcher->isMatch($prompt, $rawResponse, Dynamark3Constants::LINE_ENDING);
108
109
        if (!$bool) {
110
            throw new \Exception('prompt did not match');
111
        }
112
113
        $telnetResponse = m::mock(TelnetResponseInterface::class)
114
            ->shouldReceive('isError')
115
            ->andReturn($isError)
116
            ->shouldReceive('getResponseText')
117
            ->andReturn($promptMatcher->getResponseText())
118
            ->shouldReceive('getPromptMatches')
119
            ->andReturn($promptMatcher->getMatches())
120
            ->getMock();
121
122
        return $telnetResponse;
123
    }
124
}
125