PageRenderTime 70ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/Feed/tests/test.php

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