Database: Allow selectFieldValues() to accept SQL fragments
[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 if ( $name !== null ) {
34 $this->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 * @see PHPUnit_Framework_TestCase::getMock
49 *
50 * @return PHPUnit_Framework_MockObject_MockObject
51 */
52 public function getMock( $originalClassName, $methods = [], array $arguments = [],
53 $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
54 $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
55 $proxyTarget = null
56 ) {
57 if ( is_callable( 'parent::getMock' ) ) {
58 return parent::getMock(
59 $originalClassName, $methods, $arguments, $mockClassName,
60 $callOriginalConstructor, $callOriginalClone, $callAutoload,
61 $cloneArguments, $callOriginalMethods, $proxyTarget
62 );
63 } else {
64 $builder = $this->getMockBuilder( $originalClassName )
65 ->setMethods( $methods )
66 ->setConstructorArgs( $arguments )
67 ->setMockClassName( $mockClassName )
68 ->setProxyTarget( $proxyTarget );
69 if ( $callOriginalConstructor ) {
70 $builder->enableOriginalConstructor();
71 } else {
72 $builder->disableOriginalConstructor();
73 }
74 if ( $callOriginalClone ) {
75 $builder->enableOriginalClone();
76 } else {
77 $builder->disableOriginalClone();
78 }
79 if ( $callAutoload ) {
80 $builder->enableAutoload();
81 } else {
82 $builder->disableAutoload();
83 }
84 if ( $cloneArguments ) {
85 $builder->enableArgumentCloning();
86 } else {
87 $builder->disableArgumentCloning();
88 }
89 if ( $callOriginalMethods ) {
90 $builder->enableProxyingToOriginalMethods();
91 } else {
92 $builder->disableProxyingToOriginalMethods();
93 }
94
95 return $builder->getMock();
96 }
97 }
98
99 /**
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.
103 *
104 * @param string $originalClassName
105 * @return PHPUnit_Framework_MockObject_MockObject
106 * @throws Exception
107 */
108 public function createMock( $originalClassName ) {
109 if ( is_callable( 'parent::createMock' ) ) {
110 return parent::createMock( $originalClassName );
111 }
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()
119 ->getMock();
120 }
121 }