Passed
Push — master ( d15464...d64dd2 )
by Alexey
05:25
created

NotificationsTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class NotificationsTest extends PHPUnit_Framework_TestCase {
4
5
    public function setUp() {
6
        \App::$cur->Modules->install('Users');
7
        \App::$cur->Modules->install('Notifications');
8
    }
9
10
    /**
11
     * @covers Model
12
     * @covers Notifications::getChanel
13
     */
14
    public function testGetChanel() {
15
        \Notifications\Chanel::deleteList();
16
        $chanel = App::$cur->Notifications->getChanel('test');
17
        $this->verifyChanel($chanel);
18
    }
19
20
    /**
21
     * @covers Model
22
     * @covers Notifications::getCurDevice
23
     * @covers Notifications::setDeviceKey
24
     * @covers Notifications::getDeviceKey
25
     */
26
    public function testGetCurDevice() {
2 ignored issues
show
Coding Style introduced by
testGetCurDevice uses the super-global variable $_COOKIE 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...
Coding Style introduced by
testGetCurDevice 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...
27
        \Notifications\Subscriber\Device::deleteList();
28
        if (isset($_COOKIE['notification-device'])) {
29
            unset($_COOKIE['notification-device']);
30
        }
31
        if (isset($_SESSION['notification-device'])) {
32
            unset($_SESSION['notification-device']);
33
        }
34
35
        $device = App::$cur->Notifications->getCurDevice();
36
        $this->assertFalse($device);
37
        $device = App::$cur->Notifications->getCurDevice(true);
38
        $this->verifyDevice($device);
39
        $device = App::$cur->Notifications->getCurDevice();
40
        $this->verifyDevice($device);
41
        $device->delete();
42
        $device = App::$cur->Notifications->getCurDevice();
43
        $this->assertFalse($device);
44
    }
45
46
    /**
47
     * @covers Model
48
     * @covers Notifications::getCurSubscriber
49
     * @covers Notifications::getCurDevice
50
     * @covers Notifications::setDeviceKey
51
     * @covers Notifications::getDeviceKey
52
     */
53
    public function testGetCurSubscriber() {
2 ignored issues
show
Coding Style introduced by
testGetCurSubscriber uses the super-global variable $_COOKIE 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...
Coding Style introduced by
testGetCurSubscriber 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...
54
        \Notifications\Subscriber::deleteList();
55
        \Notifications\Subscriber\Device::deleteList();
56
        if (isset($_COOKIE['notification-device'])) {
57
            unset($_COOKIE['notification-device']);
58
        }
59
        if (isset($_SESSION['notification-device'])) {
60
            unset($_SESSION['notification-device']);
61
        }
62
63
        $subscriber = App::$cur->Notifications->getCurSubscriber();
64
        $this->assertFalse($subscriber);
65
        $subscriber = App::$cur->Notifications->getCurSubscriber(true);
66
        $this->verifySubscriber($subscriber);
67
        $subscriber = App::$cur->Notifications->getCurSubscriber();
68
        $this->verifySubscriber($subscriber);
69
    }
70
71
    /**
72
     * @covers Model
73
     * @covers Server\Result
74
     * @covers Notifications::subscribe
75
     * @covers Notifications::getCurSubscriber
76
     * @covers Notifications::getCurDevice
77
     * @covers Notifications::setDeviceKey
78
     * @covers Notifications::getDeviceKey
79
     */
80
    public function testSubscribe() {
2 ignored issues
show
Coding Style introduced by
testSubscribe uses the super-global variable $_COOKIE 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...
Coding Style introduced by
testSubscribe 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...
81
        \Notifications\Subscriber::deleteList();
82
        \Notifications\Subscriber\Device::deleteList();
83
        \Notifications\Chanel::deleteList();
84
        if (isset($_COOKIE['notification-device'])) {
85
            unset($_COOKIE['notification-device']);
86
        }
87
        if (isset($_SESSION['notification-device'])) {
88
            unset($_SESSION['notification-device']);
89
        }
90
        \Inji::$inst->exitOnStop = false;
91
92
        ob_start();
93
        $result = App::$cur->Notifications->subscribe('test');
94
        $content = ob_get_contents();
95
        ob_end_clean();
96
        $content = json_decode($content, true);
97
        $this->assertEquals('Вы были подписаны на уведомления', $content['successMsg']);
98
        $this->assertTrue($result);
99
100
        ob_start();
101
        $result = App::$cur->Notifications->subscribe('test');
102
        $content = ob_get_contents();
103
        ob_end_clean();
104
        $content = json_decode($content, true);
105
        $this->assertEquals('Вы уже подписаны', $content['successMsg']);
106
        $this->assertTrue($result);
107
    }
108
109 View Code Duplication
    private function verifyChanel($chanel) {
110
        $this->assertTrue(is_object($chanel));
111
        $this->assertEquals('Notifications\Chanel', get_class($chanel));
112
        $this->assertEquals('test', $chanel->name);
113
        $this->assertTrue((int) $chanel->id > 0);
114
    }
115
116
    private function verifySubscriber($subscriber) {
117
        $this->assertTrue(is_object($subscriber));
118
        $this->assertEquals('Notifications\Subscriber', get_class($subscriber));
119
        //$this->assertEquals($subscriber->key, App::$cur->Notifications->getDevicekey());
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
        $this->assertTrue((int) $subscriber->id > 0);
121
    }
122
123 View Code Duplication
    private function verifyDevice($device) {
124
        $this->assertTrue(is_object($device));
125
        $this->assertEquals('Notifications\Subscriber\Device', get_class($device));
126
        $this->assertEquals($device->key, App::$cur->Notifications->getDevicekey());
127
        $this->assertTrue((int) $device->id > 0);
128
    }
129
130
    private function purgeData() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
131
        
132
    }
133
}