/tests/Zend/Filter/File/RenameTest.php

https://github.com/devilsansclue/ZendFramework · PHP · 473 lines · 264 code · 57 blank · 152 comment · 10 complexity · f9ef92a1b4eef41f8ca981014fb733d7 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined("PHPUnit_MAIN_METHOD")) {
  23. define("PHPUnit_MAIN_METHOD", "Zend_Filter_File_RenameTest::main");
  24. }
  25. /**
  26. * @see Zend_Filter_File_Rename
  27. */
  28. require_once 'Zend/Filter/File/Rename.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Filter
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Filter
  36. */
  37. class Zend_Filter_File_RenameTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Path to test files
  41. *
  42. * @var string
  43. */
  44. protected $_filesPath;
  45. /**
  46. * Original testfile
  47. *
  48. * @var string
  49. */
  50. protected $_origFile;
  51. /**
  52. * Testfile
  53. *
  54. * @var string
  55. */
  56. protected $_oldFile;
  57. /**
  58. * Testfile
  59. *
  60. * @var string
  61. */
  62. protected $_newFile;
  63. /**
  64. * Testdirectory
  65. *
  66. * @var string
  67. */
  68. protected $_newDir;
  69. /**
  70. * Testfile in Testdirectory
  71. *
  72. * @var string
  73. */
  74. protected $_newDirFile;
  75. /**
  76. * Runs the test methods of this class.
  77. *
  78. * @return void
  79. */
  80. public static function main()
  81. {
  82. $suite = new PHPUnit_Framework_TestSuite("Zend_Filter_File_RenameTest");
  83. $result = PHPUnit_TextUI_TestRunner::run($suite);
  84. }
  85. /**
  86. * Sets the path to test files
  87. *
  88. * @return void
  89. */
  90. public function __construct()
  91. {
  92. $this->_filesPath = dirname(__FILE__) . DIRECTORY_SEPARATOR
  93. . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR;
  94. $this->_origFile = $this->_filesPath . 'original.file';
  95. $this->_oldFile = $this->_filesPath . 'testfile.txt';
  96. $this->_newFile = $this->_filesPath . 'newfile.xml';
  97. $this->_newDir = $this->_filesPath . DIRECTORY_SEPARATOR . '_testDir2';
  98. $this->_newDirFile = $this->_newDir . DIRECTORY_SEPARATOR . 'testfile.txt';
  99. }
  100. /**
  101. * Sets the path to test files
  102. *
  103. * @return void
  104. */
  105. public function setUp()
  106. {
  107. if (file_exists($this->_origFile)) {
  108. unlink($this->_origFile);
  109. }
  110. if (file_exists($this->_newFile)) {
  111. unlink($this->_newFile);
  112. }
  113. if (file_exists($this->_newDirFile)) {
  114. unlink($this->_newDirFile);
  115. }
  116. copy($this->_oldFile, $this->_origFile);
  117. }
  118. /**
  119. * Sets the path to test files
  120. *
  121. * @return void
  122. */
  123. public function tearDown()
  124. {
  125. if (!file_exists($this->_oldFile)) {
  126. copy($this->_origFile, $this->_oldFile);
  127. }
  128. if (file_exists($this->_origFile)) {
  129. unlink($this->_origFile);
  130. }
  131. if (file_exists($this->_newFile)) {
  132. unlink($this->_newFile);
  133. }
  134. if (file_exists($this->_newDirFile)) {
  135. unlink($this->_newDirFile);
  136. }
  137. }
  138. /**
  139. * Test single parameter filter
  140. *
  141. * @return void
  142. */
  143. public function testConstructSingleValue()
  144. {
  145. $filter = new Zend_Filter_File_Rename($this->_newFile);
  146. $this->assertEquals(array(0 =>
  147. array('source' => '*',
  148. 'target' => $this->_newFile,
  149. 'overwrite' => false)),
  150. $filter->getFile());
  151. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  152. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  153. }
  154. /**
  155. * Test single array parameter filter
  156. *
  157. * @return void
  158. */
  159. public function testConstructSingleArray()
  160. {
  161. $filter = new Zend_Filter_File_Rename(array(
  162. 'source' => $this->_oldFile,
  163. 'target' => $this->_newFile));
  164. $this->assertEquals(array(0 =>
  165. array('source' => $this->_oldFile,
  166. 'target' => $this->_newFile,
  167. 'overwrite' => false)), $filter->getFile());
  168. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  169. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  170. }
  171. /**
  172. * Test full array parameter filter
  173. *
  174. * @return void
  175. */
  176. public function testConstructFullOptionsArray()
  177. {
  178. $filter = new Zend_Filter_File_Rename(array(
  179. 'source' => $this->_oldFile,
  180. 'target' => $this->_newFile,
  181. 'overwrite' => true,
  182. 'unknown' => false));
  183. $this->assertEquals(array(0 =>
  184. array('source' => $this->_oldFile,
  185. 'target' => $this->_newFile,
  186. 'overwrite' => true)), $filter->getFile());
  187. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  188. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  189. }
  190. /**
  191. * Test single array parameter filter
  192. *
  193. * @return void
  194. */
  195. public function testConstructDoubleArray()
  196. {
  197. $filter = new Zend_Filter_File_Rename(array(
  198. 0 => array(
  199. 'source' => $this->_oldFile,
  200. 'target' => $this->_newFile)));
  201. $this->assertEquals(array(0 =>
  202. array('source' => $this->_oldFile,
  203. 'target' => $this->_newFile,
  204. 'overwrite' => false)), $filter->getFile());
  205. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  206. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  207. }
  208. /**
  209. * Test single array parameter filter
  210. *
  211. * @return void
  212. */
  213. public function testConstructTruncatedTarget()
  214. {
  215. $filter = new Zend_Filter_File_Rename(array(
  216. 'source' => $this->_oldFile));
  217. $this->assertEquals(array(0 =>
  218. array('source' => $this->_oldFile,
  219. 'target' => '*',
  220. 'overwrite' => false)), $filter->getFile());
  221. $this->assertEquals($this->_oldFile, $filter->filter($this->_oldFile));
  222. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  223. }
  224. /**
  225. * Test single array parameter filter
  226. *
  227. * @return void
  228. */
  229. public function testConstructTruncatedSource()
  230. {
  231. $filter = new Zend_Filter_File_Rename(array(
  232. 'target' => $this->_newFile));
  233. $this->assertEquals(array(0 =>
  234. array('source' => '*',
  235. 'target' => $this->_newFile,
  236. 'overwrite' => false)), $filter->getFile());
  237. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  238. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  239. }
  240. /**
  241. * Test single parameter filter by using directory only
  242. *
  243. * @return void
  244. */
  245. public function testConstructSingleDirectory()
  246. {
  247. $filter = new Zend_Filter_File_Rename($this->_newDir);
  248. $this->assertEquals(array(0 =>
  249. array('source' => '*',
  250. 'target' => $this->_newDir,
  251. 'overwrite' => false)), $filter->getFile());
  252. $this->assertEquals($this->_newDirFile, $filter->filter($this->_oldFile));
  253. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  254. }
  255. /**
  256. * Test single array parameter filter by using directory only
  257. *
  258. * @return void
  259. */
  260. public function testConstructSingleArrayDirectory()
  261. {
  262. $filter = new Zend_Filter_File_Rename(array(
  263. 'source' => $this->_oldFile,
  264. 'target' => $this->_newDir));
  265. $this->assertEquals(array(0 =>
  266. array('source' => $this->_oldFile,
  267. 'target' => $this->_newDir,
  268. 'overwrite' => false)), $filter->getFile());
  269. $this->assertEquals($this->_newDirFile, $filter->filter($this->_oldFile));
  270. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  271. }
  272. /**
  273. * Test single array parameter filter by using directory only
  274. *
  275. * @return void
  276. */
  277. public function testConstructDoubleArrayDirectory()
  278. {
  279. $filter = new Zend_Filter_File_Rename(array(
  280. 0 => array(
  281. 'source' => $this->_oldFile,
  282. 'target' => $this->_newDir)));
  283. $this->assertEquals(array(0 =>
  284. array('source' => $this->_oldFile,
  285. 'target' => $this->_newDir,
  286. 'overwrite' => false)), $filter->getFile());
  287. $this->assertEquals($this->_newDirFile, $filter->filter($this->_oldFile));
  288. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  289. }
  290. /**
  291. * Test single array parameter filter by using directory only
  292. *
  293. * @return void
  294. */
  295. public function testConstructTruncatedSourceDirectory()
  296. {
  297. $filter = new Zend_Filter_File_Rename(array(
  298. 'target' => $this->_newDir));
  299. $this->assertEquals(array(0 =>
  300. array('source' => '*',
  301. 'target' => $this->_newDir,
  302. 'overwrite' => false)), $filter->getFile());
  303. $this->assertEquals($this->_newDirFile, $filter->filter($this->_oldFile));
  304. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  305. }
  306. /**
  307. * @return void
  308. */
  309. public function testAddSameFileAgainAndOverwriteExistingTarget()
  310. {
  311. $filter = new Zend_Filter_File_Rename(array(
  312. 'source' => $this->_oldFile,
  313. 'target' => $this->_newDir));
  314. $filter->addFile(array(
  315. 'source' => $this->_oldFile,
  316. 'target' => $this->_newFile));
  317. $this->assertEquals(array(0 =>
  318. array('source' => $this->_oldFile,
  319. 'target' => $this->_newFile,
  320. 'overwrite' => false)), $filter->getFile());
  321. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  322. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  323. }
  324. /**
  325. * @return void
  326. */
  327. public function testGetNewName()
  328. {
  329. $filter = new Zend_Filter_File_Rename(array(
  330. 'source' => $this->_oldFile,
  331. 'target' => $this->_newDir));
  332. $this->assertEquals(array(0 =>
  333. array('source' => $this->_oldFile,
  334. 'target' => $this->_newDir,
  335. 'overwrite' => false)), $filter->getFile());
  336. $this->assertEquals($this->_newDirFile, $filter->getNewName($this->_oldFile));
  337. }
  338. /**
  339. * @return void
  340. */
  341. public function testGetNewNameExceptionWithExistingFile()
  342. {
  343. $filter = new Zend_Filter_File_Rename(array(
  344. 'source' => $this->_oldFile,
  345. 'target' => $this->_newFile));
  346. copy($this->_oldFile, $this->_newFile);
  347. $this->assertEquals(array(0 =>
  348. array('source' => $this->_oldFile,
  349. 'target' => $this->_newFile,
  350. 'overwrite' => false)), $filter->getFile());
  351. try {
  352. $this->assertEquals($this->_newFile, $filter->getNewName($this->_oldFile));
  353. $this->fail();
  354. } catch (Zend_Filter_Exception $e) {
  355. $this->assertContains('could not be renamed', $e->getMessage());
  356. }
  357. }
  358. /**
  359. * @return void
  360. */
  361. public function testGetNewNameOverwriteWithExistingFile()
  362. {
  363. $filter = new Zend_Filter_File_Rename(array(
  364. 'source' => $this->_oldFile,
  365. 'target' => $this->_newFile,
  366. 'overwrite' => true));
  367. copy($this->_oldFile, $this->_newFile);
  368. $this->assertEquals(array(0 =>
  369. array('source' => $this->_oldFile,
  370. 'target' => $this->_newFile,
  371. 'overwrite' => true)), $filter->getFile());
  372. $this->assertEquals($this->_newFile, $filter->getNewName($this->_oldFile));
  373. }
  374. /**
  375. * @return void
  376. */
  377. public function testAddFileWithString()
  378. {
  379. $filter = new Zend_Filter_File_Rename($this->_oldFile);
  380. $filter->addFile($this->_newFile);
  381. $this->assertEquals(array(0 =>
  382. array('source' => '*',
  383. 'target' => $this->_newFile,
  384. 'overwrite' => false)), $filter->getFile());
  385. $this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
  386. $this->assertEquals('falsefile', $filter->filter('falsefile'));
  387. }
  388. /**
  389. * @return void
  390. */
  391. public function testAddFileWithInvalidOption()
  392. {
  393. $filter = new Zend_Filter_File_Rename($this->_oldFile);
  394. try {
  395. $filter->addFile(1234);
  396. $this->fail();
  397. } catch (Zend_Filter_Exception $e) {
  398. $this->assertContains('Invalid options', $e->getMessage());
  399. }
  400. }
  401. /**
  402. * @return void
  403. */
  404. public function testInvalidContruction()
  405. {
  406. try {
  407. $filter = new Zend_Filter_File_Rename(1234);
  408. $this->fail();
  409. } catch (Zend_Filter_Exception $e) {
  410. $this->assertContains('Invalid options', $e->getMessage());
  411. }
  412. }
  413. }
  414. if (PHPUnit_MAIN_METHOD == "Zend_Filter_File_RenameTest::main") {
  415. Zend_Filter_File_RenameTest::main();
  416. }