Completed
Push — master ( 3fd10f...dc1dc7 )
by Anatoliy
11:29 queued 03:28
created

SurprisingTest::testDeletePidFileWrongParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
5
/**
6
 * Created by PhpStorm.
7
 * User: danchukas
8
 * Date: 2017-06-22 18:10
9
 */
10
11
namespace DanchukAS\DenyMultiplyRunTest;
12
13
use DanchukAS\DenyMultiplyRun\DenyMultiplyRun;
14
use PHPUnit\Framework\TestCase;
15
16
17
/**
18
 * Class SurprisingTest
19
 * поглиблені тести на "всі найбільш можливі ситуації"
20
 * @package DanchukAS\DenyMultiplyRunTest
21
 */
22
class SurprisingTest extends TestCase
23
{
24
25
    private static $noExistFileName;
26
27
    private static $existFileName;
28
29
30
    public function setUp()
31
    {
32
        self::$noExistFileName = sys_get_temp_dir() . '/' . uniqid('vd_', true);
33
        self::$existFileName = tempnam(sys_get_temp_dir(), 'vo_');
34
    }
35
36
    public function tearDown()
37
    {
38
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
39
        @unlink(self::$noExistFileName);
40
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
41
        @unlink(self::$existFileName);
42
    }
43
44
45
    /**
46
     * @expectedException \DanchukAS\DenyMultiplyRun\Exception\ProcessExisted
47
     */
48
    public function testDoubleCall()
49
    {
50
        $file_name = self::$noExistFileName;
51
        DenyMultiplyRun::setPidFile($file_name);
52
        DenyMultiplyRun::setPidFile($file_name);
53
    }/** @noinspection PhpMethodNamingConventionInspection */
54
55
56
    /**
57
     * Delete if no exist pid file.
58
     */
59
    public function testDeleteNoExistedPidFile()
60
    {
61
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
62
        DenyMultiplyRun::deletePidFile(self::$noExistFileName);
63
    }
64
65
66
    /** @noinspection PhpMethodNamingConventionInspection */
67
    public function testDeletePidFileWrongParam()
68
    {
69
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
70
        DenyMultiplyRun::deletePidFile(null);
71
    }
72
73
74
    /** @noinspection PhpMethodNamingConventionInspection */
75
    public function testDeleteNoAccessFile()
76
    {
77
        // existed file without write access for current user.
78
        // for Ubuntu is /etc/hosts.
79
        $file_name = "/etc/hosts";
80
81
        if (!file_exists($file_name)) {
82
            self::markTestSkipped("test only for *nix.");
83
        }
84
85
        if (is_writable($file_name)) {
86
            self::markTestSkipped("test runned under super/admin user. Change user.");
87
        }
88
89
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
90
        DenyMultiplyRun::deletePidFile($file_name);
91
    }/** @noinspection PhpMethodNamingConventionInspection */
92
93
    /**
94
     * @dataProvider notString
95
     * @param mixed $notString
96
     */
97
    public function testWrongTypeParam($notString)
98
    {
99
        $this->expectException("TypeError");
100
        DenyMultiplyRun::setPidFile($notString);
101
    }
102
103
    /**
104
     * @dataProvider wrongParam
105
     * @param string $no_valid_file_name
106
     */
107
    public function testWrongParam(string $no_valid_file_name)
108
    {
109
        $this->expectException("Exception");
110
        DenyMultiplyRun::setPidFile($no_valid_file_name);
111
    }
112
113
114
    /**
115
     * @return array
116
     */
117
    public function notString()
118
    {
119
        $right_resource = fopen(__FILE__, "r");
120
        fclose($right_resource);
121
        $fail_resource = $right_resource;
122
123
        return [
124
            [null]
125
            , [false]
126
            , [0]
127
            , [[]]
128
            , [function () {
129
            }]
130
            , [new \Exception]
131
            , [$fail_resource]
132
        ];
133
    }
134
135
136
    /**
137
     * @return array
138
     */
139
    public function wrongParam()
140
    {
141
        return [[""], ["."], ["/"], ['//']];
142
    }/** @noinspection PhpMethodNamingConventionInspection */
143
144
145
    /**
146
     * @dataProvider notString
147
     * @param mixed $badResource from dataProvider
148
     */
149
    public function testLockedFileBeforeClose($badResource)
150
    {
151
        $method = new \ReflectionMethod("DanchukAS\DenyMultiplyRun\DenyMultiplyRun", "closePidFile");
152
153
        $method->setAccessible(true);
154
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\CloseFileFail");
155
        $method->invoke(null, $badResource);
156
        $method->setAccessible(false);
157
    }
158
}
159