Test Failed
Push — master ( f56a84...3fd10f )
by Anatoliy
01:08
created

SurprisingTest::wrongParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
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
    }
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
    public function testDeletePidFileWrongParam()
67
    {
68
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
69
        DenyMultiplyRun::deletePidFile(null);
70
    }
71
72
73
    public function testDeleteNoAccessFile()
74
    {
75
        // existed file without write access for current user.
76
        // for Ubuntu is /etc/hosts.
77
        $file_name = "/etc/hosts";
78
79
        if (!file_exists($file_name)) {
80
            self::markTestSkipped("test only for *nix.");
81
        }
82
83
        if (is_writable($file_name)) {
84
            self::markTestSkipped("test runned under super/admin user. Change user.");
85
        }
86
87
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
88
        DenyMultiplyRun::deletePidFile($file_name);
89
    }
90
91
    /**
92
     * @dataProvider notString
93
     * @param mixed $notString
94
     */
95
    public function testWrongTypeParam($notString)
96
    {
97
        $this->expectException("TypeError");
98
        DenyMultiplyRun::setPidFile($notString);
99
    }
100
101
    /**
102
     * @dataProvider wrongParam
103
     * @param string $no_valid_file_name
104
     */
105
    public function testWrongParam(string $no_valid_file_name)
106
    {
107
        $this->expectException("Exception");
108
        DenyMultiplyRun::setPidFile($no_valid_file_name);
109
    }
110
111
112
    /**
113
     * @return array
114
     */
115
    public function notString()
116
    {
117
        $right_resource = fopen(__FILE__, "r");
118
        fclose($right_resource);
119
        $fail_resource = $right_resource;
120
121
        return [
122
            [null]
123
            , [false]
124
            , [0]
125
            , [[]]
126
            , [function () {
127
            }]
128
            , [new \Exception]
129
            , [$fail_resource]
130
        ];
131
    }
132
133
134
    /**
135
     * @return array
136
     */
137
    public function wrongParam()
138
    {
139
        return [[""], ["."], ["/"], ['//']];
140
    }
141
142
143
    /**
144
     * @dataProvider notString
145
     */
146
    public function testLockedFileBeforeClose($badResource)
147
    {
148
        $method = new \ReflectionMethod("DanchukAS\DenyMultiplyRun\DenyMultiplyRun", "closePidFile");
149
150
        $method->setAccessible(true);
151
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\CloseFileFail");
152
        $method->invoke(null, $badResource);
153
        $method->setAccessible(false);
154
    }
155
}
156