ValidatorsTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Command;
  11. use Sonata\AdminBundle\Command\Validators;
  12. /**
  13. * @author Andrej Hudec <pulzarraider@gmail.com>
  14. */
  15. class ValidatorsTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @dataProvider getValidateUsernameTests
  19. */
  20. public function testValidateUsername($expected, $value)
  21. {
  22. $this->assertSame($expected, Validators::validateUsername($value));
  23. }
  24. public function getValidateUsernameTests()
  25. {
  26. return array(
  27. array('Foo', 'Foo'),
  28. array('abcdefghijklmnopqrstuvwxyz.ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789', 'abcdefghijklmnopqrstuvwxyz.ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'),
  29. );
  30. }
  31. /**
  32. * @dataProvider getValidateUsernameWithExceptionTests
  33. */
  34. public function testValidateUsernameWithException($value)
  35. {
  36. $this->setExpectedException('\InvalidArgumentException');
  37. Validators::validateUsername($value);
  38. }
  39. public function getValidateUsernameWithExceptionTests()
  40. {
  41. return array(
  42. array(null),
  43. );
  44. }
  45. /**
  46. * @dataProvider getValidateEntityNameTests
  47. */
  48. public function testValidateEntityName($expected, $value)
  49. {
  50. $this->assertSame($expected, Validators::validateEntityName($value));
  51. }
  52. public function getValidateEntityNameTests()
  53. {
  54. return array(
  55. array(array('AcmeBlogBundle', 'Post'), 'AcmeBlogBundle:Post'),
  56. array(array('Foo\Bar\BlogBundle', 'Post'), 'Foo/Bar/BlogBundle:Post'),
  57. array(array('Foo\Bar\BlogBundle', 'Post'), 'Foo\Bar\BlogBundle:Post'),
  58. );
  59. }
  60. /**
  61. * @dataProvider getValidateEntityNamesWithExceptionTests
  62. */
  63. public function testValidateEntityNameWithException($value)
  64. {
  65. $this->setExpectedException('\InvalidArgumentException');
  66. Validators::validateEntityName($value);
  67. }
  68. public function getValidateEntityNamesWithExceptionTests()
  69. {
  70. return array(
  71. array('Sonata\AdminBundle\Admin\AbstractAdmin'),
  72. array('Sonata/AdminBundle/Admin/Admin'),
  73. array('Foo/Bar/Controller'),
  74. array('Foo/BarController'),
  75. array('Foo_Bar'),
  76. array('FooBarController'),
  77. array('FooBarAdmin'),
  78. );
  79. }
  80. /**
  81. * @dataProvider getValidateClassTests
  82. */
  83. public function testValidateClass($expected, $value)
  84. {
  85. $this->assertSame($expected, Validators::validateClass($value));
  86. }
  87. public function getValidateClassTests()
  88. {
  89. return array(
  90. array('Sonata\AdminBundle\Admin\AbstractAdmin', 'Sonata\AdminBundle\Admin\AbstractAdmin'),
  91. array('Sonata\AdminBundle\Admin\AbstractAdmin', 'Sonata/AdminBundle/Admin/AbstractAdmin'),
  92. );
  93. }
  94. /**
  95. * @dataProvider getValidateClassWithExceptionTests
  96. */
  97. public function testValidateClassWithException($value)
  98. {
  99. $this->setExpectedException('\InvalidArgumentException');
  100. Validators::validateClass($value);
  101. }
  102. public function getValidateClassWithExceptionTests()
  103. {
  104. return array(
  105. array('Foo:BarAdmin'),
  106. array('Foo:Bar:Admin'),
  107. array('Foo/Bar/Admin'),
  108. );
  109. }
  110. /**
  111. * @dataProvider getValidateAdminClassBasenameTests
  112. */
  113. public function testValidateAdminClassBasename($expected, $value)
  114. {
  115. $this->assertSame($expected, Validators::validateAdminClassBasename($value));
  116. }
  117. public function getValidateAdminClassBasenameTests()
  118. {
  119. return array(
  120. array('FooBarAdmin', 'FooBarAdmin'),
  121. array('Foo\Foo\BarAdmin', 'Foo\Foo\BarAdmin'),
  122. array('Foo\Foo\BarAdmin', 'Foo/Foo/BarAdmin'),
  123. );
  124. }
  125. /**
  126. * @dataProvider getValidateAdminClassBasenameWithExceptionTests
  127. */
  128. public function testValidateAdminClassBasenameWithException($value)
  129. {
  130. $this->setExpectedException('\InvalidArgumentException');
  131. Validators::validateAdminClassBasename($value);
  132. }
  133. public function getValidateAdminClassBasenameWithExceptionTests()
  134. {
  135. return array(
  136. array('Foo:BarAdmin'),
  137. array('Foo:Bar:Admin'),
  138. array('*+-!:@&^%'),
  139. );
  140. }
  141. /**
  142. * @dataProvider getValidateControllerClassBasenameTests
  143. */
  144. public function testValidateControllerClassBasename($expected, $value)
  145. {
  146. $this->assertSame($expected, Validators::validateControllerClassBasename($value));
  147. }
  148. public function getValidateControllerClassBasenameTests()
  149. {
  150. return array(
  151. array('FooBarController', 'FooBarController'),
  152. array('Foo\Foo\BarController', 'Foo/Foo/BarController'),
  153. array('Foo\Foo\BarController', 'Foo\Foo\BarController'),
  154. );
  155. }
  156. /**
  157. * @dataProvider getValidateControllerClassBasenameWithExceptionTests
  158. */
  159. public function testValidateControllerClassBasenameWithException($value)
  160. {
  161. $this->setExpectedException('\InvalidArgumentException');
  162. Validators::validateControllerClassBasename($value);
  163. }
  164. public function getValidateControllerClassBasenameWithExceptionTests()
  165. {
  166. return array(
  167. array(' foobar '),
  168. array(' FooBar'),
  169. array('Foo Bar'),
  170. array('Foo-Bar'),
  171. array('foo*'),
  172. array('foo+'),
  173. array('foo-'),
  174. array('foo!'),
  175. array('foo@'),
  176. array('foo&'),
  177. array('foo%'),
  178. array('foo^'),
  179. array('foo(bar)'),
  180. array('foo[bar]'),
  181. array('foo{bar}'),
  182. array('Foo/Bar'),
  183. array('Foo\Bar'),
  184. array('Foo/BarControllr'),
  185. array('Foo\BarControllr'),
  186. array('Foo:BarControllr'),
  187. );
  188. }
  189. /**
  190. * @dataProvider getValidateServicesFileTests
  191. */
  192. public function testValidateServicesFile($expected, $value)
  193. {
  194. $this->assertSame($expected, Validators::validateServicesFile($value));
  195. }
  196. public function getValidateServicesFileTests()
  197. {
  198. return array(
  199. array('foobar', 'foobar'),
  200. array('fooBar', 'fooBar'),
  201. array(' foo Bar ', ' foo Bar '),
  202. array('Foo/Bar', '/Foo/Bar/'),
  203. array('Foo/BAR', '/Foo/BAR/'),
  204. array('Foo/Bar', '/Foo/Bar'),
  205. array('Foo/Bar', 'Foo/Bar/'),
  206. );
  207. }
  208. /**
  209. * @dataProvider getValidateServiceIdTests
  210. */
  211. public function testValidateServiceId($value)
  212. {
  213. $this->assertSame($value, Validators::validateServiceId($value));
  214. }
  215. public function getValidateServiceIdTests()
  216. {
  217. return array(
  218. array('abcdefghijklmnopqrstuvwxyz.ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'),
  219. array('Foo_Bar_0123'),
  220. array('Foo.Bar.0123'),
  221. );
  222. }
  223. /**
  224. * @dataProvider getValidateServiceIdWithExceptionTests
  225. */
  226. public function testValidateServiceIdWithException($value)
  227. {
  228. $this->setExpectedException('\InvalidArgumentException');
  229. Validators::validateServiceId($value);
  230. }
  231. public function getValidateServiceIdWithExceptionTests()
  232. {
  233. return array(
  234. array(' foobar '),
  235. array(' FooBar'),
  236. array('Foo Bar'),
  237. array('Foo-Bar'),
  238. array('foo*'),
  239. array('foo+'),
  240. array('foo-'),
  241. array('foo!'),
  242. array('foo@'),
  243. array('foo&'),
  244. array('foo%'),
  245. array('foo^'),
  246. array('foo:'),
  247. array('foo(bar)'),
  248. array('foo[bar]'),
  249. array('foo{bar}'),
  250. array('Foo/Bar'),
  251. array('Foo\Bar'),
  252. );
  253. }
  254. }