SilencerTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\Silencer;
  13. /**
  14. * SilencerTest
  15. *
  16. * @author Niels Keurentjes <niels.keurentjes@omines.com>
  17. */
  18. class SilencerTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * Test succeeds when no warnings are emitted externally, and original level is restored.
  22. */
  23. public function testSilencer()
  24. {
  25. $before = error_reporting();
  26. // Check warnings are suppressed correctly
  27. Silencer::suppress();
  28. @trigger_error('Test', E_USER_WARNING);
  29. Silencer::restore();
  30. // Check all parameters and return values are passed correctly in a silenced call.
  31. $result = Silencer::call(function ($a, $b, $c) {
  32. @trigger_error('Test', E_USER_WARNING);
  33. return $a * $b * $c;
  34. }, 2, 3, 4);
  35. $this->assertEquals(24, $result);
  36. // Check the error reporting setting was restored correctly
  37. $this->assertEquals($before, error_reporting());
  38. }
  39. /**
  40. * Test whether exception from silent callbacks are correctly forwarded.
  41. */
  42. public function testSilencedException()
  43. {
  44. $verification = microtime();
  45. $this->setExpectedException('\RuntimeException', $verification);
  46. Silencer::call(function () use ($verification) {
  47. throw new \RuntimeException($verification);
  48. });
  49. }
  50. }