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 $this->expectException( $name );
34 if ( $message !== '' ) {
35 $this->expectExceptionMessage( $message );
37 if ( $code !== null ) {
38 $this->expectExceptionCode( $code );
41 parent
::setExpectedException( $name, $message, $code );
46 * @see PHPUnit_Framework_TestCase::getMock
48 * @return PHPUnit_Framework_MockObject_MockObject
50 public function getMock( $originalClassName, $methods = [], array $arguments = [],
51 $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
52 $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
55 if ( is_callable( 'parent::getMock' ) ) {
56 return parent
::getMock(
57 $originalClassName, $methods, $arguments, $mockClassName,
58 $callOriginalConstructor, $callOriginalClone, $callAutoload,
59 $cloneArguments, $callOriginalMethods, $proxyTarget
62 $builder = $this->getMockBuilder( $originalClassName )
63 ->setMethods( $methods )
64 ->setConstructorArgs( $arguments )
65 ->setMockClassName( $mockClassName )
66 ->setProxyTarget( $proxyTarget );
67 if ( $callOriginalConstructor ) {
68 $builder->enableOriginalConstructor();
70 $builder->disableOriginalConstructor();
72 if ( $callOriginalClone ) {
73 $builder->enableOriginalClone();
75 $builder->disableOriginalClone();
77 if ( $callAutoload ) {
78 $builder->enableAutoload();
80 $builder->disableAutoload();
82 if ( $cloneArguments ) {
83 $builder->enableArgumentCloning();
85 $builder->disableArgumentCloning();
87 if ( $callOriginalMethods ) {
88 $builder->enableProxyingToOriginalMethods();
90 $builder->disableProxyingToOriginalMethods();
93 return $builder->getMock();
98 * Return a test double for the specified class. This
99 * is a forward port of the createMock function that
100 * was introduced in PHPUnit 5.4.
102 * @param string $originalClassName
103 * @return PHPUnit_Framework_MockObject_MockObject
106 public function createMock( $originalClassName ) {
107 if ( is_callable( 'parent::createMock' ) ) {
108 return parent
::createMock( $originalClassName );
110 // Compat for PHPUnit <= 5.4
111 return $this->getMockBuilder( $originalClassName )
112 ->disableOriginalConstructor()
113 ->disableOriginalClone()
114 ->disableArgumentCloning()
115 // New in phpunit-mock-objects 3.2 (phpunit 5.4.0)
116 // ->disallowMockingUnknownTypes()