Passed
Push — master ( d64dd2...6e4bb1 )
by Alexey
04:40
created

NotificationsTest::purgeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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->assertTrue((int) $subscriber->id > 0);
120
    }
121
122 View Code Duplication
    private function verifyDevice($device) {
123
        $this->assertTrue(is_object($device));
124
        $this->assertEquals('Notifications\Subscriber\Device', get_class($device));
125
        $this->assertEquals($device->key, App::$cur->Notifications->getDevicekey());
126
        $this->assertTrue((int) $device->id > 0);
127
    }
128
}