SilencerTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. use Composer\Test\TestCase;
  14. /**
  15. * SilencerTest
  16. *
  17. * @author Niels Keurentjes <niels.keurentjes@omines.com>
  18. */
  19. class SilencerTest extends TestCase
  20. {
  21. /**
  22. * Test succeeds when no warnings are emitted externally, and original level is restored.
  23. */
  24. public function testSilencer()
  25. {
  26. $before = error_reporting();
  27. // Check warnings are suppressed correctly
  28. Silencer::suppress();
  29. @trigger_error('Test', E_USER_WARNING);
  30. Silencer::restore();
  31. // Check all parameters and return values are passed correctly in a silenced call.
  32. $result = Silencer::call(function ($a, $b, $c) {
  33. @trigger_error('Test', E_USER_WARNING);
  34. return $a * $b * $c;
  35. }, 2, 3, 4);
  36. $this->assertEquals(24, $result);
  37. // Check the error reporting setting was restored correctly
  38. $this->assertEquals($before, error_reporting());
  39. }
  40. /**
  41. * Test whether exception from silent callbacks are correctly forwarded.
  42. */
  43. public function testSilencedException()
  44. {
  45. $verification = microtime();
  46. $this->setExpectedException('\RuntimeException', $verification);
  47. Silencer::call(function () use ($verification) {
  48. throw new \RuntimeException($verification);
  49. });
  50. }
  51. }