Completed
Pull Request — master (#20)
by Anatoliy
18:45 queued 08:46
created

ExistRightPidFileTest::testExistedPid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
    public function testNoExistedPid()
57
    {
58
59
        $no_exist_pid = 1;
60
        while (++$no_exist_pid < PHP_INT_MAX) {
61
            if (false === posix_kill($no_exist_pid, 0)
62
                && 3 === posix_get_last_error()
63
            ) {
64
                break;
65
            }
66
        }
67
        file_put_contents(self::$existFileName, $no_exist_pid);
68
69
        self::waitError();
70
        DenyMultiplyRun::setPidFile(self::$existFileName);
71
        $wait_error = "[" . E_USER_NOTICE . "] pid-file exist"
72
            . ", but process with contained ID(%i) in it is not exist."
73
            . " pid-file updated with pid this process: %i";
74
        self::catchError($wait_error);
75
    }
76
77
    /**
78
     * @dataProvider setPidFileParam
79
     */
80
    public function testSetPidFile(\Generator $filename, string $exception)
81
    {
82
        $this->expectException($exception);
83
        DenyMultiplyRun::setPidFile($filename->current());
84
    }
85
}
86