1<?php 2/* 3 * This file is part of the PHPUnit_MockObject package. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11use SebastianBergmann\Exporter\Exporter; 12 13/** 14 * Stubs a method by raising a user-defined exception. 15 * 16 * @since Class available since Release 1.0.0 17 */ 18class PHPUnit_Framework_MockObject_Stub_Exception implements PHPUnit_Framework_MockObject_Stub 19{ 20 protected $exception; 21 22 public function __construct($exception) 23 { 24 // TODO Replace check with type declaration when support for PHP 5 is dropped 25 if (!$exception instanceof Throwable && !$exception instanceof Exception) { 26 throw new PHPUnit_Framework_MockObject_RuntimeException( 27 'Exception must be an instance of Throwable (PHP 7) or Exception (PHP 5)' 28 ); 29 } 30 31 $this->exception = $exception; 32 } 33 34 public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation) 35 { 36 throw $this->exception; 37 } 38 39 public function toString() 40 { 41 $exporter = new Exporter; 42 43 return sprintf( 44 'raise user-specified exception %s', 45 $exporter->export($this->exception) 46 ); 47 } 48} 49