Completed
Push — develope ( 31b18b...6fd8e0 )
by Anatoliy
02:01
created

SurprisingTest::testDeleteNoAccessFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
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 DanchukAS\DenyMultiplyRun\PidFileTestCase;
15
16
/**
17
 * Class SurprisingTest
18
 * поглиблені тести на "всі найбільш можливі ситуації"
19
 * @package DanchukAS\DenyMultiplyRunTest
20
 */
21
class SurprisingTest extends PidFileTestCase
22
{
23
24
    /**
25
     * @expectedException \DanchukAS\DenyMultiplyRun\Exception\ProcessExisted
26
     */
27
    public function testDoubleCall()
28
    {
29
        $file_name = self::$noExistFileName;
30
        DenyMultiplyRun::setPidFile($file_name);
31
        DenyMultiplyRun::setPidFile($file_name);
32
    }/** @noinspection PhpMethodNamingConventionInspection */
33
34
35
    /**
36
     * Delete if no exist pid file.
37
     */
38
    public function testDeleteNoExistedPidFile()
39
    {
40
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
41
        DenyMultiplyRun::deletePidFile(self::$noExistFileName);
42
    }
43
44
45
    /** @noinspection PhpMethodNamingConventionInspection */
46
    public function testDeletePidFileWrongParam()
47
    {
48
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
49
        DenyMultiplyRun::deletePidFile(null);
50
    }
51
52
53
    /** @noinspection PhpMethodNamingConventionInspection */
54
    public function testDeleteNoAccessFile()
55
    {
56
        // existed file without write access for current user.
57
        // for Ubuntu is /etc/hosts.
58
        $file_name = "/etc/hosts";
59
60
        if (!file_exists($file_name)) {
61
            self::markTestSkipped("test only for *nix.");
62
        }
63
64
        if (is_writable($file_name)) {
65
            self::markTestSkipped("test runned under super/admin user. Change user.");
66
        }
67
68
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\DeleteFileFail");
69
        DenyMultiplyRun::deletePidFile($file_name);
70
    }/** @noinspection PhpMethodNamingConventionInspection */
71
72
    /**
73
     * @dataProvider notString
74
     * @param mixed $notString
75
     */
76
    public function testWrongTypeParam($notString)
77
    {
78
        $this->expectException("TypeError");
79
        DenyMultiplyRun::setPidFile($notString);
80
    }
81
82
    /**
83
     * @dataProvider wrongParam
84
     * @param string $no_valid_file_name
85
     */
86
    public function testWrongParam(string $no_valid_file_name)
87
    {
88
        $this->expectException("Exception");
89
        DenyMultiplyRun::setPidFile($no_valid_file_name);
90
    }
91
92
93
    /**
94
     * @return array
95
     */
96
    public function notString()
97
    {
98
        $right_resource = fopen(__FILE__, "r");
99
        fclose($right_resource);
100
        $fail_resource = $right_resource;
101
102
        return [
103
            [null]
104
            , [false]
105
            , [0]
106
            , [[]]
107
            , [function () {
108
            }]
109
            , [new \Exception]
110
            , [$fail_resource]
111
        ];
112
    }
113
114
115
    /**
116
     * @return array
117
     */
118
    public function wrongParam()
119
    {
120
        return [[""], ["."], ["/"], ['//']];
121
    }/** @noinspection PhpMethodNamingConventionInspection */
122
123
124
    /**
125
     * @dataProvider notString
126
     * @param mixed $badResource from dataProvider
127
     */
128
    public function testLockedFileBeforeClose($badResource)
129
    {
130
        $method = new \ReflectionMethod("DanchukAS\DenyMultiplyRun\DenyMultiplyRun", "closePidFile");
131
132
        $method->setAccessible(true);
133
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\CloseFileFail");
134
        $method->invoke(null, $badResource);
135
        $method->setAccessible(false);
136
    }
137
}
138