PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Feed/tests/test.php

https://github.com/Yannix/zetacomponents
PHP | 318 lines | 162 code | 19 blank | 137 comment | 5 complexity | 1bf521e621bcd009a20dc4eef46950d6 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @filesource
  23. * @package Feed
  24. * @version //autogen//
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package Feed
  29. * @version //autogen//
  30. * @subpackage Tests
  31. * @access private
  32. */
  33. class ezcFeedTestCase extends ezcTestCase
  34. {
  35. /**
  36. * Tests assigning an invalid value to a property.
  37. *
  38. * Expects that an ezcBaseValueException is raised by the invalid value.
  39. *
  40. * @param object $properties An object which implements properties access
  41. * @param string $property The property of the $properties object to test
  42. * @param mixed $value The value to try to assign to $property
  43. * @param string $allowedValue The values which are allowed for $property
  44. */
  45. protected function invalidPropertyTest( $properties, $property, $value, $allowedValue )
  46. {
  47. try
  48. {
  49. $properties->$property = $value;
  50. $this->fail( "Expected exception was not thrown." );
  51. }
  52. catch ( ezcBaseValueException $e )
  53. {
  54. $value = is_array( $value ) ? serialize( $value ) : $value;
  55. $this->assertEquals( "The value '{$value}' that you were trying to assign to setting '{$property}' is invalid. Allowed values are: {$allowedValue}.", $e->getMessage() );
  56. }
  57. }
  58. /**
  59. * Tests assigning a read-only property.
  60. *
  61. * Expects that an ezcBasePropertyPermissionException is raised.
  62. *
  63. * @param object $properties An object which implements properties access
  64. * @param string $property The property of the $properties object to test
  65. */
  66. protected function readonlyPropertyTest( $properties, $property )
  67. {
  68. try
  69. {
  70. $properties->$property = null;
  71. $this->fail( "Expected exception was not thrown." );
  72. }
  73. catch ( ezcBasePropertyPermissionException $e )
  74. {
  75. $this->assertEquals( "The property '{$property}' is read-only.", $e->getMessage() );
  76. }
  77. }
  78. /**
  79. * Tests assigning a value to a missing property.
  80. *
  81. * Expects that an ezcBasePropertyNotFoundException is raised by the missing
  82. * property.
  83. *
  84. * @param object $properties An object which implements properties access
  85. * @param string $property The property of the $properties object to test
  86. */
  87. protected function missingPropertyTest( $properties, $property )
  88. {
  89. try
  90. {
  91. $properties->$property = null;
  92. $this->fail( "Expected exception was not thrown." );
  93. }
  94. catch ( ezcBasePropertyNotFoundException $e )
  95. {
  96. $this->assertEquals( "No such property name '{$property}'.", $e->getMessage() );
  97. }
  98. // workaround around a bug (?) - __isset() in ezcBaseOptions complains and warns
  99. // that the second parameter for array_exists() must be an array or an object
  100. if ( !$properties instanceof ezcBaseOptions )
  101. {
  102. try
  103. {
  104. $value = $properties->$property;
  105. $this->fail( "Expected exception was not thrown." );
  106. }
  107. catch ( ezcBasePropertyNotFoundException $e )
  108. {
  109. $this->assertEquals( "No such property name '{$property}'.", $e->getMessage() );
  110. }
  111. }
  112. }
  113. /**
  114. * Tests if a property is set.
  115. *
  116. * Compares the result of isset() with $value.
  117. *
  118. * @param object $properties An object which implements properties access
  119. * @param string $property The property of the $properties object to test
  120. * @param bool $value True if expecting that $property is set, false otherwise
  121. */
  122. protected function issetPropertyTest( $properties, $property, $value )
  123. {
  124. $this->assertEquals( $value, isset( $properties->$property ) );
  125. }
  126. /**
  127. * Tests assigning a non-existent path to a property.
  128. *
  129. * Expects that an ezcBaseFileNotFoundException is raised by the missing
  130. * path.
  131. *
  132. * @param object $properties An object which implements properties access
  133. * @param string $property The property of the $properties object to test
  134. * @param string $value A path which does not exist
  135. */
  136. protected function missingFileTest( $properties, $property, $value )
  137. {
  138. try
  139. {
  140. $properties->$property = $value;
  141. $this->fail( "Expected exception was not thrown" );
  142. }
  143. catch ( ezcBaseFileNotFoundException $e )
  144. {
  145. $this->assertEquals( "The file '{$value}' could not be found.", $e->getMessage() );
  146. }
  147. }
  148. /**
  149. * Tests assigning an unreadable path to a property.
  150. *
  151. * Expects that an ezcBaseFilePermissionException is raised by the unreadable
  152. * path.
  153. *
  154. * This function creates a temporary file and makes it unreadable.
  155. *
  156. * @param object $properties An object which implements properties access
  157. * @param string $property The property of the $properties object to test
  158. * @param string $value A filename without paths or slashes
  159. */
  160. protected function unreadableFileTest( $properties, $property, $value )
  161. {
  162. $tempDir = $this->createTempDir( get_class( $this ) );
  163. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  164. $fh = fopen( $path, "wb" );
  165. fwrite( $fh, "some values" );
  166. fclose( $fh );
  167. chmod( $path, 0 );
  168. try
  169. {
  170. $properties->$property = $path;
  171. $this->removeTempDir();
  172. $this->fail( "Expected exception was not thrown." );
  173. }
  174. catch ( ezcBaseFilePermissionException $e )
  175. {
  176. $this->assertEquals( "The file '{$path}' can not be opened for reading.", $e->getMessage() );
  177. }
  178. $this->removeTempDir();
  179. }
  180. /**
  181. * Tests assigning an unwritable path to a property.
  182. *
  183. * Expects that an ezcBaseFilePermissionException is raised by the unwritable
  184. * path.
  185. *
  186. * This function creates a temporary file and makes it unwritable.
  187. *
  188. * @param object $properties An object which implements properties access
  189. * @param string $property The property of the $properties object to test
  190. * @param string $value A filename without paths or slashes
  191. */
  192. protected function unwritableFileTest( $properties, $property, $value )
  193. {
  194. $tempDir = $this->createTempDir( get_class( $this ) );
  195. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  196. $fh = fopen( $path, "wb" );
  197. fwrite( $fh, "some values" );
  198. fclose( $fh );
  199. chmod( $path, 0 );
  200. try
  201. {
  202. $properties->$property = $path;
  203. $this->removeTempDir();
  204. $this->fail( "Expected exception was not thrown." );
  205. }
  206. catch ( ezcBaseFilePermissionException $e )
  207. {
  208. $this->assertEquals( "The file '{$path}' can not be opened for writing.", $e->getMessage() );
  209. }
  210. $this->removeTempDir();
  211. }
  212. /**
  213. * Tests assigning a non-existent directory to a property.
  214. *
  215. * Expects that an ezcBaseFileNotFoundException is raised by the missing
  216. * directory.
  217. *
  218. * @param object $properties An object which implements properties access
  219. * @param string $property The property of the $properties object to test
  220. * @param string $value A directory which does not exist
  221. */
  222. protected function missingDirTest( $properties, $property, $value )
  223. {
  224. try
  225. {
  226. $properties->$property = $value;
  227. $this->fail( "Expected exception was not thrown" );
  228. }
  229. catch ( ezcBaseFileNotFoundException $e )
  230. {
  231. $this->assertEquals( "The file '{$value}' could not be found.", $e->getMessage() );
  232. }
  233. }
  234. /**
  235. * Tests assigning an unreadable directory to a property.
  236. *
  237. * Expects that an ezcBaseFilePermissionException is raised by the unreadable
  238. * path.
  239. *
  240. * This function creates a temporary directory and makes it unreadable.
  241. *
  242. * @param object $properties An object which implements properties access
  243. * @param string $property The property of the $properties object to test
  244. * @param string $value A directory name without paths or slashes
  245. */
  246. protected function unreadableDirTest( $properties, $property, $value )
  247. {
  248. $tempDir = $this->createTempDir( get_class( $this ) );
  249. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  250. mkdir( $path );
  251. chmod( $path, 0 );
  252. try
  253. {
  254. $properties->$property = $path;
  255. chmod( $path, 0777 );
  256. $this->removeTempDir();
  257. $this->fail( "Expected exception was not thrown." );
  258. }
  259. catch ( ezcBaseFilePermissionException $e )
  260. {
  261. $this->assertEquals( "The file '{$path}' can not be opened for reading.", $e->getMessage() );
  262. }
  263. chmod( $path, 0777 );
  264. $this->removeTempDir();
  265. }
  266. /**
  267. * Tests assigning an unwritable directory to a property.
  268. *
  269. * Expects that an ezcBaseFilePermissionException is raised by the unwritable
  270. * path.
  271. *
  272. * This function creates a temporary directory and makes it unwritable.
  273. *
  274. * @param object $properties An object which implements properties access
  275. * @param string $property The property of the $properties object to test
  276. * @param string $value A directory name without paths or slashes
  277. */
  278. protected function unwritableDirTest( $properties, $property, $value )
  279. {
  280. $tempDir = $this->createTempDir( get_class( $this ) );
  281. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  282. mkdir( $path );
  283. chmod( $path, 0444 );
  284. try
  285. {
  286. $properties->$property = $path;
  287. chmod( $path, 0777 );
  288. $this->removeTempDir();
  289. $this->fail( "Expected exception was not thrown." );
  290. }
  291. catch ( ezcBaseFilePermissionException $e )
  292. {
  293. $this->assertEquals( "The file '{$path}' can not be opened for writing.", $e->getMessage() );
  294. }
  295. chmod( $path, 0777 );
  296. $this->removeTempDir();
  297. }
  298. }
  299. ?>