PolyfillTestCaseTrait.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\PhpUnit\Legacy;
  11. use PHPUnit\Framework\Error\Error;
  12. use PHPUnit\Framework\Error\Notice;
  13. use PHPUnit\Framework\Error\Warning;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use PHPUnit\Framework\TestCase;
  16. /**
  17. * This trait is @internal.
  18. */
  19. trait PolyfillTestCaseTrait
  20. {
  21. /**
  22. * @param string|string[] $originalClassName
  23. *
  24. * @return MockObject
  25. */
  26. protected function createMock($originalClassName)
  27. {
  28. $mock = $this->getMockBuilder($originalClassName)
  29. ->disableOriginalConstructor()
  30. ->disableOriginalClone()
  31. ->disableArgumentCloning();
  32. if (method_exists($mock, 'disallowMockingUnknownTypes')) {
  33. $mock = $mock->disallowMockingUnknownTypes();
  34. }
  35. return $mock->getMock();
  36. }
  37. /**
  38. * @param string|string[] $originalClassName
  39. * @param string[] $methods
  40. *
  41. * @return MockObject
  42. */
  43. protected function createPartialMock($originalClassName, array $methods)
  44. {
  45. $mock = $this->getMockBuilder($originalClassName)
  46. ->disableOriginalConstructor()
  47. ->disableOriginalClone()
  48. ->disableArgumentCloning()
  49. ->setMethods(empty($methods) ? null : $methods);
  50. if (method_exists($mock, 'disallowMockingUnknownTypes')) {
  51. $mock = $mock->disallowMockingUnknownTypes();
  52. }
  53. return $mock->getMock();
  54. }
  55. /**
  56. * @param string $exception
  57. *
  58. * @return void
  59. */
  60. public function expectException($exception)
  61. {
  62. $this->doExpectException($exception);
  63. }
  64. /**
  65. * @param int|string $code
  66. *
  67. * @return void
  68. */
  69. public function expectExceptionCode($code)
  70. {
  71. $property = new \ReflectionProperty(TestCase::class, 'expectedExceptionCode');
  72. $property->setAccessible(true);
  73. $property->setValue($this, $code);
  74. }
  75. /**
  76. * @param string $message
  77. *
  78. * @return void
  79. */
  80. public function expectExceptionMessage($message)
  81. {
  82. $property = new \ReflectionProperty(TestCase::class, 'expectedExceptionMessage');
  83. $property->setAccessible(true);
  84. $property->setValue($this, $message);
  85. }
  86. /**
  87. * @param string $messageRegExp
  88. *
  89. * @return void
  90. */
  91. public function expectExceptionMessageMatches($messageRegExp)
  92. {
  93. $this->expectExceptionMessageRegExp($messageRegExp);
  94. }
  95. /**
  96. * @param string $messageRegExp
  97. *
  98. * @return void
  99. */
  100. public function expectExceptionMessageRegExp($messageRegExp)
  101. {
  102. $property = new \ReflectionProperty(TestCase::class, 'expectedExceptionMessageRegExp');
  103. $property->setAccessible(true);
  104. $property->setValue($this, $messageRegExp);
  105. }
  106. /**
  107. * @return void
  108. */
  109. public function expectNotice()
  110. {
  111. $this->doExpectException(Notice::class);
  112. }
  113. /**
  114. * @param string $message
  115. *
  116. * @return void
  117. */
  118. public function expectNoticeMessage($message)
  119. {
  120. $this->expectExceptionMessage($message);
  121. }
  122. /**
  123. * @param string $regularExpression
  124. *
  125. * @return void
  126. */
  127. public function expectNoticeMessageMatches($regularExpression)
  128. {
  129. $this->expectExceptionMessageMatches($regularExpression);
  130. }
  131. /**
  132. * @return void
  133. */
  134. public function expectWarning()
  135. {
  136. $this->doExpectException(Warning::class);
  137. }
  138. /**
  139. * @param string $message
  140. *
  141. * @return void
  142. */
  143. public function expectWarningMessage($message)
  144. {
  145. $this->expectExceptionMessage($message);
  146. }
  147. /**
  148. * @param string $regularExpression
  149. *
  150. * @return void
  151. */
  152. public function expectWarningMessageMatches($regularExpression)
  153. {
  154. $this->expectExceptionMessageMatches($regularExpression);
  155. }
  156. /**
  157. * @return void
  158. */
  159. public function expectError()
  160. {
  161. $this->doExpectException(Error::class);
  162. }
  163. /**
  164. * @param string $message
  165. *
  166. * @return void
  167. */
  168. public function expectErrorMessage($message)
  169. {
  170. $this->expectExceptionMessage($message);
  171. }
  172. /**
  173. * @param string $regularExpression
  174. *
  175. * @return void
  176. */
  177. public function expectErrorMessageMatches($regularExpression)
  178. {
  179. $this->expectExceptionMessageMatches($regularExpression);
  180. }
  181. private function doExpectException($exception)
  182. {
  183. $property = new \ReflectionProperty(TestCase::class, 'expectedException');
  184. $property->setAccessible(true);
  185. $property->setValue($this, $exception);
  186. }
  187. }