Completed
Push — master ( 0df352...724305 )
by John
04:42
created

CommandGenericTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 57
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommand() 0 4 1
A getRawTelnetResponse() 0 4 1
A getExpectedResponseText() 0 4 1
A testCommandSuccess() 0 5 1
A dataProviderCommandSuccess() 0 7 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\Command;
16
17
use Graze\Dynamark3Client\Test\AbstractCommandTestCase;
18
use Graze\Dynamark3Client\Dynamark3Constants;
19
use Graze\Dynamark3Client\Command\CommandGeneric;
20
21
class CommandGenericTest extends AbstractCommandTestCase
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $commandText;
27
28
    /**
29
     * @return CommandGeneric
30
     */
31
    protected function getCommand()
32
    {
33
        return new CommandGeneric($this->commandText);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Graze\Dynama...ic($this->commandText); (Graze\Dynamark3Client\Command\CommandGeneric) is incompatible with the return type declared by the abstract method Graze\Dynamark3Client\Te...andTestCase::getCommand of type Graze\Dynamark3Client\Te...ommand\CommandInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    protected function getRawTelnetResponse()
40
    {
41
        return Dynamark3Constants::PROMPT . Dynamark3Constants::LINE_ENDING;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    protected function getExpectedResponseText()
48
    {
49
        return Dynamark3Constants::PROMPT;
50
    }
51
52
    /**
53
     * @dataProvider dataProviderCommandSuccess
54
     *
55
     * @param string $commandText
56
     * @param string $rawTelnetResponse
0 ignored issues
show
Bug introduced by
There is no parameter named $rawTelnetResponse. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
     * @param string $expectedResponseText
0 ignored issues
show
Bug introduced by
There is no parameter named $expectedResponseText. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
58
     *
59
     * @return void
60
     */
61
    public function testCommandSuccess($commandText)
62
    {
63
        $this->commandText = $commandText;
64
        parent::testCommandSuccess();
65
    }
66
67
    /**
68
     * @return []
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
69
     */
70
    public function dataProviderCommandSuccess()
71
    {
72
        return [
73
            ['MARK STOP'],
74
            ['ANOTHERCOMMAND']
75
        ];
76
    }
77
}
78