3 * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
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.
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.
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.
24 trait PHPUnit4And6Compat
{
26 * @see PHPUnit_Framework_TestCase::setExpectedException
28 * This function was renamed to expectException() in PHPUnit 6, so this
29 * is a temporary backwards-compatibility layer while we transition.
31 public function setExpectedException( $name, $message = '', $code = null ) {
32 if ( is_callable( 'parent::expectException' ) ) {
33 if ( $name !== null ) {
34 parent
::expectException( $name );
36 if ( $message !== '' ) {
37 $this->expectExceptionMessage( $message );
39 if ( $code !== null ) {
40 $this->expectExceptionCode( $code );
43 parent
::setExpectedException( $name, $message, $code );
48 * Future-compatible layer for PHPUnit 4's setExpectedException.
50 public function expectException( $exception ) {
51 if ( is_callable( 'parent::expectException' ) ) {
52 parent
::expectException( $exception );
56 parent
::setExpectedException( $exception );
60 * @see PHPUnit_Framework_TestCase::getMock
62 * @return PHPUnit_Framework_MockObject_MockObject
64 public function getMock( $originalClassName, $methods = [], array $arguments = [],
65 $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
66 $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
69 if ( is_callable( 'parent::getMock' ) ) {
70 return parent
::getMock(
71 $originalClassName, $methods, $arguments, $mockClassName,
72 $callOriginalConstructor, $callOriginalClone, $callAutoload,
73 $cloneArguments, $callOriginalMethods, $proxyTarget
76 $builder = $this->getMockBuilder( $originalClassName )
77 ->setMethods( $methods )
78 ->setConstructorArgs( $arguments )
79 ->setMockClassName( $mockClassName )
80 ->setProxyTarget( $proxyTarget );
81 if ( $callOriginalConstructor ) {
82 $builder->enableOriginalConstructor();
84 $builder->disableOriginalConstructor();
86 if ( $callOriginalClone ) {
87 $builder->enableOriginalClone();
89 $builder->disableOriginalClone();
91 if ( $callAutoload ) {
92 $builder->enableAutoload();
94 $builder->disableAutoload();
96 if ( $cloneArguments ) {
97 $builder->enableArgumentCloning();
99 $builder->disableArgumentCloning();
101 if ( $callOriginalMethods ) {
102 $builder->enableProxyingToOriginalMethods();
104 $builder->disableProxyingToOriginalMethods();
107 return $builder->getMock();
112 * Return a test double for the specified class. This
113 * is a forward port of the createMock function that
114 * was introduced in PHPUnit 5.4.
116 * @param string $originalClassName
117 * @return PHPUnit_Framework_MockObject_MockObject
120 public function createMock( $originalClassName ) {
121 if ( is_callable( 'parent::createMock' ) ) {
122 return parent
::createMock( $originalClassName );
124 // Compat for PHPUnit <= 5.4
125 return $this->getMockBuilder( $originalClassName )
126 ->disableOriginalConstructor()
127 ->disableOriginalClone()
128 ->disableArgumentCloning()
129 // New in phpunit-mock-objects 3.2 (phpunit 5.4.0)
130 // ->disallowMockingUnknownTypes()
135 * Marks the current test as risky. This
136 * is a forward port of the markAsRisky function that
137 * was introduced in PHPUnit 5.7.6.
139 public function markAsRisky() {
140 if ( is_callable( 'parent::markAsRisky' ) ) {
141 return parent
::markAsRisky();
144 // "risky" tests are not supported in phpunit 4, so just ignore