File: SilencerTest.php

package info (click to toggle)
composer 2.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,528 kB
  • sloc: php: 83,030; makefile: 59; xml: 39
file content (61 lines) | stat: -rw-r--r-- 1,730 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php declare(strict_types=1);

/*
 * This file is part of Composer.
 *
 * (c) Nils Adermann <naderman@naderman.de>
 *     Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Test\Util;

use Composer\Util\Silencer;
use Composer\Test\TestCase;

/**
 * SilencerTest
 *
 * @author Niels Keurentjes <niels.keurentjes@omines.com>
 */
class SilencerTest extends TestCase
{
    /**
     * Test succeeds when no warnings are emitted externally, and original level is restored.
     */
    public function testSilencer(): void
    {
        $before = error_reporting();

        // Check warnings are suppressed correctly
        Silencer::suppress();
        @trigger_error('Test', E_USER_WARNING);
        Silencer::restore();

        // Check all parameters and return values are passed correctly in a silenced call.
        $result = Silencer::call(static function ($a, $b, $c) {
            @trigger_error('Test', E_USER_WARNING);

            return $a * $b * $c;
        }, 2, 3, 4);
        self::assertEquals(24, $result);

        // Check the error reporting setting was restored correctly
        self::assertEquals($before, error_reporting());
    }

    /**
     * Test whether exception from silent callbacks are correctly forwarded.
     */
    public function testSilencedException(): void
    {
        $verification = microtime();
        self::expectException('RuntimeException');
        self::expectExceptionMessage($verification);
        Silencer::call(static function () use ($verification): void {
            throw new \RuntimeException($verification);
        });
    }
}