3 namespace Wikimedia\ParamValidator
;
5 use Psr\Http\Message\UploadedFileInterface
;
8 * @covers Wikimedia\ParamValidator\SimpleCallbacks
10 class SimpleCallbacksTest
extends \PHPUnit\Framework\TestCase
{
12 public function testDataAccess() {
13 $callbacks = new SimpleCallbacks(
14 [ 'foo' => 'Foo!', 'bar' => null ],
17 'name' => 'example.txt',
18 'type' => 'text/plain',
20 'error' => UPLOAD_ERR_OK
,
27 'error' => UPLOAD_ERR_NO_FILE
,
33 $this->assertTrue( $callbacks->hasParam( 'foo', [] ) );
34 $this->assertFalse( $callbacks->hasParam( 'bar', [] ) );
35 $this->assertFalse( $callbacks->hasParam( 'baz', [] ) );
36 $this->assertFalse( $callbacks->hasParam( 'file1', [] ) );
38 $this->assertSame( 'Foo!', $callbacks->getValue( 'foo', null, [] ) );
39 $this->assertSame( null, $callbacks->getValue( 'bar', null, [] ) );
40 $this->assertSame( 123, $callbacks->getValue( 'bar', 123, [] ) );
41 $this->assertSame( null, $callbacks->getValue( 'baz', null, [] ) );
42 $this->assertSame( null, $callbacks->getValue( 'file1', null, [] ) );
44 $this->assertFalse( $callbacks->hasUpload( 'foo', [] ) );
45 $this->assertFalse( $callbacks->hasUpload( 'bar', [] ) );
46 $this->assertTrue( $callbacks->hasUpload( 'file1', [] ) );
47 $this->assertTrue( $callbacks->hasUpload( 'file2', [] ) );
48 $this->assertFalse( $callbacks->hasUpload( 'baz', [] ) );
50 $this->assertNull( $callbacks->getUploadedFile( 'foo', [] ) );
51 $this->assertNull( $callbacks->getUploadedFile( 'bar', [] ) );
52 $this->assertInstanceOf(
53 UploadedFileInterface
::class, $callbacks->getUploadedFile( 'file1', [] )
55 $this->assertInstanceOf(
56 UploadedFileInterface
::class, $callbacks->getUploadedFile( 'file2', [] )
58 $this->assertNull( $callbacks->getUploadedFile( 'baz', [] ) );
60 $file = $callbacks->getUploadedFile( 'file1', [] );
61 $this->assertSame( 'example.txt', $file->getClientFilename() );
62 $file = $callbacks->getUploadedFile( 'file2', [] );
63 $this->assertSame( UPLOAD_ERR_NO_FILE
, $file->getError() );
65 $this->assertFalse( $callbacks->useHighLimits( [] ) );
66 $this->assertFalse( $callbacks->useHighLimits( [ 'useHighLimits' => false ] ) );
67 $this->assertTrue( $callbacks->useHighLimits( [ 'useHighLimits' => true ] ) );
70 public function testRecording() {
71 $callbacks = new SimpleCallbacks( [] );
73 $this->assertSame( [], $callbacks->getRecordedConditions() );
75 $ex1 = new ValidationException( 'foo', 'Foo!', [], 'foo', [] );
76 $callbacks->recordCondition( $ex1, [] );
77 $ex2 = new ValidationException( 'bar', null, [], 'barbar', [ 'bAr' => 'BaR' ] );
78 $callbacks->recordCondition( $ex2, [] );
79 $callbacks->recordCondition( $ex2, [] );
80 $this->assertSame( [ $ex1, $ex2, $ex2 ], $callbacks->getRecordedConditions() );
82 $callbacks->clearRecordedConditions();
83 $this->assertSame( [], $callbacks->getRecordedConditions() );
84 $callbacks->recordCondition( $ex1, [] );
85 $this->assertSame( [ $ex1 ], $callbacks->getRecordedConditions() );