Update declaration of UploadFromUrlTest::doApiRequest
[lhc/web/wiklou.git] / tests / phpunit / PHPUnit4And6Compat.php
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 /**
22 * @since 1.31
23 */
24 trait PHPUnit4And6Compat {
25 /**
26 * @see PHPUnit_Framework_TestCase::setExpectedException
27 *
28 * This function was renamed to expectException() in PHPUnit 6, so this
29 * is a temporary backwards-compatibility layer while we transition.
30 */
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 );
36 }
37 if ( $code !== null ) {
38 $this->expectExceptionCode( $code );
39 }
40 } else {
41 parent::setExpectedException( $name, $message, $code );
42 }
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
93 return $builder->getMock();
94 }
95 }
96
97 /**
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.
101 *
102 * @param string $originalClassName
103 * @return PHPUnit_Framework_MockObject_MockObject
104 * @throws Exception
105 */
106 public function createMock( $originalClassName ) {
107 if ( is_callable( 'parent::createMock' ) ) {
108 return parent::createMock( $originalClassName );
109 }
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()
117 ->getMock();
118 }
119 }