Dynamark3ClientTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testSend() 0 37 1
A testFactory() 0 4 1
A testConnect() 0 19 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\Unit;
16
17
use \Graze\TelnetClient\TelnetResponseInterface;
18
use \Graze\Dynamark3Client\Command\CommandInterface;
19
use \Graze\TelnetClient\TelnetClientInterface;
20
use \Graze\Dynamark3Client\CommandResolver;
21
use \Graze\Dynamark3Client\Dynamark3Client;
22
use \Graze\Dynamark3Client\Dynamark3ResponseInterface;
23
use \Graze\Dynamark3Client\Dynamark3Constants;
24
use \Mockery as m;
25
26
class Dynamark3ClientTest extends \PHPUnit_Framework_TestCase
27
{
28
    public function testSend()
29
    {
30
        $telnetResponse = m::mock(TelnetResponseInterface::class);
31
        $telnet = m::mock(TelnetClientInterface::class)
32
            ->shouldReceive('execute')
33
            ->with('COMMAND ARGS', null)
34
            ->andReturn($telnetResponse)
35
            ->once()
36
            ->getMock();
37
38
        $dynamark3Response = m::mock(Dynamark3ResponseInterface::class);
39
        $command = m::mock(CommandInterface::class)
40
            ->shouldReceive('getCommandText')
41
            ->andReturn('COMMAND')
42
            ->once()
43
            ->shouldReceive('getArgumentText')
44
            ->andReturn(' ARGS')
45
            ->once()
46
            ->shouldReceive('getPrompt')
47
            ->andReturn(null)
48
            ->once()
49
            ->shouldReceive('parseResponse')
50
            ->with($telnetResponse)
51
            ->andReturn($dynamark3Response)
52
            ->once()
53
            ->getMock();
54
        $commandResolver = m::mock(CommandResolver::class)
55
            ->shouldReceive('resolve')
56
            ->with('command')
57
            ->andReturn($command)
58
            ->once()
59
            ->getMock();
60
61
        $client = new Dynamark3Client($telnet, $commandResolver);
62
        $resp = $client->command();
0 ignored issues
show
Documentation Bug introduced by
The method command does not exist on object<Graze\Dynamark3Client\Dynamark3Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
        $this->assertSame($dynamark3Response, $resp);
64
    }
65
66
    public function testFactory()
67
    {
68
        $this->assertInstanceOf(Dynamark3Client::class, Dynamark3Client::factory());
69
    }
70
71
    public function testConnect()
72
    {
73
        $dsn = '127.0.0.1:23';
74
        $telnet = m::mock(TelnetClientInterface::class)
75
            ->shouldReceive('connect')
76
            ->with(
77
                $dsn,
78
                Dynamark3Constants::PROMPT,
79
                Dynamark3Constants::PROMPT_ERROR,
80
                Dynamark3Constants::LINE_ENDING
81
            )
82
            ->once()
83
            ->getMock();
84
85
        $commandResolver = m::mock(CommandResolver::class);
86
87
        $client = new Dynamark3Client($telnet, $commandResolver);
88
        $client->connect($dsn);
89
    }
90
}
91