PHPUnit4And6Compat.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. */
  20. /**
  21. * @since 1.31
  22. */
  23. trait PHPUnit4And6Compat {
  24. /**
  25. * @see PHPUnit_Framework_TestCase::setExpectedException
  26. *
  27. * This function was renamed to expectException() in PHPUnit 6, so this
  28. * is a temporary backwards-compatibility layer while we transition.
  29. */
  30. public function setExpectedException( $name, $message = '', $code = null ) {
  31. if ( is_callable( [ $this, 'expectException' ] ) ) {
  32. if ( $name !== null ) {
  33. $this->expectException( $name );
  34. }
  35. if ( $message !== '' ) {
  36. $this->expectExceptionMessage( $message );
  37. }
  38. if ( $code !== null ) {
  39. $this->expectExceptionCode( $code );
  40. }
  41. } else {
  42. parent::setExpectedException( $name, $message, $code );
  43. }
  44. }
  45. /**
  46. * @see PHPUnit_Framework_TestCase::getMock
  47. *
  48. * @return PHPUnit_Framework_MockObject_MockObject
  49. */
  50. public function getMock( $originalClassName, $methods = [], array $arguments = [],
  51. $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
  52. $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
  53. $proxyTarget = null
  54. ) {
  55. if ( is_callable( 'parent::getMock' ) ) {
  56. return parent::getMock(
  57. $originalClassName, $methods, $arguments, $mockClassName,
  58. $callOriginalConstructor, $callOriginalClone, $callAutoload,
  59. $cloneArguments, $callOriginalMethods, $proxyTarget
  60. );
  61. } else {
  62. $builder = $this->getMockBuilder( $originalClassName )
  63. ->setMethods( $methods )
  64. ->setConstructorArgs( $arguments )
  65. ->setMockClassName( $mockClassName )
  66. ->setProxyTarget( $proxyTarget );
  67. if ( $callOriginalConstructor ) {
  68. $builder->enableOriginalConstructor();
  69. } else {
  70. $builder->disableOriginalConstructor();
  71. }
  72. if ( $callOriginalClone ) {
  73. $builder->enableOriginalClone();
  74. } else {
  75. $builder->disableOriginalClone();
  76. }
  77. if ( $callAutoload ) {
  78. $builder->enableAutoload();
  79. } else {
  80. $builder->disableAutoload();
  81. }
  82. if ( $cloneArguments ) {
  83. $builder->enableArgumentCloning();
  84. } else {
  85. $builder->disableArgumentCloning();
  86. }
  87. if ( $callOriginalMethods ) {
  88. $builder->enableProxyingToOriginalMethods();
  89. } else {
  90. $builder->disableProxyingToOriginalMethods();
  91. }
  92. return $builder->getMock();
  93. }
  94. }
  95. /**
  96. * Return a test double for the specified class. This
  97. * is a forward port of the createMock function that
  98. * was introduced in PHPUnit 5.4.
  99. *
  100. * @param string $originalClassName
  101. * @return PHPUnit_Framework_MockObject_MockObject
  102. * @throws Exception
  103. */
  104. public function createMock( $originalClassName ) {
  105. if ( is_callable( 'parent::createMock' ) ) {
  106. return parent::createMock( $originalClassName );
  107. }
  108. // Compat for PHPUnit <= 5.4
  109. return $this->getMockBuilder( $originalClassName )
  110. ->disableOriginalConstructor()
  111. ->disableOriginalClone()
  112. ->disableArgumentCloning()
  113. // New in phpunit-mock-objects 3.2 (phpunit 5.4.0)
  114. // ->disallowMockingUnknownTypes()
  115. ->getMock();
  116. }
  117. }