Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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( 'parent::expectException' ) ) {
33 if ( $name !== null ) {
34 parent::expectException( $name );
35 }
36 if ( $message !== '' ) {
37 $this->expectExceptionMessage( $message );
38 }
39 if ( $code !== null ) {
40 $this->expectExceptionCode( $code );
41 }
42 } else {
43 parent::setExpectedException( $name, $message, $code );
44 }
45 }
46
47 /**
48 * Future-compatible layer for PHPUnit 4's setExpectedException.
49 */
50 public function expectException( $exception ) {
51 if ( is_callable( 'parent::expectException' ) ) {
52 parent::expectException( $exception );
53 return;
54 }
55
56 parent::setExpectedException( $exception );
57 }
58
59 /**
60 * @see PHPUnit_Framework_TestCase::getMock
61 *
62 * @return PHPUnit_Framework_MockObject_MockObject
63 */
64 public function getMock( $originalClassName, $methods = [], array $arguments = [],
65 $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
66 $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
67 $proxyTarget = null
68 ) {
69 if ( is_callable( 'parent::getMock' ) ) {
70 return parent::getMock(
71 $originalClassName, $methods, $arguments, $mockClassName,
72 $callOriginalConstructor, $callOriginalClone, $callAutoload,
73 $cloneArguments, $callOriginalMethods, $proxyTarget
74 );
75 } else {
76 $builder = $this->getMockBuilder( $originalClassName )
77 ->setMethods( $methods )
78 ->setConstructorArgs( $arguments )
79 ->setMockClassName( $mockClassName )
80 ->setProxyTarget( $proxyTarget );
81 if ( $callOriginalConstructor ) {
82 $builder->enableOriginalConstructor();
83 } else {
84 $builder->disableOriginalConstructor();
85 }
86 if ( $callOriginalClone ) {
87 $builder->enableOriginalClone();
88 } else {
89 $builder->disableOriginalClone();
90 }
91 if ( $callAutoload ) {
92 $builder->enableAutoload();
93 } else {
94 $builder->disableAutoload();
95 }
96 if ( $cloneArguments ) {
97 $builder->enableArgumentCloning();
98 } else {
99 $builder->disableArgumentCloning();
100 }
101 if ( $callOriginalMethods ) {
102 $builder->enableProxyingToOriginalMethods();
103 } else {
104 $builder->disableProxyingToOriginalMethods();
105 }
106
107 return $builder->getMock();
108 }
109 }
110
111 /**
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.
115 *
116 * @param string $originalClassName
117 * @return PHPUnit_Framework_MockObject_MockObject
118 * @throws Exception
119 */
120 public function createMock( $originalClassName ) {
121 if ( is_callable( 'parent::createMock' ) ) {
122 return parent::createMock( $originalClassName );
123 }
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()
131 ->getMock();
132 }
133
134 /**
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.
138 */
139 public function markAsRisky() {
140 if ( is_callable( 'parent::markAsRisky' ) ) {
141 return parent::markAsRisky();
142 }
143
144 // "risky" tests are not supported in phpunit 4, so just ignore
145 }
146
147 }