FlashMessagesTest::testReset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace KochTest\Session;
4
5
use Koch\Session\FlashMessages;
6
7
class FlashMessagesTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function tearDown()
10
    {
11
        Flashmessages::reset();
12
    }
13
14
    /**
15
     * @covers Koch\Session\FlashMessages::setMessage
16
     */
17 View Code Duplication
    public function testSetMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testSetMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
18
    {
19
        FlashMessages::setMessage('MyMessage', 'error');
20
21
        $this->assertArrayHasKey('error', $_SESSION['user']['flashmessages']);
22
        $this->assertEquals('MyMessage', $_SESSION['user']['flashmessages']['error'][0]);
23
    }
24
25
    /**
26
     * @covers Koch\Session\FlashMessages::setErrorMessage
27
     */
28 View Code Duplication
    public function testSetErrorMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testSetErrorMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
    {
30
        FlashMessages::setErrorMessage('OneErrorMessage');
31
32
        $this->assertArrayHasKey('error', $_SESSION['user']['flashmessages']);
33
        $this->assertEquals('OneErrorMessage', $_SESSION['user']['flashmessages']['error'][0]);
34
    }
35
36
    /**
37
     * @covers Koch\Session\FlashMessages::getMessages
38
     */
39
    public function testGetMessages()
40
    {
41
        FlashMessages::setMessage('NoticeMessage', 'notice');
42
        // get message without type and unset
43
        $this->assertTrue(is_array(FlashMessages::getMessages()));
44
        $this->assertCount(1, FlashMessages::getMessages());
45
46
        // get messages by type and no unset
47
        FlashMessages::setMessage('NoticeMessage', 'notice');
48
        FlashMessages::setMessage('DebugMessage', 'debug');
49
        FlashMessages::setMessage('DebugMessage2', 'debug');
50
        $r = FlashMessages::getMessages('debug');
51
        $this->assertCount(2, $r);
52
        $this->assertEquals('DebugMessage', $r[0]);
53
        $this->assertEquals('DebugMessage2', $r[1]);
54
    }
55
56
    /**
57
     * @covers Koch\Session\FlashMessages::reset
58
     */
59
    public function testReset()
60
    {
61
        FlashMessages::reset();
62
        $this->assertTrue(is_array(FlashMessages::getMessages()));
63
        $this->assertCount(0, FlashMessages::getMessages());
64
    }
65
66
    /**
67
     * @covers Koch\Session\FlashMessages::render
68
     */
69
    public function testRender()
70
    {
71
        FlashMessages::setMessage('ErrorMessage', 'error');
72
        $this->assertEquals(
73
            $this->getFlashMessageRenderContent(),
74
            FlashMessages::render()
75
        );
76
    }
77
78
    public function getFlashMessageRenderContent()
79
    {
80
        return '<link rel="stylesheet" type="text/css" href="http://css/error.css" /><div id="flashmessage" class="flashmessage 0">ErrorMessage</div>';
81
    }
82
}
83