3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\ParamValidator
;
6 use Wikimedia\ParamValidator\SimpleCallbacks
;
7 use Wikimedia\ParamValidator\ValidationException
;
10 * @covers Wikimedia\ParamValidator\TypeDef\StringDef
12 class StringDefTest
extends TypeDefTestCase
{
14 protected static $testClass = StringDef
::class;
16 protected function getInstance( SimpleCallbacks
$callbacks, array $options ) {
17 if ( static::$testClass === null ) {
18 throw new \
LogicException( 'Either assign static::$testClass or override ' . __METHOD__
);
21 return new static::$testClass( $callbacks, $options );
24 public function provideValidate() {
26 ParamValidator
::PARAM_REQUIRED
=> true,
29 StringDef
::PARAM_MAX_BYTES
=> 4,
32 StringDef
::PARAM_MAX_CHARS
=> 2,
36 'Basic' => [ '123', '123' ],
37 'Empty' => [ '', '' ],
38 'Empty, required' => [
40 new ValidationException( 'test', '', [], 'missingparam', [] ),
43 'Empty, required, allowed' => [ '', '', $req, [ 'allowEmptyWhenRequired' => true ] ],
44 'Max bytes, ok' => [ 'abcd', 'abcd', $maxBytes ],
45 'Max bytes, exceeded' => [
47 new ValidationException( 'test', '', [], 'maxbytes', [ 'maxbytes' => 4, 'maxchars' => '' ] ),
50 'Max bytes, ok (2)' => [ '😄', '😄', $maxBytes ],
51 'Max bytes, exceeded (2)' => [
53 new ValidationException( 'test', '', [], 'maxbytes', [ 'maxbytes' => 4, 'maxchars' => '' ] ),
56 'Max chars, ok' => [ 'ab', 'ab', $maxChars ],
57 'Max chars, exceeded' => [
59 new ValidationException( 'test', '', [], 'maxchars', [ 'maxbytes' => '', 'maxchars' => 2 ] ),
62 'Max chars, ok (2)' => [ '😄😄', '😄😄', $maxChars ],
63 'Max chars, exceeded (2)' => [
65 new ValidationException( 'test', '', [], 'maxchars', [ 'maxbytes' => '', 'maxchars' => 2 ] ),