ValidatorTest.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Validation;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Validation\Validation;
  18. use Cake\Validation\ValidationSet;
  19. use Cake\Validation\Validator;
  20. /**
  21. * Tests Validator class
  22. */
  23. class ValidatorTest extends TestCase
  24. {
  25. /**
  26. * Testing you can dynamically add rules to a field
  27. *
  28. * @return void
  29. */
  30. public function testAddingRulesToField()
  31. {
  32. $validator = new Validator();
  33. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  34. $set = $validator->field('title');
  35. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  36. $this->assertCount(1, $set);
  37. $validator->add('title', 'another', ['rule' => 'alphanumeric']);
  38. $this->assertCount(2, $set);
  39. $validator->add('body', 'another', ['rule' => 'crazy']);
  40. $this->assertCount(1, $validator->field('body'));
  41. $this->assertCount(2, $validator);
  42. $validator->add('email', 'notBlank');
  43. $result = $validator->field('email')->rule('notBlank')->get('rule');
  44. $this->assertEquals('notBlank', $result);
  45. }
  46. /**
  47. * Testing addNested field rules
  48. *
  49. * @return void
  50. */
  51. public function testAddNestedSingle()
  52. {
  53. $validator = new Validator();
  54. $inner = new Validator();
  55. $inner->add('username', 'not-blank', ['rule' => 'notBlank']);
  56. $this->assertSame($validator, $validator->addNested('user', $inner));
  57. $this->assertCount(1, $validator->field('user'));
  58. }
  59. /**
  60. * Testing addNested connects providers
  61. *
  62. * @return void
  63. */
  64. public function testAddNestedSingleProviders()
  65. {
  66. $validator = new Validator();
  67. $validator->setProvider('test', $this);
  68. $inner = new Validator();
  69. $inner->add('username', 'not-blank', ['rule' => function () use ($inner, $validator) {
  70. $this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
  71. return false;
  72. }]);
  73. $validator->addNested('user', $inner);
  74. $result = $validator->errors(['user' => ['username' => 'example']]);
  75. $this->assertNotEmpty($result, 'Validation should fail');
  76. }
  77. /**
  78. * Testing addNestedMany field rules
  79. *
  80. * @return void
  81. */
  82. public function testAddNestedMany()
  83. {
  84. $validator = new Validator();
  85. $inner = new Validator();
  86. $inner->add('comment', 'not-blank', ['rule' => 'notBlank']);
  87. $this->assertSame($validator, $validator->addNestedMany('comments', $inner));
  88. $this->assertCount(1, $validator->field('comments'));
  89. }
  90. /**
  91. * Testing addNestedMany connects providers
  92. *
  93. * @return void
  94. */
  95. public function testAddNestedManyProviders()
  96. {
  97. $validator = new Validator();
  98. $validator->setProvider('test', $this);
  99. $inner = new Validator();
  100. $inner->add('comment', 'not-blank', ['rule' => function () use ($inner, $validator) {
  101. $this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
  102. return false;
  103. }]);
  104. $validator->addNestedMany('comments', $inner);
  105. $result = $validator->errors(['comments' => [['comment' => 'example']]]);
  106. $this->assertNotEmpty($result, 'Validation should fail');
  107. }
  108. /**
  109. * Tests that calling field will create a default validation set for it
  110. *
  111. * @return void
  112. */
  113. public function testFieldDefault()
  114. {
  115. $validator = new Validator();
  116. $this->assertFalse($validator->hasField('foo'));
  117. $field = $validator->field('foo');
  118. $this->assertInstanceOf('Cake\Validation\ValidationSet', $field);
  119. $this->assertCount(0, $field);
  120. $this->assertTrue($validator->hasField('foo'));
  121. }
  122. /**
  123. * Tests that field method can be used as a setter
  124. *
  125. * @return void
  126. */
  127. public function testFieldSetter()
  128. {
  129. $validator = new Validator();
  130. $validationSet = new ValidationSet;
  131. $validator->field('thing', $validationSet);
  132. $this->assertSame($validationSet, $validator->field('thing'));
  133. }
  134. /**
  135. * Tests the remove method
  136. *
  137. * @return void
  138. */
  139. public function testRemove()
  140. {
  141. $validator = new Validator();
  142. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  143. $validator->add('title', 'foo', ['rule' => 'bar']);
  144. $this->assertCount(2, $validator->field('title'));
  145. $validator->remove('title');
  146. $this->assertCount(0, $validator->field('title'));
  147. $validator->remove('title');
  148. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  149. $validator->add('title', 'foo', ['rule' => 'bar']);
  150. $this->assertCount(2, $validator->field('title'));
  151. $validator->remove('title', 'foo');
  152. $this->assertCount(1, $validator->field('title'));
  153. $this->assertNull($validator->field('title')->rule('foo'));
  154. }
  155. /**
  156. * Tests the requirePresence method
  157. *
  158. * @return void
  159. */
  160. public function testRequirePresence()
  161. {
  162. $validator = new Validator();
  163. $this->assertSame($validator, $validator->requirePresence('title'));
  164. $this->assertTrue($validator->field('title')->isPresenceRequired());
  165. $validator->requirePresence('title', false);
  166. $this->assertFalse($validator->field('title')->isPresenceRequired());
  167. $validator->requirePresence('title', 'create');
  168. $this->assertEquals('create', $validator->field('title')->isPresenceRequired());
  169. $validator->requirePresence('title', 'update');
  170. $this->assertEquals('update', $validator->field('title')->isPresenceRequired());
  171. }
  172. /**
  173. * Tests the requirePresence method
  174. *
  175. * @return void
  176. */
  177. public function testRequirePresenceAsArray()
  178. {
  179. $validator = new Validator();
  180. $validator->requirePresence(['title', 'created']);
  181. $this->assertTrue($validator->field('title')->isPresenceRequired());
  182. $this->assertTrue($validator->field('created')->isPresenceRequired());
  183. $validator->requirePresence([
  184. 'title' => [
  185. 'mode' => false
  186. ],
  187. 'content' => [
  188. 'mode' => 'update'
  189. ],
  190. 'subject'
  191. ], true);
  192. $this->assertFalse($validator->field('title')->isPresenceRequired());
  193. $this->assertEquals('update', $validator->field('content')->isPresenceRequired());
  194. $this->assertTrue($validator->field('subject')->isPresenceRequired());
  195. }
  196. /**
  197. * Tests the requirePresence failure case
  198. *
  199. * @return void
  200. */
  201. public function testRequirePresenceAsArrayFailure()
  202. {
  203. $this->expectException(\InvalidArgumentException::class);
  204. $validator = new Validator();
  205. $validator->requirePresence(['title' => 'derp', 'created' => false]);
  206. }
  207. /**
  208. * Tests the requirePresence method when passing a callback
  209. *
  210. * @return void
  211. */
  212. public function testRequirePresenceCallback()
  213. {
  214. $validator = new Validator();
  215. $require = true;
  216. $validator->requirePresence('title', function ($context) use (&$require) {
  217. $this->assertEquals([], $context['data']);
  218. $this->assertEquals([], $context['providers']);
  219. $this->assertEquals('title', $context['field']);
  220. $this->assertTrue($context['newRecord']);
  221. return $require;
  222. });
  223. $this->assertTrue($validator->isPresenceRequired('title', true));
  224. $require = false;
  225. $this->assertFalse($validator->isPresenceRequired('title', true));
  226. }
  227. /**
  228. * Tests the isPresenceRequired method
  229. *
  230. * @return void
  231. */
  232. public function testIsPresenceRequired()
  233. {
  234. $validator = new Validator();
  235. $this->assertSame($validator, $validator->requirePresence('title'));
  236. $this->assertTrue($validator->isPresenceRequired('title', true));
  237. $this->assertTrue($validator->isPresenceRequired('title', false));
  238. $validator->requirePresence('title', false);
  239. $this->assertFalse($validator->isPresenceRequired('title', true));
  240. $this->assertFalse($validator->isPresenceRequired('title', false));
  241. $validator->requirePresence('title', 'create');
  242. $this->assertTrue($validator->isPresenceRequired('title', true));
  243. $this->assertFalse($validator->isPresenceRequired('title', false));
  244. $validator->requirePresence('title', 'update');
  245. $this->assertTrue($validator->isPresenceRequired('title', false));
  246. $this->assertFalse($validator->isPresenceRequired('title', true));
  247. }
  248. /**
  249. * Tests errors generated when a field presence is required
  250. *
  251. * @return void
  252. */
  253. public function testErrorsWithPresenceRequired()
  254. {
  255. $validator = new Validator();
  256. $validator->requirePresence('title');
  257. $errors = $validator->errors(['foo' => 'something']);
  258. $expected = ['title' => ['_required' => 'This field is required']];
  259. $this->assertEquals($expected, $errors);
  260. $this->assertEmpty($validator->errors(['title' => 'bar']));
  261. $validator->requirePresence('title', false);
  262. $this->assertEmpty($validator->errors(['foo' => 'bar']));
  263. }
  264. /**
  265. * Test that validation on a certain condition generate errors
  266. *
  267. * @return void
  268. */
  269. public function testErrorsWithPresenceRequiredOnCreate()
  270. {
  271. $validator = new Validator();
  272. $validator->requirePresence('id', 'update');
  273. $validator->allowEmpty('id', 'create');
  274. $validator->requirePresence('title');
  275. $data = [
  276. 'title' => 'Example title'
  277. ];
  278. $expected = [];
  279. $result = $validator->errors($data);
  280. $this->assertEquals($expected, $result);
  281. }
  282. /**
  283. * Test that errors() can work with nested data.
  284. *
  285. * @return void
  286. */
  287. public function testErrorsWithNestedFields()
  288. {
  289. $validator = new Validator();
  290. $user = new Validator();
  291. $user->add('username', 'letter', ['rule' => 'alphanumeric']);
  292. $comments = new Validator();
  293. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  294. $validator->addNested('user', $user);
  295. $validator->addNestedMany('comments', $comments);
  296. $data = [
  297. 'user' => [
  298. 'username' => 'is wrong'
  299. ],
  300. 'comments' => [
  301. ['comment' => 'is wrong']
  302. ]
  303. ];
  304. $errors = $validator->errors($data);
  305. $expected = [
  306. 'user' => [
  307. 'username' => ['letter' => 'The provided value is invalid']
  308. ],
  309. 'comments' => [
  310. 0 => ['comment' => ['letter' => 'The provided value is invalid']]
  311. ]
  312. ];
  313. $this->assertEquals($expected, $errors);
  314. }
  315. /**
  316. * Test nested fields with many, but invalid data.
  317. *
  318. * @return void
  319. */
  320. public function testErrorsWithNestedSingleInvalidType()
  321. {
  322. $validator = new Validator();
  323. $user = new Validator();
  324. $user->add('user', 'letter', ['rule' => 'alphanumeric']);
  325. $validator->addNested('user', $user);
  326. $data = [
  327. 'user' => 'a string',
  328. ];
  329. $errors = $validator->errors($data);
  330. $expected = [
  331. 'user' => ['_nested' => 'The provided value is invalid'],
  332. ];
  333. $this->assertEquals($expected, $errors);
  334. }
  335. /**
  336. * Test nested fields with many, but invalid data.
  337. *
  338. * @return void
  339. */
  340. public function testErrorsWithNestedManyInvalidType()
  341. {
  342. $validator = new Validator();
  343. $comments = new Validator();
  344. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  345. $validator->addNestedMany('comments', $comments);
  346. $data = [
  347. 'comments' => 'a string',
  348. ];
  349. $errors = $validator->errors($data);
  350. $expected = [
  351. 'comments' => ['_nested' => 'The provided value is invalid'],
  352. ];
  353. $this->assertEquals($expected, $errors);
  354. }
  355. /**
  356. * Test nested fields with many, but invalid data.
  357. *
  358. * @return void
  359. */
  360. public function testErrorsWithNestedManySomeInvalid()
  361. {
  362. $validator = new Validator();
  363. $comments = new Validator();
  364. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  365. $validator->addNestedMany('comments', $comments);
  366. $data = [
  367. 'comments' => [
  368. 'a string',
  369. ['comment' => 'letters'],
  370. ['comment' => 'more invalid']
  371. ]
  372. ];
  373. $errors = $validator->errors($data);
  374. $expected = [
  375. 'comments' => [
  376. '_nested' => 'The provided value is invalid',
  377. ],
  378. ];
  379. $this->assertEquals($expected, $errors);
  380. }
  381. /**
  382. * Tests custom error messages generated when a field presence is required
  383. *
  384. * @return void
  385. */
  386. public function testCustomErrorsWithPresenceRequired()
  387. {
  388. $validator = new Validator();
  389. $validator->requirePresence('title', true, 'Custom message');
  390. $errors = $validator->errors(['foo' => 'something']);
  391. $expected = ['title' => ['_required' => 'Custom message']];
  392. $this->assertEquals($expected, $errors);
  393. }
  394. /**
  395. * Tests custom error messages generated when a field presence is required
  396. *
  397. * @return void
  398. */
  399. public function testCustomErrorsWithPresenceRequiredAsArray()
  400. {
  401. $validator = new Validator();
  402. $validator->requirePresence(['title', 'content'], true, 'Custom message');
  403. $errors = $validator->errors(['foo' => 'something']);
  404. $expected = [
  405. 'title' => ['_required' => 'Custom message'],
  406. 'content' => ['_required' => 'Custom message']
  407. ];
  408. $this->assertEquals($expected, $errors);
  409. $validator->requirePresence([
  410. 'title' => [
  411. 'message' => 'Test message'
  412. ],
  413. 'content'
  414. ], true, 'Custom message');
  415. $errors = $validator->errors(['foo' => 'something']);
  416. $expected = [
  417. 'title' => ['_required' => 'Test message'],
  418. 'content' => ['_required' => 'Custom message']
  419. ];
  420. $this->assertEquals($expected, $errors);
  421. }
  422. /**
  423. * Tests the allowEmpty method
  424. *
  425. * @return void
  426. */
  427. public function testAllowEmpty()
  428. {
  429. $validator = new Validator();
  430. $this->assertSame($validator, $validator->allowEmpty('title'));
  431. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  432. $validator->allowEmpty('title', 'create');
  433. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  434. $validator->allowEmpty('title', 'update');
  435. $this->assertEquals('update', $validator->field('title')->isEmptyAllowed());
  436. }
  437. /**
  438. * Tests the allowEmpty method with date/time fields.
  439. *
  440. * @return void
  441. */
  442. public function testAllowEmptyDateTime()
  443. {
  444. $validator = new Validator();
  445. $validator->allowEmpty('created')
  446. ->add('created', 'date', ['rule' => 'date']);
  447. $data = [
  448. 'created' => [
  449. 'year' => '',
  450. 'month' => '',
  451. 'day' => ''
  452. ]
  453. ];
  454. $result = $validator->errors($data);
  455. $this->assertEmpty($result, 'No errors on empty date');
  456. $data = [
  457. 'created' => [
  458. 'year' => '',
  459. 'month' => '',
  460. 'day' => '',
  461. 'hour' => '',
  462. 'minute' => '',
  463. 'second' => '',
  464. 'meridian' => '',
  465. ]
  466. ];
  467. $result = $validator->errors($data);
  468. $this->assertEmpty($result, 'No errors on empty datetime');
  469. $data = [
  470. 'created' => [
  471. 'hour' => '',
  472. 'minute' => '',
  473. 'meridian' => '',
  474. ]
  475. ];
  476. $result = $validator->errors($data);
  477. $this->assertEmpty($result, 'No errors on empty time');
  478. }
  479. /**
  480. * Tests the allowEmpty method with file fields.
  481. *
  482. * @return void
  483. */
  484. public function testAllowEmptyFileFields()
  485. {
  486. $validator = new Validator();
  487. $validator->allowEmpty('picture')
  488. ->add('picture', 'file', ['rule' => 'uploadedFile']);
  489. $data = [
  490. 'picture' => [
  491. 'name' => '',
  492. 'type' => '',
  493. 'tmp_name' => '',
  494. 'error' => UPLOAD_ERR_NO_FILE,
  495. ]
  496. ];
  497. $result = $validator->errors($data);
  498. $this->assertEmpty($result, 'No errors on empty date');
  499. $data = [
  500. 'picture' => [
  501. 'name' => 'fake.png',
  502. 'type' => '',
  503. 'tmp_name' => '',
  504. 'error' => UPLOAD_ERR_OK,
  505. ]
  506. ];
  507. $result = $validator->errors($data);
  508. $this->assertNotEmpty($result, 'Invalid file should be caught still.');
  509. }
  510. /**
  511. * Tests the allowEmpty as array method
  512. *
  513. * @return void
  514. */
  515. public function testAllowEmptyAsArray()
  516. {
  517. $validator = new Validator();
  518. $validator->allowEmpty([
  519. 'title',
  520. 'subject',
  521. 'posted_at' => [
  522. 'when' => false,
  523. 'message' => 'Post time cannot be empty'
  524. ],
  525. 'updated_at' => [
  526. 'when' => true
  527. ],
  528. 'show_at' => [
  529. 'when' => 'update'
  530. ]
  531. ], 'create', 'Cannot be empty');
  532. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  533. $this->assertEquals('create', $validator->field('subject')->isEmptyAllowed());
  534. $this->assertFalse($validator->field('posted_at')->isEmptyAllowed());
  535. $this->assertTrue($validator->field('updated_at')->isEmptyAllowed());
  536. $this->assertEquals('update', $validator->field('show_at')->isEmptyAllowed());
  537. $errors = $validator->errors([
  538. 'title' => '',
  539. 'subject' => null,
  540. 'posted_at' => null,
  541. 'updated_at' => null,
  542. 'show_at' => ''
  543. ], false);
  544. $expected = [
  545. 'title' => ['_empty' => 'Cannot be empty'],
  546. 'subject' => ['_empty' => 'Cannot be empty'],
  547. 'posted_at' => ['_empty' => 'Post time cannot be empty']
  548. ];
  549. $this->assertEquals($expected, $errors);
  550. }
  551. /**
  552. * Tests the allowEmpty failure case
  553. *
  554. * @return void
  555. */
  556. public function testAllowEmptyAsArrayFailure()
  557. {
  558. $this->expectException(\InvalidArgumentException::class);
  559. $validator = new Validator();
  560. $validator->allowEmpty(['title' => 'derp', 'created' => false]);
  561. }
  562. /**
  563. * Test the notEmpty() method.
  564. *
  565. * @return void
  566. */
  567. public function testNotEmpty()
  568. {
  569. $validator = new Validator();
  570. $validator->notEmpty('title');
  571. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  572. $validator->allowEmpty('title');
  573. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  574. }
  575. /**
  576. * Tests the notEmpty as array method
  577. *
  578. * @return void
  579. */
  580. public function testNotEmptyAsArray()
  581. {
  582. $validator = new Validator();
  583. $validator->notEmpty(['title', 'created']);
  584. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  585. $this->assertFalse($validator->field('created')->isEmptyAllowed());
  586. $validator->notEmpty([
  587. 'title' => [
  588. 'when' => false
  589. ],
  590. 'content' => [
  591. 'when' => 'update'
  592. ],
  593. 'posted_at' => [
  594. 'when' => 'create'
  595. ],
  596. 'show_at' => [
  597. 'message' => 'Show date cannot be empty',
  598. 'when' => false
  599. ],
  600. 'subject'
  601. ], 'Not empty', true);
  602. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  603. $this->assertTrue($validator->isEmptyAllowed('content', true));
  604. $this->assertFalse($validator->isEmptyAllowed('content', false));
  605. $this->assertFalse($validator->isEmptyAllowed('posted_at', true));
  606. $this->assertTrue($validator->isEmptyAllowed('posted_at', false));
  607. $this->assertTrue($validator->field('subject')->isEmptyAllowed());
  608. $errors = $validator->errors([
  609. 'title' => '',
  610. 'content' => '',
  611. 'posted_at' => null,
  612. 'show_at' => null,
  613. 'subject' => ''
  614. ], false);
  615. $expected = [
  616. 'title' => ['_empty' => 'Not empty'],
  617. 'content' => ['_empty' => 'Not empty'],
  618. 'show_at' => ['_empty' => 'Show date cannot be empty']
  619. ];
  620. $this->assertEquals($expected, $errors);
  621. }
  622. /**
  623. * Tests the notEmpty failure case
  624. *
  625. * @return void
  626. */
  627. public function testNotEmptyAsArrayFailure()
  628. {
  629. $this->expectException(\InvalidArgumentException::class);
  630. $validator = new Validator();
  631. $validator->notEmpty(['title' => 'derp', 'created' => false]);
  632. }
  633. /**
  634. * Test the notEmpty() method.
  635. *
  636. * @return void
  637. */
  638. public function testNotEmptyModes()
  639. {
  640. $validator = new Validator();
  641. $validator->notEmpty('title', 'Need a title', 'create');
  642. $this->assertFalse($validator->isEmptyAllowed('title', true));
  643. $this->assertTrue($validator->isEmptyAllowed('title', false));
  644. $validator->notEmpty('title', 'Need a title', 'update');
  645. $this->assertTrue($validator->isEmptyAllowed('title', true));
  646. $this->assertFalse($validator->isEmptyAllowed('title', false));
  647. $validator->notEmpty('title', 'Need a title');
  648. $this->assertFalse($validator->isEmptyAllowed('title', true));
  649. $this->assertFalse($validator->isEmptyAllowed('title', false));
  650. $validator->notEmpty('title');
  651. $this->assertFalse($validator->isEmptyAllowed('title', true));
  652. $this->assertFalse($validator->isEmptyAllowed('title', false));
  653. }
  654. /**
  655. * Test interactions between notEmpty() and isAllowed().
  656. *
  657. * @return void
  658. */
  659. public function testNotEmptyAndIsAllowed()
  660. {
  661. $validator = new Validator();
  662. $validator->allowEmpty('title')
  663. ->notEmpty('title', 'Need it', 'update');
  664. $this->assertTrue($validator->isEmptyAllowed('title', true));
  665. $this->assertFalse($validator->isEmptyAllowed('title', false));
  666. $validator->allowEmpty('title')
  667. ->notEmpty('title');
  668. $this->assertFalse($validator->isEmptyAllowed('title', true));
  669. $this->assertFalse($validator->isEmptyAllowed('title', false));
  670. $validator->notEmpty('title')
  671. ->allowEmpty('title', 'create');
  672. $this->assertTrue($validator->isEmptyAllowed('title', true));
  673. $this->assertFalse($validator->isEmptyAllowed('title', false));
  674. }
  675. /**
  676. * Tests the allowEmpty method when passing a callback
  677. *
  678. * @return void
  679. */
  680. public function testAllowEmptyCallback()
  681. {
  682. $validator = new Validator();
  683. $allow = true;
  684. $validator->allowEmpty('title', function ($context) use (&$allow) {
  685. $this->assertEquals([], $context['data']);
  686. $this->assertEquals([], $context['providers']);
  687. $this->assertTrue($context['newRecord']);
  688. return $allow;
  689. });
  690. $this->assertTrue($validator->isEmptyAllowed('title', true));
  691. $allow = false;
  692. $this->assertFalse($validator->isEmptyAllowed('title', true));
  693. }
  694. /**
  695. * Tests the notEmpty method when passing a callback
  696. *
  697. * @return void
  698. */
  699. public function testNotEmptyCallback()
  700. {
  701. $validator = new Validator();
  702. $prevent = true;
  703. $validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
  704. $this->assertEquals([], $context['data']);
  705. $this->assertEquals([], $context['providers']);
  706. $this->assertFalse($context['newRecord']);
  707. return $prevent;
  708. });
  709. $this->assertFalse($validator->isEmptyAllowed('title', false));
  710. $prevent = false;
  711. $this->assertTrue($validator->isEmptyAllowed('title', false));
  712. }
  713. /**
  714. * Tests the isEmptyAllowed method
  715. *
  716. * @return void
  717. */
  718. public function testIsEmptyAllowed()
  719. {
  720. $validator = new Validator();
  721. $this->assertSame($validator, $validator->allowEmpty('title'));
  722. $this->assertTrue($validator->isEmptyAllowed('title', true));
  723. $this->assertTrue($validator->isEmptyAllowed('title', false));
  724. $validator->notEmpty('title');
  725. $this->assertFalse($validator->isEmptyAllowed('title', true));
  726. $this->assertFalse($validator->isEmptyAllowed('title', false));
  727. $validator->allowEmpty('title', 'create');
  728. $this->assertTrue($validator->isEmptyAllowed('title', true));
  729. $this->assertFalse($validator->isEmptyAllowed('title', false));
  730. $validator->allowEmpty('title', 'update');
  731. $this->assertTrue($validator->isEmptyAllowed('title', false));
  732. $this->assertFalse($validator->isEmptyAllowed('title', true));
  733. }
  734. /**
  735. * Tests errors generated when a field is not allowed to be empty
  736. *
  737. * @return void
  738. */
  739. public function testErrorsWithEmptyNotAllowed()
  740. {
  741. $validator = new Validator();
  742. $validator->notEmpty('title');
  743. $errors = $validator->errors(['title' => '']);
  744. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  745. $this->assertEquals($expected, $errors);
  746. $errors = $validator->errors(['title' => []]);
  747. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  748. $this->assertEquals($expected, $errors);
  749. $errors = $validator->errors(['title' => null]);
  750. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  751. $this->assertEquals($expected, $errors);
  752. $errors = $validator->errors(['title' => 0]);
  753. $this->assertEmpty($errors);
  754. $errors = $validator->errors(['title' => '0']);
  755. $this->assertEmpty($errors);
  756. $errors = $validator->errors(['title' => false]);
  757. $this->assertEmpty($errors);
  758. }
  759. /**
  760. * Tests custom error messages generated when a field is allowed to be empty
  761. *
  762. * @return void
  763. */
  764. public function testCustomErrorsWithAllowedEmpty()
  765. {
  766. $validator = new Validator();
  767. $validator->allowEmpty('title', false, 'Custom message');
  768. $errors = $validator->errors(['title' => null]);
  769. $expected = ['title' => ['_empty' => 'Custom message']];
  770. $this->assertEquals($expected, $errors);
  771. }
  772. /**
  773. * Tests custom error messages generated when a field is not allowed to be empty
  774. *
  775. * @return void
  776. */
  777. public function testCustomErrorsWithEmptyNotAllowed()
  778. {
  779. $validator = new Validator();
  780. $validator->notEmpty('title', 'Custom message');
  781. $errors = $validator->errors(['title' => '']);
  782. $expected = ['title' => ['_empty' => 'Custom message']];
  783. $this->assertEquals($expected, $errors);
  784. }
  785. /**
  786. * Tests errors generated when a field is allowed to be empty
  787. *
  788. * @return void
  789. */
  790. public function testErrorsWithEmptyAllowed()
  791. {
  792. $validator = new Validator();
  793. $validator->allowEmpty('title');
  794. $errors = $validator->errors(['title' => '']);
  795. $this->assertEmpty($errors);
  796. $errors = $validator->errors(['title' => []]);
  797. $this->assertEmpty($errors);
  798. $errors = $validator->errors(['title' => null]);
  799. $this->assertEmpty($errors);
  800. $errors = $validator->errors(['title' => 0]);
  801. $this->assertEmpty($errors);
  802. $errors = $validator->errors(['title' => 0.0]);
  803. $this->assertEmpty($errors);
  804. $errors = $validator->errors(['title' => '0']);
  805. $this->assertEmpty($errors);
  806. $errors = $validator->errors(['title' => false]);
  807. $this->assertEmpty($errors);
  808. }
  809. /**
  810. * Test the provider() method
  811. *
  812. * @return void
  813. */
  814. public function testProvider()
  815. {
  816. $validator = new Validator();
  817. $object = new \stdClass;
  818. $this->assertSame($validator, $validator->setProvider('foo', $object));
  819. $this->assertSame($object, $validator->getProvider('foo'));
  820. $this->assertNull($validator->getProvider('bar'));
  821. $another = new \stdClass;
  822. $this->assertSame($validator, $validator->setProvider('bar', $another));
  823. $this->assertSame($another, $validator->getProvider('bar'));
  824. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->getProvider('default'));
  825. }
  826. /**
  827. * Tests errors() method when using validators from the default provider, this proves
  828. * that it returns a default validation message and the custom one set in the rule
  829. *
  830. * @return void
  831. */
  832. public function testErrorsFromDefaultProvider()
  833. {
  834. $validator = new Validator();
  835. $validator
  836. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  837. ->add('email', 'notBlank', ['rule' => 'notBlank'])
  838. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  839. $errors = $validator->errors(['email' => 'not an email!']);
  840. $expected = [
  841. 'email' => [
  842. 'alpha' => 'The provided value is invalid',
  843. 'email' => 'Y u no write email?'
  844. ]
  845. ];
  846. $this->assertEquals($expected, $errors);
  847. }
  848. /**
  849. * Tests using validation methods from different providers and returning the error
  850. * as a string
  851. *
  852. * @return void
  853. */
  854. public function testErrorsFromCustomProvider()
  855. {
  856. $validator = new Validator();
  857. $validator
  858. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  859. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  860. $thing = $this->getMockBuilder('\stdClass')
  861. ->setMethods(['isCool'])
  862. ->getMock();
  863. $thing->expects($this->once())->method('isCool')
  864. ->will($this->returnCallback(function ($data, $context) use ($thing) {
  865. $this->assertEquals('bar', $data);
  866. $expected = [
  867. 'default' => new \Cake\Validation\RulesProvider,
  868. 'thing' => $thing
  869. ];
  870. $expected = [
  871. 'newRecord' => true,
  872. 'providers' => $expected,
  873. 'data' => [
  874. 'email' => '!',
  875. 'title' => 'bar'
  876. ],
  877. 'field' => 'title'
  878. ];
  879. $this->assertEquals($expected, $context);
  880. return "That ain't cool, yo";
  881. }));
  882. $validator->setProvider('thing', $thing);
  883. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  884. $expected = [
  885. 'email' => ['alpha' => 'The provided value is invalid'],
  886. 'title' => ['cool' => "That ain't cool, yo"]
  887. ];
  888. $this->assertEquals($expected, $errors);
  889. }
  890. /**
  891. * Tests that it is possible to pass extra arguments to the validation function
  892. * and it still gets the providers as last argument
  893. *
  894. * @return void
  895. */
  896. public function testMethodsWithExtraArguments()
  897. {
  898. $validator = new Validator();
  899. $validator->add('title', 'cool', [
  900. 'rule' => ['isCool', 'and', 'awesome'],
  901. 'provider' => 'thing'
  902. ]);
  903. $thing = $this->getMockBuilder('\stdClass')
  904. ->setMethods(['isCool'])
  905. ->getMock();
  906. $thing->expects($this->once())->method('isCool')
  907. ->will($this->returnCallback(function ($data, $a, $b, $context) use ($thing) {
  908. $this->assertEquals('bar', $data);
  909. $this->assertEquals('and', $a);
  910. $this->assertEquals('awesome', $b);
  911. $expected = [
  912. 'default' => new \Cake\Validation\RulesProvider,
  913. 'thing' => $thing
  914. ];
  915. $expected = [
  916. 'newRecord' => true,
  917. 'providers' => $expected,
  918. 'data' => [
  919. 'email' => '!',
  920. 'title' => 'bar'
  921. ],
  922. 'field' => 'title'
  923. ];
  924. $this->assertEquals($expected, $context);
  925. return "That ain't cool, yo";
  926. }));
  927. $validator->setProvider('thing', $thing);
  928. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  929. $expected = [
  930. 'title' => ['cool' => "That ain't cool, yo"]
  931. ];
  932. $this->assertEquals($expected, $errors);
  933. }
  934. /**
  935. * Tests that it is possible to use a closure as a rule
  936. *
  937. * @return void
  938. */
  939. public function testUsingClosureAsRule()
  940. {
  941. $validator = new Validator();
  942. $validator->add('name', 'myRule', [
  943. 'rule' => function ($data, $provider) {
  944. $this->assertEquals('foo', $data);
  945. return 'You fail';
  946. }
  947. ]);
  948. $expected = ['name' => ['myRule' => 'You fail']];
  949. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  950. }
  951. /**
  952. * Tests that setting last to a rule will stop validating the rest of the rules
  953. *
  954. * @return void
  955. */
  956. public function testErrorsWithLastRule()
  957. {
  958. $validator = new Validator();
  959. $validator
  960. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  961. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  962. $errors = $validator->errors(['email' => 'not an email!']);
  963. $expected = [
  964. 'email' => [
  965. 'alpha' => 'The provided value is invalid'
  966. ]
  967. ];
  968. $this->assertEquals($expected, $errors);
  969. }
  970. /**
  971. * Tests it is possible to get validation sets for a field using an array interface
  972. *
  973. * @return void
  974. */
  975. public function testArrayAccessGet()
  976. {
  977. $validator = new Validator();
  978. $validator
  979. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  980. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  981. $this->assertSame($validator['email'], $validator->field('email'));
  982. $this->assertSame($validator['title'], $validator->field('title'));
  983. }
  984. /**
  985. * Tests it is possible to check for validation sets for a field using an array interface
  986. *
  987. * @return void
  988. */
  989. public function testArrayAccessExists()
  990. {
  991. $validator = new Validator();
  992. $validator
  993. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  994. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  995. $this->assertArrayHasKey('email', $validator);
  996. $this->assertArrayHasKey('title', $validator);
  997. $this->assertArrayNotHasKey('foo', $validator);
  998. }
  999. /**
  1000. * Tests it is possible to set validation rules for a field using an array interface
  1001. *
  1002. * @return void
  1003. */
  1004. public function testArrayAccessSet()
  1005. {
  1006. $validator = new Validator();
  1007. $validator
  1008. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1009. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1010. $validator['name'] = $validator->field('title');
  1011. $this->assertSame($validator->field('title'), $validator->field('name'));
  1012. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  1013. $this->assertEquals($validator->field('email'), $validator->field('email'));
  1014. }
  1015. /**
  1016. * Tests it is possible to unset validation rules
  1017. *
  1018. * @return void
  1019. */
  1020. public function testArrayAccessUnset()
  1021. {
  1022. $validator = new Validator();
  1023. $validator
  1024. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1025. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1026. $this->assertArrayHasKey('title', $validator);
  1027. unset($validator['title']);
  1028. $this->assertArrayNotHasKey('title', $validator);
  1029. }
  1030. /**
  1031. * Tests the countable interface
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCount()
  1036. {
  1037. $validator = new Validator();
  1038. $validator
  1039. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1040. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1041. $this->assertCount(2, $validator);
  1042. }
  1043. /**
  1044. * Tests adding rules via alternative syntax
  1045. *
  1046. * @return void
  1047. */
  1048. public function testAddMultiple()
  1049. {
  1050. $validator = new Validator();
  1051. $validator->add('title', [
  1052. 'notBlank' => [
  1053. 'rule' => 'notBlank'
  1054. ],
  1055. 'length' => [
  1056. 'rule' => ['minLength', 10],
  1057. 'message' => 'Titles need to be at least 10 characters long'
  1058. ]
  1059. ]);
  1060. $set = $validator->field('title');
  1061. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  1062. $this->assertCount(2, $set);
  1063. }
  1064. /**
  1065. * Integration test for compareWith validator.
  1066. *
  1067. * @return void
  1068. */
  1069. public function testCompareWithIntegration()
  1070. {
  1071. $validator = new Validator();
  1072. $validator->add('password', [
  1073. 'compare' => [
  1074. 'rule' => ['compareWith', 'password_compare']
  1075. ],
  1076. ]);
  1077. $data = [
  1078. 'password' => 'test',
  1079. 'password_compare' => 'not the same'
  1080. ];
  1081. $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
  1082. }
  1083. /**
  1084. * Test debugInfo helper method.
  1085. *
  1086. * @return void
  1087. */
  1088. public function testDebugInfo()
  1089. {
  1090. $validator = new Validator();
  1091. $validator->setProvider('test', $this);
  1092. $validator->add('title', 'not-empty', ['rule' => 'notBlank']);
  1093. $validator->requirePresence('body');
  1094. $validator->allowEmpty('published');
  1095. $result = $validator->__debugInfo();
  1096. $expected = [
  1097. '_providers' => ['test'],
  1098. '_fields' => [
  1099. 'title' => [
  1100. 'isPresenceRequired' => false,
  1101. 'isEmptyAllowed' => false,
  1102. 'rules' => ['not-empty'],
  1103. ],
  1104. 'body' => [
  1105. 'isPresenceRequired' => true,
  1106. 'isEmptyAllowed' => false,
  1107. 'rules' => [],
  1108. ],
  1109. 'published' => [
  1110. 'isPresenceRequired' => false,
  1111. 'isEmptyAllowed' => true,
  1112. 'rules' => [],
  1113. ],
  1114. ],
  1115. '_presenceMessages' => [],
  1116. '_allowEmptyMessages' => [],
  1117. '_useI18n' => true,
  1118. ];
  1119. $this->assertEquals($expected, $result);
  1120. }
  1121. /**
  1122. * Tests that the 'create' and 'update' modes are preserved when using
  1123. * nested validators
  1124. *
  1125. * @return void
  1126. */
  1127. public function testNestedValidatorCreate()
  1128. {
  1129. $validator = new Validator();
  1130. $inner = new Validator();
  1131. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  1132. $validator->addNested('user', $inner);
  1133. $this->assertNotEmpty($validator->errors(['user' => ['username' => 'example']], true));
  1134. $this->assertEmpty($validator->errors(['user' => ['username' => 'a']], false));
  1135. }
  1136. /**
  1137. * Tests that the 'create' and 'update' modes are preserved when using
  1138. * nested validators
  1139. *
  1140. * @return void
  1141. */
  1142. public function testNestedManyValidatorCreate()
  1143. {
  1144. $validator = new Validator();
  1145. $inner = new Validator();
  1146. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  1147. $validator->addNestedMany('user', $inner);
  1148. $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
  1149. $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
  1150. }
  1151. /**
  1152. * Tests the notBlank proxy method
  1153. *
  1154. * @return void
  1155. */
  1156. public function testNotBlank()
  1157. {
  1158. $validator = new Validator();
  1159. $this->assertProxyMethod($validator, 'notBlank');
  1160. $this->assertNotEmpty($validator->errors(['username' => ' ']));
  1161. }
  1162. /**
  1163. * Tests the alphanumeric proxy method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testAlphanumeric()
  1168. {
  1169. $validator = new Validator();
  1170. $this->assertProxyMethod($validator, 'alphaNumeric');
  1171. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1172. }
  1173. /**
  1174. * Tests the lengthBetween proxy method
  1175. *
  1176. * @return void
  1177. */
  1178. public function testLengthBetween()
  1179. {
  1180. $validator = new Validator();
  1181. $this->assertProxyMethod($validator, 'lengthBetween', [5, 7], [5, 7]);
  1182. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1183. }
  1184. /**
  1185. * Tests the lengthBetween proxy method
  1186. *
  1187. * @return void
  1188. */
  1189. public function testLengthBetweenFailure()
  1190. {
  1191. $this->expectException(\InvalidArgumentException::class);
  1192. $validator = new Validator();
  1193. $validator->lengthBetween('username', [7]);
  1194. }
  1195. /**
  1196. * Tests the creditCard proxy method
  1197. *
  1198. * @return void
  1199. */
  1200. public function testCreditCard()
  1201. {
  1202. $validator = new Validator();
  1203. $this->assertProxyMethod($validator, 'creditCard', 'all', ['all', true], 'cc');
  1204. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1205. }
  1206. /**
  1207. * Tests the greaterThan proxy method
  1208. *
  1209. * @return void
  1210. */
  1211. public function testGreaterThan()
  1212. {
  1213. $validator = new Validator();
  1214. $this->assertProxyMethod($validator, 'greaterThan', 5, [Validation::COMPARE_GREATER, 5], 'comparison');
  1215. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1216. }
  1217. /**
  1218. * Tests the greaterThanOrEqual proxy method
  1219. *
  1220. * @return void
  1221. */
  1222. public function testGreaterThanOrEqual()
  1223. {
  1224. $validator = new Validator();
  1225. $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, [Validation::COMPARE_GREATER_OR_EQUAL, 5], 'comparison');
  1226. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1227. }
  1228. /**
  1229. * Tests the lessThan proxy method
  1230. *
  1231. * @return void
  1232. */
  1233. public function testLessThan()
  1234. {
  1235. $validator = new Validator();
  1236. $this->assertProxyMethod($validator, 'lessThan', 5, [Validation::COMPARE_LESS, 5], 'comparison');
  1237. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1238. }
  1239. /**
  1240. * Tests the lessThanOrEqual proxy method
  1241. *
  1242. * @return void
  1243. */
  1244. public function testLessThanOrEqual()
  1245. {
  1246. $validator = new Validator();
  1247. $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, [Validation::COMPARE_LESS_OR_EQUAL, 5], 'comparison');
  1248. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1249. }
  1250. /**
  1251. * Tests the equals proxy method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testEquals()
  1256. {
  1257. $validator = new Validator();
  1258. $this->assertProxyMethod($validator, 'equals', 5, [Validation::COMPARE_EQUAL, 5], 'comparison');
  1259. $this->assertEmpty($validator->errors(['username' => 5]));
  1260. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1261. }
  1262. /**
  1263. * Tests the notEquals proxy method
  1264. *
  1265. * @return void
  1266. */
  1267. public function testNotEquals()
  1268. {
  1269. $validator = new Validator();
  1270. $this->assertProxyMethod($validator, 'notEquals', 5, [Validation::COMPARE_NOT_EQUAL, 5], 'comparison');
  1271. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1272. }
  1273. /**
  1274. * Tests the sameAs proxy method
  1275. *
  1276. * @return void
  1277. */
  1278. public function testSameAs()
  1279. {
  1280. $validator = new Validator();
  1281. $this->assertProxyMethod($validator, 'sameAs', 'other', ['other', Validation::COMPARE_SAME], 'compareFields');
  1282. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1283. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => '1']));
  1284. }
  1285. /**
  1286. * Tests the notSameAs proxy method
  1287. *
  1288. * @return void
  1289. */
  1290. public function testNotSameAs()
  1291. {
  1292. $validator = new Validator();
  1293. $this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', Validation::COMPARE_NOT_SAME], 'compareFields');
  1294. $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
  1295. }
  1296. /**
  1297. * Tests the equalToField proxy method
  1298. *
  1299. * @return void
  1300. */
  1301. public function testEqualToField()
  1302. {
  1303. $validator = new Validator();
  1304. $this->assertProxyMethod($validator, 'equalToField', 'other', ['other', Validation::COMPARE_EQUAL], 'compareFields');
  1305. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1306. $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'bar']));
  1307. }
  1308. /**
  1309. * Tests the notEqualToField proxy method
  1310. *
  1311. * @return void
  1312. */
  1313. public function testNotEqualToField()
  1314. {
  1315. $validator = new Validator();
  1316. $this->assertProxyMethod($validator, 'notEqualToField', 'other', ['other', Validation::COMPARE_NOT_EQUAL], 'compareFields');
  1317. $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
  1318. }
  1319. /**
  1320. * Tests the greaterThanField proxy method
  1321. *
  1322. * @return void
  1323. */
  1324. public function testGreaterThanField()
  1325. {
  1326. $validator = new Validator();
  1327. $this->assertProxyMethod($validator, 'greaterThanField', 'other', ['other', Validation::COMPARE_GREATER], 'compareFields');
  1328. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
  1329. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
  1330. }
  1331. /**
  1332. * Tests the greaterThanOrEqualToField proxy method
  1333. *
  1334. * @return void
  1335. */
  1336. public function testGreaterThanOrEqualToField()
  1337. {
  1338. $validator = new Validator();
  1339. $this->assertProxyMethod($validator, 'greaterThanOrEqualToField', 'other', ['other', Validation::COMPARE_GREATER_OR_EQUAL], 'compareFields');
  1340. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
  1341. }
  1342. /**
  1343. * Tests the lessThanField proxy method
  1344. *
  1345. * @return void
  1346. */
  1347. public function testLessThanField()
  1348. {
  1349. $validator = new Validator();
  1350. $this->assertProxyMethod($validator, 'lessThanField', 'other', ['other', Validation::COMPARE_LESS], 'compareFields');
  1351. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
  1352. $this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
  1353. }
  1354. /**
  1355. * Tests the lessThanOrEqualToField proxy method
  1356. *
  1357. * @return void
  1358. */
  1359. public function testLessThanOrEqualToField()
  1360. {
  1361. $validator = new Validator();
  1362. $this->assertProxyMethod($validator, 'lessThanOrEqualToField', 'other', ['other', Validation::COMPARE_LESS_OR_EQUAL], 'compareFields');
  1363. $this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
  1364. }
  1365. /**
  1366. * Tests the containsNonAlphaNumeric proxy method
  1367. *
  1368. * @return void
  1369. */
  1370. public function testContainsNonAlphaNumeric()
  1371. {
  1372. $validator = new Validator();
  1373. $this->assertProxyMethod($validator, 'containsNonAlphaNumeric', 2, [2]);
  1374. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1375. }
  1376. /**
  1377. * Tests the date proxy method
  1378. *
  1379. * @return void
  1380. */
  1381. public function testDate()
  1382. {
  1383. $validator = new Validator();
  1384. $this->assertProxyMethod($validator, 'date', ['ymd'], [['ymd']]);
  1385. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1386. }
  1387. /**
  1388. * Tests the dateTime proxy method
  1389. *
  1390. * @return void
  1391. */
  1392. public function testDateTime()
  1393. {
  1394. $validator = new Validator();
  1395. $this->assertProxyMethod($validator, 'dateTime', ['ymd'], [['ymd']], 'datetime');
  1396. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1397. }
  1398. /**
  1399. * Tests the time proxy method
  1400. *
  1401. * @return void
  1402. */
  1403. public function testTime()
  1404. {
  1405. $validator = new Validator();
  1406. $this->assertProxyMethod($validator, 'time');
  1407. $this->assertNotEmpty($validator->errors(['username' => 'not a time']));
  1408. }
  1409. /**
  1410. * Tests the localizedTime proxy method
  1411. *
  1412. * @return void
  1413. */
  1414. public function testLocalizedTime()
  1415. {
  1416. $validator = new Validator();
  1417. $this->assertProxyMethod($validator, 'localizedTime', 'date', ['date']);
  1418. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1419. }
  1420. /**
  1421. * Tests the boolean proxy method
  1422. *
  1423. * @return void
  1424. */
  1425. public function testBoolean()
  1426. {
  1427. $validator = new Validator();
  1428. $this->assertProxyMethod($validator, 'boolean');
  1429. $this->assertNotEmpty($validator->errors(['username' => 'not a boolean']));
  1430. }
  1431. /**
  1432. * Tests the decimal proxy method
  1433. *
  1434. * @return void
  1435. */
  1436. public function testDecimal()
  1437. {
  1438. $validator = new Validator();
  1439. $this->assertProxyMethod($validator, 'decimal', 2, [2]);
  1440. $this->assertNotEmpty($validator->errors(['username' => 10.1]));
  1441. }
  1442. /**
  1443. * Tests the ip proxy methods
  1444. *
  1445. * @return void
  1446. */
  1447. public function testIps()
  1448. {
  1449. $validator = new Validator();
  1450. $this->assertProxyMethod($validator, 'ip');
  1451. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1452. $this->assertProxyMethod($validator, 'ipv4', null, ['ipv4'], 'ip');
  1453. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1454. $this->assertProxyMethod($validator, 'ipv6', null, ['ipv6'], 'ip');
  1455. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1456. }
  1457. /**
  1458. * Tests the minLength proxy method
  1459. *
  1460. * @return void
  1461. */
  1462. public function testMinLength()
  1463. {
  1464. $validator = new Validator();
  1465. $this->assertProxyMethod($validator, 'minLength', 2, [2]);
  1466. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1467. }
  1468. /**
  1469. * Tests the minLengthBytes proxy method
  1470. *
  1471. * @return void
  1472. */
  1473. public function testMinLengthBytes()
  1474. {
  1475. $validator = new Validator();
  1476. $this->assertProxyMethod($validator, 'minLengthBytes', 11, [11]);
  1477. $this->assertNotEmpty($validator->errors(['username' => 'ÆΔΩЖÇ']));
  1478. }
  1479. /**
  1480. * Tests the maxLength proxy method
  1481. *
  1482. * @return void
  1483. */
  1484. public function testMaxLength()
  1485. {
  1486. $validator = new Validator();
  1487. $this->assertProxyMethod($validator, 'maxLength', 2, [2]);
  1488. $this->assertNotEmpty($validator->errors(['username' => 'aaa']));
  1489. }
  1490. /**
  1491. * Tests the maxLengthBytes proxy method
  1492. *
  1493. * @return void
  1494. */
  1495. public function testMaxLengthBytes()
  1496. {
  1497. $validator = new Validator();
  1498. $this->assertProxyMethod($validator, 'maxLengthBytes', 9, [9]);
  1499. $this->assertNotEmpty($validator->errors(['username' => 'ÆΔΩЖÇ']));
  1500. }
  1501. /**
  1502. * Tests the numeric proxy method
  1503. *
  1504. * @return void
  1505. */
  1506. public function testNumeric()
  1507. {
  1508. $validator = new Validator();
  1509. $this->assertProxyMethod($validator, 'numeric');
  1510. $this->assertEmpty($validator->errors(['username' => '22']));
  1511. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1512. }
  1513. /**
  1514. * Tests the naturalNumber proxy method
  1515. *
  1516. * @return void
  1517. */
  1518. public function testNaturalNumber()
  1519. {
  1520. $validator = new Validator();
  1521. $this->assertProxyMethod($validator, 'naturalNumber', null, [false]);
  1522. $this->assertNotEmpty($validator->errors(['username' => 0]));
  1523. }
  1524. /**
  1525. * Tests the nonNegativeInteger proxy method
  1526. *
  1527. * @return void
  1528. */
  1529. public function testNonNegativeInteger()
  1530. {
  1531. $validator = new Validator();
  1532. $this->assertProxyMethod($validator, 'nonNegativeInteger', null, [true], 'naturalNumber');
  1533. $this->assertNotEmpty($validator->errors(['username' => -1]));
  1534. }
  1535. /**
  1536. * Tests the range proxy method
  1537. *
  1538. * @return void
  1539. */
  1540. public function testRange()
  1541. {
  1542. $validator = new Validator();
  1543. $this->assertProxyMethod($validator, 'range', [1, 4], [1, 4]);
  1544. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1545. }
  1546. /**
  1547. * Tests the range failure case
  1548. *
  1549. * @return void
  1550. */
  1551. public function testRangeFailure()
  1552. {
  1553. $this->expectException(\InvalidArgumentException::class);
  1554. $validator = new Validator();
  1555. $validator->range('username', [1]);
  1556. }
  1557. /**
  1558. * Tests the url proxy method
  1559. *
  1560. * @return void
  1561. */
  1562. public function testUrl()
  1563. {
  1564. $validator = new Validator();
  1565. $this->assertProxyMethod($validator, 'url', null, [false]);
  1566. $this->assertNotEmpty($validator->errors(['username' => 'not url']));
  1567. }
  1568. /**
  1569. * Tests the urlWithProtocol proxy method
  1570. *
  1571. * @return void
  1572. */
  1573. public function testUrlWithProtocol()
  1574. {
  1575. $validator = new Validator();
  1576. $this->assertProxyMethod($validator, 'urlWithProtocol', null, [true], 'url');
  1577. $this->assertNotEmpty($validator->errors(['username' => 'google.com']));
  1578. }
  1579. /**
  1580. * Tests the inList proxy method
  1581. *
  1582. * @return void
  1583. */
  1584. public function testInList()
  1585. {
  1586. $validator = new Validator();
  1587. $this->assertProxyMethod($validator, 'inList', ['a', 'b'], [['a', 'b']]);
  1588. $this->assertNotEmpty($validator->errors(['username' => 'c']));
  1589. }
  1590. /**
  1591. * Tests the uuid proxy method
  1592. *
  1593. * @return void
  1594. */
  1595. public function testUuid()
  1596. {
  1597. $validator = new Validator();
  1598. $this->assertProxyMethod($validator, 'uuid');
  1599. $this->assertNotEmpty($validator->errors(['username' => 'not uuid']));
  1600. }
  1601. /**
  1602. * Tests the uploadedFile proxy method
  1603. *
  1604. * @return void
  1605. */
  1606. public function testUploadedFile()
  1607. {
  1608. $validator = new Validator();
  1609. $this->assertProxyMethod($validator, 'uploadedFile', ['foo' => 'bar'], [['foo' => 'bar']]);
  1610. $this->assertNotEmpty($validator->errors(['username' => []]));
  1611. }
  1612. /**
  1613. * Tests the latlog proxy methods
  1614. *
  1615. * @return void
  1616. */
  1617. public function testLatLong()
  1618. {
  1619. $validator = new Validator();
  1620. $this->assertProxyMethod($validator, 'latLong', null, [], 'geoCoordinate');
  1621. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1622. $this->assertProxyMethod($validator, 'latitude');
  1623. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1624. $this->assertProxyMethod($validator, 'longitude');
  1625. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1626. }
  1627. /**
  1628. * Tests the ascii proxy method
  1629. *
  1630. * @return void
  1631. */
  1632. public function testAscii()
  1633. {
  1634. $validator = new Validator();
  1635. $this->assertProxyMethod($validator, 'ascii');
  1636. $this->assertNotEmpty($validator->errors(['username' => 'ü']));
  1637. }
  1638. /**
  1639. * Tests the utf8 proxy methods
  1640. *
  1641. * @return void
  1642. */
  1643. public function testUtf8()
  1644. {
  1645. // Grinning face
  1646. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1647. $validator = new Validator();
  1648. $this->assertProxyMethod($validator, 'utf8', null, [['extended' => false]]);
  1649. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1650. $this->assertNotEmpty($validator->errors(['username' => $extended]));
  1651. }
  1652. /**
  1653. * Test utf8extended proxy method.
  1654. *
  1655. * @return void
  1656. */
  1657. public function testUtf8Extended()
  1658. {
  1659. // Grinning face
  1660. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1661. $validator = new Validator();
  1662. $this->assertProxyMethod($validator, 'utf8Extended', null, [['extended' => true]], 'utf8');
  1663. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1664. $this->assertEmpty($validator->errors(['username' => $extended]));
  1665. }
  1666. /**
  1667. * Tests the email proxy method
  1668. *
  1669. * @return void
  1670. */
  1671. public function testEmail()
  1672. {
  1673. $validator = new Validator();
  1674. $validator->email('username');
  1675. $this->assertEmpty($validator->errors(['username' => 'test@example.com']));
  1676. $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
  1677. }
  1678. /**
  1679. * Tests the integer proxy method
  1680. *
  1681. * @return void
  1682. */
  1683. public function testInteger()
  1684. {
  1685. $validator = new Validator();
  1686. $this->assertProxyMethod($validator, 'integer', null, [], 'isInteger');
  1687. $this->assertNotEmpty($validator->errors(['username' => 'not integer']));
  1688. }
  1689. /**
  1690. * Tests the isArray proxy method
  1691. *
  1692. * @return void
  1693. */
  1694. public function testIsArray()
  1695. {
  1696. $validator = new Validator();
  1697. $validator->isArray('username');
  1698. $this->assertEmpty($validator->errors(['username' => [1, 2, 3]]));
  1699. $this->assertNotEmpty($validator->errors(['username' => 'is not an array']));
  1700. }
  1701. /**
  1702. * Tests the scalar proxy method
  1703. *
  1704. * @return void
  1705. */
  1706. public function testScalar()
  1707. {
  1708. $validator = new Validator();
  1709. $validator->scalar('username');
  1710. $this->assertEmpty($validator->errors(['username' => 'scalar']));
  1711. $this->assertNotEmpty($validator->errors(['username' => ['array']]));
  1712. }
  1713. /**
  1714. * Tests the hexColor proxy method
  1715. *
  1716. * @return void
  1717. */
  1718. public function testHexColor()
  1719. {
  1720. $validator = new Validator();
  1721. $this->assertProxyMethod($validator, 'hexColor');
  1722. $this->assertEmpty($validator->errors(['username' => '#FFFFFF']));
  1723. $this->assertNotEmpty($validator->errors(['username' => 'FFFFFF']));
  1724. }
  1725. /**
  1726. * Tests the multiple proxy method
  1727. *
  1728. * @return void
  1729. */
  1730. public function testMultiple()
  1731. {
  1732. $validator = new Validator();
  1733. $this->assertProxyMethod(
  1734. $validator,
  1735. 'multipleOptions',
  1736. ['min' => 1, 'caseInsensitive' => true],
  1737. [['min' => 1], true],
  1738. 'multiple'
  1739. );
  1740. $this->assertProxyMethod(
  1741. $validator,
  1742. 'multipleOptions',
  1743. ['min' => 1, 'caseInsensitive' => false],
  1744. [['min' => 1], false],
  1745. 'multiple'
  1746. );
  1747. $this->assertNotEmpty($validator->errors(['username' => '']));
  1748. }
  1749. /**
  1750. * Tests the hasAtLeast method
  1751. *
  1752. * @return void
  1753. */
  1754. public function testHasAtLeast()
  1755. {
  1756. $validator = new Validator();
  1757. $validator->hasAtLeast('things', 3);
  1758. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1759. $this->assertEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1760. $this->assertNotEmpty($validator->errors(['things' => [1, 2]]));
  1761. $this->assertNotEmpty($validator->errors(['things' => []]));
  1762. $this->assertNotEmpty($validator->errors(['things' => 'string']));
  1763. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1764. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1765. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1766. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => []]]));
  1767. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => 'string']]));
  1768. }
  1769. /**
  1770. * Tests the hasAtMost method
  1771. *
  1772. * @return void
  1773. */
  1774. public function testHasAtMost()
  1775. {
  1776. $validator = new Validator();
  1777. $validator->hasAtMost('things', 3);
  1778. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1779. $this->assertEmpty($validator->errors(['things' => [1]]));
  1780. $this->assertNotEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1781. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1782. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1783. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1784. }
  1785. /**
  1786. * Tests the regex proxy method
  1787. *
  1788. * @return void
  1789. */
  1790. public function testRegex()
  1791. {
  1792. $validator = new Validator();
  1793. $this->assertProxyMethod($validator, 'regex', '/(?<!\\S)\\d++(?!\\S)/', ['/(?<!\\S)\\d++(?!\\S)/'], 'custom');
  1794. $this->assertEmpty($validator->errors(['username' => '123']));
  1795. $this->assertNotEmpty($validator->errors(['username' => 'Foo']));
  1796. }
  1797. /**
  1798. * Tests that a rule in the Validator class exists and was configured as expected.
  1799. *
  1800. * @param Validator $validator
  1801. * @param string $method
  1802. * @param mixed $extra
  1803. * @param array $pass
  1804. * @param string|null $name
  1805. */
  1806. protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
  1807. {
  1808. $name = $name ?: $method;
  1809. if ($extra !== null) {
  1810. $this->assertSame($validator, $validator->{$method}('username', $extra));
  1811. } else {
  1812. $this->assertSame($validator, $validator->{$method}('username'));
  1813. }
  1814. $rule = $validator->field('username')->rule($method);
  1815. $this->assertNotEmpty($rule, "Rule was not found for $method");
  1816. $this->assertNull($rule->get('message'), 'Message is present when it should not be');
  1817. $this->assertNull($rule->get('on'), 'On clause is present when it should not be');
  1818. $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match');
  1819. $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different');
  1820. $this->assertEquals('default', $rule->get('provider'), 'Provider does not match');
  1821. if ($extra !== null) {
  1822. $validator->{$method}('username', $extra, 'the message', 'create');
  1823. } else {
  1824. $validator->{$method}('username', 'the message', 'create');
  1825. }
  1826. $rule = $validator->field('username')->rule($method);
  1827. $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same');
  1828. $this->assertEquals('create', $rule->get('on'), 'On clause is wrong');
  1829. }
  1830. /**
  1831. * Testing adding DefaultProvider
  1832. *
  1833. * @return void
  1834. */
  1835. public function testAddingDefaultProvider()
  1836. {
  1837. $validator = new Validator();
  1838. $this->assertEmpty($validator->providers(), 'Providers should be empty');
  1839. Validator::addDefaultProvider('test-provider', 'MyNameSpace\Validation\MyProvider');
  1840. $validator = new Validator();
  1841. $this->assertEquals($validator->providers(), ['test-provider'], 'Default provider `test-provider` is missing');
  1842. }
  1843. /**
  1844. * Testing getting DefaultProvider(s)
  1845. *
  1846. * @return void
  1847. */
  1848. public function testGetDefaultProvider()
  1849. {
  1850. Validator::addDefaultProvider('test-provider', 'MyNameSpace\Validation\MyProvider');
  1851. $this->assertEquals(Validator::getDefaultProvider('test-provider'), 'MyNameSpace\Validation\MyProvider', 'Default provider `test-provider` is missing');
  1852. $this->assertNull(Validator::getDefaultProvider('invalid-provider'), 'Default provider (`invalid-provider`) should be missing');
  1853. $this->assertNull(Validator::getDefaultProvider(null), 'Default provider (null) should be missing');
  1854. Validator::addDefaultProvider('test-provider2', 'MyNameSpace\Validation\MySecondProvider');
  1855. $this->assertEquals(Validator::getDefaultProviders(), ['test-provider', 'test-provider2'], 'Default providers incorrect');
  1856. }
  1857. }