Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ParamValidator / TypeDef / StringDefTest.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\ParamValidator\ParamValidator;
6 use Wikimedia\ParamValidator\SimpleCallbacks;
7 use Wikimedia\ParamValidator\ValidationException;
8
9 /**
10 * @covers Wikimedia\ParamValidator\TypeDef\StringDef
11 */
12 class StringDefTest extends TypeDefTestCase {
13
14 protected static $testClass = StringDef::class;
15
16 protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
17 if ( static::$testClass === null ) {
18 throw new \LogicException( 'Either assign static::$testClass or override ' . __METHOD__ );
19 }
20
21 return new static::$testClass( $callbacks, $options );
22 }
23
24 public function provideValidate() {
25 $req = [
26 ParamValidator::PARAM_REQUIRED => true,
27 ];
28 $maxBytes = [
29 StringDef::PARAM_MAX_BYTES => 4,
30 ];
31 $maxChars = [
32 StringDef::PARAM_MAX_CHARS => 2,
33 ];
34
35 return [
36 'Basic' => [ '123', '123' ],
37 'Empty' => [ '', '' ],
38 'Empty, required' => [
39 '',
40 new ValidationException( 'test', '', [], 'missingparam', [] ),
41 $req,
42 ],
43 'Empty, required, allowed' => [ '', '', $req, [ 'allowEmptyWhenRequired' => true ] ],
44 'Max bytes, ok' => [ 'abcd', 'abcd', $maxBytes ],
45 'Max bytes, exceeded' => [
46 'abcde',
47 new ValidationException( 'test', '', [], 'maxbytes', [ 'maxbytes' => 4, 'maxchars' => '' ] ),
48 $maxBytes,
49 ],
50 'Max bytes, ok (2)' => [ '😄', '😄', $maxBytes ],
51 'Max bytes, exceeded (2)' => [
52 '😭?',
53 new ValidationException( 'test', '', [], 'maxbytes', [ 'maxbytes' => 4, 'maxchars' => '' ] ),
54 $maxBytes,
55 ],
56 'Max chars, ok' => [ 'ab', 'ab', $maxChars ],
57 'Max chars, exceeded' => [
58 'abc',
59 new ValidationException( 'test', '', [], 'maxchars', [ 'maxbytes' => '', 'maxchars' => 2 ] ),
60 $maxChars,
61 ],
62 'Max chars, ok (2)' => [ '😄😄', '😄😄', $maxChars ],
63 'Max chars, exceeded (2)' => [
64 '😭??',
65 new ValidationException( 'test', '', [], 'maxchars', [ 'maxbytes' => '', 'maxchars' => 2 ] ),
66 $maxChars,
67 ],
68 ];
69 }
70
71 }