PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Validator/TimezoneTest.php

http://github.com/zendframework/zf2
PHP | 332 lines | 173 code | 37 blank | 122 comment | 2 complexity | af45aebea5c809c8995f35f7aa86a7dc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Validator;
  10. use Zend\Validator\Timezone;
  11. /**
  12. * Tests for {@see \Zend\Validator\Timezone}
  13. *
  14. * @covers \Zend\Validator\Timezone
  15. */
  16. class TimezoneTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var Timezone
  20. */
  21. protected $validator;
  22. public function setUp()
  23. {
  24. $this->validator = new Timezone();
  25. }
  26. /**
  27. * Test locations
  28. *
  29. * @return void
  30. *
  31. * @dataProvider locationProvider
  32. */
  33. public function testLocations($value, $valid)
  34. {
  35. $this->validator->setType(Timezone::LOCATION);
  36. $this->checkValidationValue($value, $valid);
  37. }
  38. /**
  39. * Test locations by type is string
  40. *
  41. * @return void
  42. *
  43. * @dataProvider locationProvider
  44. */
  45. public function testLocationsByTypeAsString($value, $valid)
  46. {
  47. $this->validator->setType('location');
  48. $this->checkValidationValue($value, $valid);
  49. }
  50. /**
  51. * Provides location values
  52. *
  53. * @return array
  54. */
  55. public function locationProvider()
  56. {
  57. return array(
  58. array('America/Anguilla', true),
  59. array('Antarctica/Palmer', true),
  60. array('Asia/Dubai', true),
  61. array('Atlantic/Cape_Verde', true),
  62. array('Australia/Broken_Hill', true),
  63. array('America/Sao_Paulo', true),
  64. array('America/Toronto', true),
  65. array('Pacific/Easter', true),
  66. array('Europe/Copenhagen', true),
  67. array('Indian/Maldives', true),
  68. array('anast', false), // abbreviation of Anadyr Summer Time
  69. array('Asia/London', false), // wrong location
  70. array('', false), // empty string
  71. array(null, false), // null value
  72. );
  73. }
  74. /**
  75. * Test abbreviations
  76. *
  77. * @return void
  78. *
  79. * @dataProvider abbreviationProvider
  80. */
  81. public function testAbbreviations($value, $valid)
  82. {
  83. $this->validator->setType(Timezone::ABBREVIATION);
  84. $this->checkValidationValue($value, $valid);
  85. }
  86. /**
  87. * Test abbreviations byTypeAsString
  88. *
  89. * @return void
  90. *
  91. * @dataProvider abbreviationProvider
  92. */
  93. public function testAbbreviationsByTypeAsString($value, $valid)
  94. {
  95. $this->validator->setType('abbreviation');
  96. $this->checkValidationValue($value, $valid);
  97. }
  98. /**
  99. * Provides abbreviation values
  100. *
  101. * @return array
  102. */
  103. public function abbreviationProvider()
  104. {
  105. return array(
  106. array('anast', true), // Anadyr Summer Time
  107. array('bnt', true), // Brunei Darussalam Time
  108. array('cest', true), // Central European Summer Time
  109. array('easst', true), // Easter Island Summer Time
  110. array('egst', true), // Eastern Greenland Summer Time
  111. array('hkt', true), // Hong Kong Time
  112. array('irkst', true), // Irkutsk Summer Time
  113. array('krast', true), // Krasnoyarsk Summer Time
  114. array('nzdt', true), // New Zealand Daylight Time
  115. array('sast', true), // South Africa Standard Time
  116. array('America/Toronto', false), // location
  117. array('xyz', false), // wrong abbreviation
  118. array('', false), // empty string
  119. array(null, false), // null value
  120. );
  121. }
  122. /**
  123. * Test locations and abbreviations
  124. *
  125. * @return void
  126. *
  127. * @dataProvider locationAndAbbreviationProvider
  128. */
  129. public function testlocationsAndAbbreviationsWithAllTypeAsString($value, $valid)
  130. {
  131. $this->validator->setType(Timezone::ALL);
  132. $this->checkValidationValue($value, $valid);
  133. }
  134. /**
  135. * Test locations and abbreviations
  136. *
  137. * @return void
  138. *
  139. * @dataProvider locationAndAbbreviationProvider
  140. */
  141. public function testlocationsAndAbbreviationsWithAllTypeAsArray($value, $valid)
  142. {
  143. $this->validator->setType(array(Timezone::LOCATION, Timezone::ABBREVIATION));
  144. $this->checkValidationValue($value, $valid);
  145. }
  146. /**
  147. * Test locations and abbreviations
  148. *
  149. * @return void
  150. *
  151. * @dataProvider locationAndAbbreviationProvider
  152. */
  153. public function testlocationsAndAbbreviationsWithAllTypeAsArrayWithStrings($value, $valid)
  154. {
  155. $this->validator->setType(array('location', 'abbreviation'));
  156. $this->checkValidationValue($value, $valid);
  157. }
  158. /**
  159. * Provides location and abbreviation values
  160. *
  161. * @return array
  162. */
  163. public function locationAndAbbreviationProvider()
  164. {
  165. return array(
  166. array('America/Anguilla', true),
  167. array('Antarctica/Palmer', true),
  168. array('Asia/Dubai', true),
  169. array('Atlantic/Cape_Verde', true),
  170. array('Australia/Broken_Hill', true),
  171. array('hkt', true), // Hong Kong Time
  172. array('irkst', true), // Irkutsk Summer Time
  173. array('krast', true), // Krasnoyarsk Summer Time
  174. array('nzdt', true), // New Zealand Daylight Time
  175. array('sast', true), // South Africa Standard Time
  176. array('xyz', false), // wrong abbreviation
  177. array('Asia/London', false), // wrong location
  178. array('', false), // empty string
  179. array(null, false), // null value
  180. );
  181. }
  182. /**
  183. * Test wrong type
  184. *
  185. * @return void
  186. *
  187. * @dataProvider wrongTypesProvider
  188. */
  189. public function testWrongType($value)
  190. {
  191. $this->checkExpectedException($value);
  192. }
  193. /**
  194. * Provides wrong types
  195. *
  196. * @return array
  197. */
  198. public function wrongTypesProvider()
  199. {
  200. return array(
  201. array(null),
  202. array(''),
  203. array(array()),
  204. array(0),
  205. array(4),
  206. );
  207. }
  208. /**
  209. * Test pass `type` option through constructor
  210. *
  211. * @return void
  212. */
  213. public function testTypeThroughConstructor()
  214. {
  215. $timezone1 = new Timezone(Timezone::LOCATION);
  216. $this->assertTrue($timezone1->isValid('Asia/Dubai'));
  217. $this->assertFalse($timezone1->isValid('sast'));
  218. $timezone2 = new Timezone('location');
  219. $this->assertTrue($timezone2->isValid('Asia/Dubai'));
  220. $this->assertFalse($timezone2->isValid('sast'));
  221. $timezone3 = new Timezone(array('type' => 'location'));
  222. $this->assertTrue($timezone3->isValid('Asia/Dubai'));
  223. $this->assertFalse($timezone3->isValid('sast'));
  224. $timezone4 = new Timezone(Timezone::ABBREVIATION);
  225. $this->assertFalse($timezone4->isValid('Asia/Dubai'));
  226. $this->assertTrue($timezone4->isValid('sast'));
  227. $timezone5 = new Timezone('abbreviation');
  228. $this->assertFalse($timezone5->isValid('Asia/Dubai'));
  229. $this->assertTrue($timezone5->isValid('sast'));
  230. $timezone6 = new Timezone(array('type' => 'abbreviation'));
  231. $this->assertFalse($timezone6->isValid('Asia/Dubai'));
  232. $this->assertTrue($timezone6->isValid('sast'));
  233. // default value is `all`
  234. $timezone7 = new Timezone();
  235. $this->assertTrue($timezone7->isValid('Asia/Dubai'));
  236. $this->assertTrue($timezone7->isValid('sast'));
  237. $timezone8 = new Timezone(array('type' => array('location', 'abbreviation')));
  238. $this->assertTrue($timezone8->isValid('Asia/Dubai'));
  239. $this->assertTrue($timezone8->isValid('sast'));
  240. }
  241. /**
  242. * @param mixed $invalidType
  243. *
  244. * @dataProvider getInvalidTypes
  245. */
  246. public function testRejectsInvalidIntType($invalidType)
  247. {
  248. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
  249. new Timezone(array('type' => $invalidType));
  250. }
  251. /**
  252. * Checks that the validation value matches the expected validity
  253. *
  254. * @param mixed $value Value to validate
  255. * @param bool $valid Expected validity
  256. *
  257. * @return void
  258. */
  259. protected function checkValidationValue($value, $valid)
  260. {
  261. $isValid = $this->validator->isValid($value);
  262. if ($valid) {
  263. $this->assertTrue($isValid);
  264. } else {
  265. $this->assertFalse($isValid);
  266. }
  267. }
  268. /**
  269. * Checks expected exception on wrong type
  270. *
  271. * @param mixed $value Value to validate
  272. *
  273. * @return void
  274. */
  275. protected function checkExpectedException($value)
  276. {
  277. $this->setExpectedException('\Zend\Validator\Exception\InvalidArgumentException');
  278. $this->validator->setType($value);
  279. }
  280. /**
  281. * Data provider
  282. *
  283. * @return mixed[][]
  284. */
  285. public function getInvalidTypes()
  286. {
  287. return array(
  288. array(new \stdClass()),
  289. array(array()),
  290. array(0),
  291. array(10),
  292. array('foo'),
  293. );
  294. }
  295. }