Do not insert page titles into querycache.qc_value
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCaseTrait.php
1 <?php
2
3 /**
4 * For code common to both MediaWikiUnitTestCase and MediaWikiIntegrationTestCase.
5 */
6 trait MediaWikiTestCaseTrait {
7 /**
8 * Returns a PHPUnit constraint that matches anything other than a fixed set of values. This can
9 * be used to whitelist values, e.g.
10 * $mock->expects( $this->never() )->method( $this->anythingBut( 'foo', 'bar' ) );
11 * which will throw if any unexpected method is called.
12 *
13 * @param mixed ...$values Values that are not matched
14 */
15 protected function anythingBut( ...$values ) {
16 return $this->logicalNot( $this->logicalOr(
17 ...array_map( [ $this, 'matches' ], $values )
18 ) );
19 }
20
21 /**
22 * Return a PHPUnit mock that is expected to never have any methods called on it.
23 *
24 * @param string $type
25 * @return object
26 */
27 protected function createNoOpMock( $type ) {
28 $mock = $this->createMock( $type );
29 $mock->expects( $this->never() )->method( $this->anythingBut( '__destruct' ) );
30 return $mock;
31 }
32
33 /**
34 * Check whether file contains given data.
35 * @param string $fileName
36 * @param string $actualData
37 * @param bool $createIfMissing If true, and file does not exist, create it with given data
38 * and skip the test.
39 * @param string $msg
40 * @since 1.30
41 */
42 protected function assertFileContains(
43 $fileName,
44 $actualData,
45 $createIfMissing = false,
46 $msg = ''
47 ) {
48 if ( $createIfMissing ) {
49 if ( !file_exists( $fileName ) ) {
50 file_put_contents( $fileName, $actualData );
51 $this->markTestSkipped( "Data file $fileName does not exist" );
52 }
53 } else {
54 self::assertFileExists( $fileName );
55 }
56 self::assertEquals( file_get_contents( $fileName ), $actualData, $msg );
57 }
58 }