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( [ $this, 'expectException' ] ) ) {
33 if ( $name !== null ) {
34 $this->expectException( $name );
36 if ( $message !== '' ) {
37 $this->expectExceptionMessage( $message );
39 if ( $code !== null ) {
40 $this->expectExceptionCode( $code );
43 parent
::setExpectedException( $name, $message, $code );
48 * @see PHPUnit_Framework_TestCase::getMock
50 * @return PHPUnit_Framework_MockObject_MockObject
52 public function getMock( $originalClassName, $methods = [], array $arguments = [],
53 $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
54 $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
57 if ( is_callable( 'parent::getMock' ) ) {
58 return parent
::getMock(
59 $originalClassName, $methods, $arguments, $mockClassName,
60 $callOriginalConstructor, $callOriginalClone, $callAutoload,
61 $cloneArguments, $callOriginalMethods, $proxyTarget
64 $builder = $this->getMockBuilder( $originalClassName )
65 ->setMethods( $methods )
66 ->setConstructorArgs( $arguments )
67 ->setMockClassName( $mockClassName )
68 ->setProxyTarget( $proxyTarget );
69 if ( $callOriginalConstructor ) {
70 $builder->enableOriginalConstructor();
72 $builder->disableOriginalConstructor();
74 if ( $callOriginalClone ) {
75 $builder->enableOriginalClone();
77 $builder->disableOriginalClone();
79 if ( $callAutoload ) {
80 $builder->enableAutoload();
82 $builder->disableAutoload();
84 if ( $cloneArguments ) {
85 $builder->enableArgumentCloning();
87 $builder->disableArgumentCloning();
89 if ( $callOriginalMethods ) {
90 $builder->enableProxyingToOriginalMethods();
92 $builder->disableProxyingToOriginalMethods();
95 return $builder->getMock();
100 * Return a test double for the specified class. This
101 * is a forward port of the createMock function that
102 * was introduced in PHPUnit 5.4.
104 * @param string $originalClassName
105 * @return PHPUnit_Framework_MockObject_MockObject
108 public function createMock( $originalClassName ) {
109 if ( is_callable( 'parent::createMock' ) ) {
110 return parent
::createMock( $originalClassName );
112 // Compat for PHPUnit <= 5.4
113 return $this->getMockBuilder( $originalClassName )
114 ->disableOriginalConstructor()
115 ->disableOriginalClone()
116 ->disableArgumentCloning()
117 // New in phpunit-mock-objects 3.2 (phpunit 5.4.0)
118 // ->disallowMockingUnknownTypes()