StatusGuesserTest::createResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 2
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Renderer;
17
18
use Hogosha\Monitor\Guesser\StatusGuesser;
19
use Hogosha\Monitor\Model\Result;
20
use Hogosha\Monitor\Model\UrlInfo;
21
use Hogosha\Monitor\Validator\Validator;
22
23
/**
24
 * @author Guillaume Cavana <[email protected]>
25
 */
26
class StatusGuesserTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * testIsFailed.
30
     */
31
    public function testIsFailed()
32
    {
33
        $statusGuesser = new StatusGuesser();
34
        $this->assertTrue($statusGuesser->isFailed($this->createResult()));
35
    }
36
37
    /**
38
     * testIsOk.
39
     */
40
    public function testIsOk()
41
    {
42
        $statusGuesser = new StatusGuesser();
43
        $this->assertFalse($statusGuesser->isOk($this->createResult()));
44
    }
45
46
    /**
47
     * createResult.
48
     *
49
     * @return Result
50
     */
51
    public function createResult()
52
    {
53
        $result = new Result($this->createUrlInfo(), 200, 0.42, null, false, null);
54
55
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (Hogosha\Monitor\Model\Result) is incompatible with the return type of the parent method PHPUnit_Framework_TestCase::createResult of type PHPUnit_Framework_TestResult.

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...
56
    }
57
58
    private function createUrlInfo()
59
    {
60
        return new UrlInfo(
61
            'Example',
62
            'http://example.com',
63
            'GET',
64
            [],
65
            1,
66
            404,
67
            (new Validator()),
68
            null,
69
            null
70
        );
71
    }
72
}
73