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

ExistRightPidFileTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
dl 0
loc 75
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A catchError() 0 4 1
A testExistedPid() 0 7 1
A waitError() 0 9 1
A testLockedFile() 0 7 1
A testNoExistedPid() 0 19 4
A testEmptyFile() 0 7 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: danchukas
6
 * Date: 2017-07-15 01:03
7
 */
8
9
namespace DanchukAS\DenyMultiplyRunTest;
10
11
use DanchukAS\DenyMultiplyRun\DenyMultiplyRun;
12
use DanchukAS\DenyMultiplyRun\PidFileTestCase;
13
14
/** @noinspection PhpClassNamingConventionInspection */
15
16
/**
17
 * Class ExistRightPidFileTest
18
 * Тести з вірним підфайлом.
19
 * @package DanchukAS\DenyMultiplyRunTest
20
 */
21
class ExistRightPidFileTest extends PidFileTestCase
22
{
23
24
    private static $lastError;
25
26
    public function testEmptyFile()
27
    {
28
        self::waitError();
29
        DenyMultiplyRun::setPidFile(self::$existFileName);
30
        $wait_error = "[" . E_USER_NOTICE . "] pid-file exist, but file empty."
31
            . " pid-file updated with pid this process: %i";
32
        self::catchError($wait_error);
33
    }
34
35
    private static function waitError()
36
    {
37
38
        /** @noinspection PhpUnusedParameterInspection */
39
        set_error_handler(function (int $messageType, string $messageText) {
40
            self::$lastError = "[$messageType] " . $messageText;
41
        });
42
43
        self::$lastError = null;
44
    }
45
46
    /**
47
     * перевіряє чи помилка відбулась, і саме та яка очікувалась.
48
     * @param string $message
49
     */
50
    private static function catchError(string $message)
51
    {
52
        restore_error_handler();
53
        self::assertStringMatchesFormat("$message", self::$lastError);
54
    }
55
56
    /**
57
     * @expectedException \DanchukAS\DenyMultiplyRun\Exception\ProcessExisted
58
     */
59
    public function testExistedPid()
60
    {
61
62
        $file_name = self::$existFileName;
63
        file_put_contents($file_name, getmypid());
64
65
        DenyMultiplyRun::setPidFile($file_name);
66
    }
67
68
    public function testNoExistedPid()
69
    {
70
71
        $no_exist_pid = 1;
72
        while (++$no_exist_pid < PHP_INT_MAX) {
73
            if (false === posix_kill($no_exist_pid, 0)
74
                && 3 === posix_get_last_error()
75
            ) {
76
                break;
77
            }
78
        }
79
        file_put_contents(self::$existFileName, $no_exist_pid);
80
81
        self::waitError();
82
        DenyMultiplyRun::setPidFile(self::$existFileName);
83
        $wait_error = "[" . E_USER_NOTICE . "] pid-file exist"
84
            . ", but process with contained ID(%i) in it is not exist."
85
            . " pid-file updated with pid this process: %i";
86
        self::catchError($wait_error);
87
    }
88
89
    public function testLockedFile()
90
    {
91
        $file_resource = fopen(self::$existFileName, "r+");
92
        flock($file_resource, LOCK_EX);
93
94
        $this->expectException("DanchukAS\DenyMultiplyRun\Exception\LockFileFail");
95
        DenyMultiplyRun::setPidFile(self::$existFileName);
96
    }
97
}
98