2016-01-25 22:37:54 +00:00
|
|
|
<?php
|
2017-03-08 14:07:29 +00:00
|
|
|
|
2016-01-25 22:37:54 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2016-01-25 22:37:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SilencerTest
|
|
|
|
*
|
|
|
|
* @author Niels Keurentjes <niels.keurentjes@omines.com>
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class SilencerTest extends TestCase
|
2016-01-25 22:37:54 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test succeeds when no warnings are emitted externally, and original level is restored.
|
|
|
|
*/
|
|
|
|
public function testSilencer()
|
|
|
|
{
|
|
|
|
$before = error_reporting();
|
|
|
|
|
|
|
|
// Check warnings are suppressed correctly
|
|
|
|
Silencer::suppress();
|
2016-01-25 22:51:11 +00:00
|
|
|
@trigger_error('Test', E_USER_WARNING);
|
2016-01-25 22:37:54 +00:00
|
|
|
Silencer::restore();
|
|
|
|
|
|
|
|
// Check all parameters and return values are passed correctly in a silenced call.
|
2016-01-26 13:07:05 +00:00
|
|
|
$result = Silencer::call(function ($a, $b, $c) {
|
2016-01-25 22:51:11 +00:00
|
|
|
@trigger_error('Test', E_USER_WARNING);
|
2016-01-26 13:07:05 +00:00
|
|
|
|
2016-01-25 22:37:54 +00:00
|
|
|
return $a * $b * $c;
|
|
|
|
}, 2, 3, 4);
|
|
|
|
$this->assertEquals(24, $result);
|
|
|
|
|
|
|
|
// Check the error reporting setting was restored correctly
|
|
|
|
$this->assertEquals($before, error_reporting());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether exception from silent callbacks are correctly forwarded.
|
|
|
|
*/
|
|
|
|
public function testSilencedException()
|
|
|
|
{
|
|
|
|
$verification = microtime();
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('RuntimeException');
|
|
|
|
self::expectExceptionMessage($verification);
|
2016-01-26 13:07:05 +00:00
|
|
|
Silencer::call(function () use ($verification) {
|
2016-01-25 22:37:54 +00:00
|
|
|
throw new \RuntimeException($verification);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|