Debian Codegraph @

tests/phpunit/unit/includes/libs/ParamValidator/SimpleCallbacksTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\ParamValidator;
4
5 use PHPUnit\Framework\TestCase;
6 use Psr\Http\Message\UploadedFileInterface;
7 use Wikimedia\Message\DataMessageValue;
8 use Wikimedia\ParamValidator\SimpleCallbacks;
9
10 /**
11 * @covers \Wikimedia\ParamValidator\SimpleCallbacks
12 */
13 class SimpleCallbacksTest extends TestCase {
14
15 public function testDataAccess() {
16 $callbacks = new SimpleCallbacks(
17 [ 'foo' => 'Foo!', 'bar' => null ],
18 [
19 'file1' => [
20 'name' => 'example.txt',
21 'type' => 'text/plain',
22 'tmp_name' => '...',
23 'error' => UPLOAD_ERR_OK,
24 'size' => 123,
25 ],
26 'file2' => [
27 'name' => '',
28 'type' => '',
29 'tmp_name' => '',
30 'error' => UPLOAD_ERR_NO_FILE,
31 'size' => 0,
32 ],
33 ]
34 );
35
36 $this->assertTrue( $callbacks->hasParam( 'foo', [] ) );
37 $this->assertFalse( $callbacks->hasParam( 'bar', [] ) );
38 $this->assertFalse( $callbacks->hasParam( 'baz', [] ) );
39 $this->assertFalse( $callbacks->hasParam( 'file1', [] ) );
40
41 $this->assertSame( 'Foo!', $callbacks->getValue( 'foo', null, [] ) );
42 $this->assertSame( null, $callbacks->getValue( 'bar', null, [] ) );
43 $this->assertSame( 123, $callbacks->getValue( 'bar', 123, [] ) );
44 $this->assertSame( null, $callbacks->getValue( 'baz', null, [] ) );
45 $this->assertSame( null, $callbacks->getValue( 'file1', null, [] ) );
46
47 $this->assertFalse( $callbacks->hasUpload( 'foo', [] ) );
48 $this->assertFalse( $callbacks->hasUpload( 'bar', [] ) );
49 $this->assertTrue( $callbacks->hasUpload( 'file1', [] ) );
50 $this->assertTrue( $callbacks->hasUpload( 'file2', [] ) );
51 $this->assertFalse( $callbacks->hasUpload( 'baz', [] ) );
52
53 $this->assertNull( $callbacks->getUploadedFile( 'foo', [] ) );
54 $this->assertNull( $callbacks->getUploadedFile( 'bar', [] ) );
55 $this->assertInstanceOf(
56 UploadedFileInterface::class, $callbacks->getUploadedFile( 'file1', [] )
57 );
58 $this->assertInstanceOf(
59 UploadedFileInterface::class, $callbacks->getUploadedFile( 'file2', [] )
60 );
61 $this->assertNull( $callbacks->getUploadedFile( 'baz', [] ) );
62
63 $file = $callbacks->getUploadedFile( 'file1', [] );
64 $this->assertSame( 'example.txt', $file->getClientFilename() );
65 $file = $callbacks->getUploadedFile( 'file2', [] );
66 $this->assertSame( UPLOAD_ERR_NO_FILE, $file->getError() );
67
68 $this->assertFalse( $callbacks->useHighLimits( [] ) );
69 $this->assertFalse( $callbacks->useHighLimits( [ 'useHighLimits' => false ] ) );
70 $this->assertTrue( $callbacks->useHighLimits( [ 'useHighLimits' => true ] ) );
71 }
72
73 public function testRecording() {
74 $callbacks = new SimpleCallbacks( [] );
75
76 $this->assertSame( [], $callbacks->getRecordedConditions() );
77
78 $m1 = DataMessageValue::new( 'XXX-from-test-1', [], 'foo' );
79 $callbacks->recordCondition( $m1, 'foo', 'Foo!', [], [] );
80 $m2 = DataMessageValue::new( 'XXX-from-test-2', [], 'barbar', [ 'bAr' => 'BaR' ] );
81 $callbacks->recordCondition( $m2, 'bar', null, [], [] );
82 $callbacks->recordCondition( $m2, 'bar', null, [], [] );
83 $this->assertSame( [
84 [ 'message' => $m1, 'name' => 'foo', 'value' => 'Foo!', 'settings' => [] ],
85 [ 'message' => $m2, 'name' => 'bar', 'value' => null, 'settings' => [] ],
86 [ 'message' => $m2, 'name' => 'bar', 'value' => null, 'settings' => [] ],
87 ], $callbacks->getRecordedConditions() );
88
89 $callbacks->clearRecordedConditions();
90 $this->assertSame( [], $callbacks->getRecordedConditions() );
91 $callbacks->recordCondition( $m1, 'foo', 'Foo!', [], [] );
92 $this->assertSame( [
93 [ 'message' => $m1, 'name' => 'foo', 'value' => 'Foo!', 'settings' => [] ],
94 ], $callbacks->getRecordedConditions() );
95 }
96
97 }