PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Tasks/ProjectCleanTaskTest.php

https://github.com/Proudio-Interactive/phpUnderControl
PHP | 360 lines | 256 code | 18 blank | 86 comment | 0 complexity | 1220e6f602d54d4e30dce3dffd5853a8 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of phpUnderControl.
  4. *
  5. * PHP Version 5.2.0
  6. *
  7. * Copyright (c) 2007-2010, Manuel Pichler <mapi@manuel-pichler.de>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package Tasks
  41. * @author Manuel Pichler <mapi@manuel-pichler.de>
  42. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @version SVN: $Id$
  45. * @link http://www.phpundercontrol.org/
  46. */
  47. require_once dirname( __FILE__ ) . '/AbstractTaskTest.php';
  48. /**
  49. * Test case for the {@link phpucProjectCleanTask} class.
  50. *
  51. * @category QualityAssurance
  52. * @package Tasks
  53. * @author Manuel Pichler <mapi@manuel-pichler.de>
  54. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: @package_version@
  57. * @link http://www.phpundercontrol.org/
  58. */
  59. class phpucProjectCleanTaskTest extends phpucAbstractTaskTest
  60. {
  61. /**
  62. * Creates some sample logs and artifacts.
  63. *
  64. * @return void
  65. */
  66. protected function setUp()
  67. {
  68. parent::setUp();
  69. $this->createTestDirectories(
  70. array(
  71. "/cruisecontrol/logs/{$this->projectName}",
  72. "/cruisecontrol/logs/{$this->projectName}/20071211211853",
  73. "/cruisecontrol/logs/{$this->projectName}/20071211220903",
  74. "/cruisecontrol/logs/{$this->projectName}/20071217180035",
  75. "/cruisecontrol/logs/{$this->projectName}/20080106030401",
  76. "/cruisecontrol/logs/{$this->projectName}/20080109182028",
  77. "/cruisecontrol/logs/{$this->projectName}/20080113145726",
  78. "/cruisecontrol/logs/{$this->projectName}/20080114115320",
  79. "/cruisecontrol/logs/{$this->projectName}/20080118220842",
  80. "/cruisecontrol/artifacts",
  81. "/cruisecontrol/artifacts/{$this->projectName}/20071211211853",
  82. "/cruisecontrol/artifacts/{$this->projectName}/20071211220903",
  83. "/cruisecontrol/artifacts/{$this->projectName}/20071217180035",
  84. "/cruisecontrol/artifacts/{$this->projectName}/20080106030401",
  85. "/cruisecontrol/artifacts/{$this->projectName}/20080109182028",
  86. "/cruisecontrol/artifacts/{$this->projectName}/20080113145726",
  87. "/cruisecontrol/artifacts/{$this->projectName}/20080114115320",
  88. "/cruisecontrol/artifacts/{$this->projectName}/20080118220842",
  89. )
  90. );
  91. $this->createTestFile(
  92. "/cruisecontrol/logs/{$this->projectName}/log20071211211853.xml",
  93. file_get_contents( PHPUC_TEST_LOGS . '/log20071211211853.xml' )
  94. );
  95. $this->createTestFile(
  96. "/cruisecontrol/logs/{$this->projectName}/log20071211220903Lbuild.3.xml",
  97. file_get_contents( PHPUC_TEST_LOGS . '/log20071211220903Lbuild.3.xml' )
  98. );
  99. $this->createTestFile(
  100. "/cruisecontrol/logs/{$this->projectName}/log20071217180035Lbuild.18.xml",
  101. file_get_contents( PHPUC_TEST_LOGS . '/log20071217180035Lbuild.18.xml' )
  102. );
  103. $this->createTestFile(
  104. "/cruisecontrol/logs/{$this->projectName}/log20080106030401Lbuild.24.xml",
  105. file_get_contents( PHPUC_TEST_LOGS . '/log20080106030401Lbuild.24.xml' )
  106. );
  107. $this->createTestFile(
  108. "/cruisecontrol/logs/{$this->projectName}/log20080109182028Lbuild.30.xml",
  109. file_get_contents( PHPUC_TEST_LOGS . '/log20080109182028Lbuild.30.xml' )
  110. );
  111. $this->createTestFile(
  112. "/cruisecontrol/logs/{$this->projectName}/log20080113145726.xml",
  113. file_get_contents( PHPUC_TEST_LOGS . '/log20080113145726.xml' )
  114. );
  115. $this->createTestFile(
  116. "/cruisecontrol/logs/{$this->projectName}/log20080114115320.xml",
  117. file_get_contents( PHPUC_TEST_LOGS . '/log20080114115320.xml' )
  118. );
  119. $this->createTestFile(
  120. "/cruisecontrol/logs/{$this->projectName}/log20080118220842Lbuild.57.xml",
  121. file_get_contents( PHPUC_TEST_LOGS . '/log20080118220842Lbuild.57.xml' )
  122. );
  123. }
  124. /**
  125. * Tests that {@phpucProjectCleanTask#execute()} keeps the specified number
  126. * of logs and artifacts.
  127. *
  128. * @return void
  129. */
  130. public function testExecuteProjectCleanTaskWithNumberOfBuilds()
  131. {
  132. $args = $this->prepareConsoleArgs(
  133. array(
  134. 'clean',
  135. '-j',
  136. $this->projectName,
  137. '-k',
  138. 4,
  139. PHPUC_TEST_DIR . '/cruisecontrol'
  140. )
  141. );
  142. $task = new phpucProjectCleanTask();
  143. $task->setConsoleArgs( $args );
  144. $task->execute();
  145. // Check artifacts directory
  146. $this->assertFileNotExists(
  147. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20071211211853"
  148. );
  149. $this->assertFileNotExists(
  150. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20071211220903"
  151. );
  152. $this->assertFileNotExists(
  153. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20071217180035"
  154. );
  155. $this->assertFileNotExists(
  156. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080106030401"
  157. );
  158. $this->assertFileExists(
  159. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080109182028"
  160. );
  161. $this->assertFileExists(
  162. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080113145726"
  163. );
  164. $this->assertFileExists(
  165. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080114115320"
  166. );
  167. $this->assertFileExists(
  168. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080118220842"
  169. );
  170. // Check logs directory
  171. $this->assertFileNotExists(
  172. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20071211211853"
  173. );
  174. $this->assertFileNotExists(
  175. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20071211220903"
  176. );
  177. $this->assertFileNotExists(
  178. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20071217180035"
  179. );
  180. $this->assertFileNotExists(
  181. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080106030401"
  182. );
  183. $this->assertFileExists(
  184. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080109182028"
  185. );
  186. $this->assertFileExists(
  187. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080113145726"
  188. );
  189. $this->assertFileExists(
  190. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080114115320"
  191. );
  192. $this->assertFileExists(
  193. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080118220842"
  194. );
  195. // Check log files
  196. $this->assertFileNotExists(
  197. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20071211211853.xml"
  198. );
  199. $this->assertFileNotExists(
  200. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20071211220903Lbuild.3.xml"
  201. );
  202. $this->assertFileNotExists(
  203. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20071217180035Lbuild.18.xml"
  204. );
  205. $this->assertFileNotExists(
  206. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080106030401Lbuild.24.xml"
  207. );
  208. $this->assertFileExists(
  209. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080109182028Lbuild.30.xml"
  210. );
  211. $this->assertFileExists(
  212. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080113145726.xml"
  213. );
  214. $this->assertFileExists(
  215. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080114115320.xml"
  216. );
  217. $this->assertFileExists(
  218. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080118220842Lbuild.57.xml"
  219. );
  220. }
  221. /**
  222. * Tests the {@phpucProjectCleanTask#execute()} implementation with the
  223. * <b>--keep-days</b> option.
  224. *
  225. * @return void
  226. */
  227. public function testExecuteProjectCleanTaskWithKeepDays()
  228. {
  229. $oldestBuild = mktime( 0, 0, 0, 1, 13, 2008 );
  230. $currentTime = mktime( 0, 0, 0 );
  231. $days = ceil( ( $currentTime - $oldestBuild ) / 86400 );
  232. $args = $this->prepareConsoleArgs(
  233. array(
  234. 'clean',
  235. '-j',
  236. $this->projectName,
  237. '--keep-days',
  238. $days,
  239. PHPUC_TEST_DIR . '/cruisecontrol'
  240. )
  241. );
  242. $task = new phpucProjectCleanTask();
  243. $task->setConsoleArgs( $args );
  244. $task->execute();
  245. // Check artifacts directory
  246. $this->assertFileNotExists(
  247. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20071211211853"
  248. );
  249. $this->assertFileNotExists(
  250. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20071211220903"
  251. );
  252. $this->assertFileNotExists(
  253. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20071217180035"
  254. );
  255. $this->assertFileNotExists(
  256. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080106030401"
  257. );
  258. $this->assertFileNotExists(
  259. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080109182028"
  260. );
  261. $this->assertFileExists(
  262. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080113145726"
  263. );
  264. $this->assertFileExists(
  265. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080114115320"
  266. );
  267. $this->assertFileExists(
  268. PHPUC_TEST_DIR . "/cruisecontrol/artifacts/{$this->projectName}/20080118220842"
  269. );
  270. // Check logs directory
  271. $this->assertFileNotExists(
  272. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20071211211853"
  273. );
  274. $this->assertFileNotExists(
  275. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20071211220903"
  276. );
  277. $this->assertFileNotExists(
  278. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20071217180035"
  279. );
  280. $this->assertFileNotExists(
  281. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080106030401"
  282. );
  283. $this->assertFileNotExists(
  284. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080109182028"
  285. );
  286. $this->assertFileExists(
  287. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080113145726"
  288. );
  289. $this->assertFileExists(
  290. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080114115320"
  291. );
  292. $this->assertFileExists(
  293. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/20080118220842"
  294. );
  295. // Check log files
  296. $this->assertFileNotExists(
  297. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20071211211853.xml"
  298. );
  299. $this->assertFileNotExists(
  300. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20071211220903Lbuild.3.xml"
  301. );
  302. $this->assertFileNotExists(
  303. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20071217180035Lbuild.18.xml"
  304. );
  305. $this->assertFileNotExists(
  306. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080106030401Lbuild.24.xml"
  307. );
  308. $this->assertFileNotExists(
  309. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080109182028Lbuild.30.xml"
  310. );
  311. $this->assertFileExists(
  312. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080113145726.xml"
  313. );
  314. $this->assertFileExists(
  315. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080114115320.xml"
  316. );
  317. $this->assertFileExists(
  318. PHPUC_TEST_DIR . "/cruisecontrol/logs/{$this->projectName}/log20080118220842Lbuild.57.xml"
  319. );
  320. }
  321. /**
  322. * Tests that the validate method throws an exception when the specified
  323. * project does not exist.
  324. *
  325. * @return void
  326. * @expectedException phpucValidateException
  327. */
  328. public function testExecuteProjectCleanTaskThrowsValidationException()
  329. {
  330. $args = $this->prepareConsoleArgs(
  331. array(
  332. 'clean',
  333. '-j',
  334. __FUNCTION__,
  335. PHPUC_TEST_DIR . '/cruisecontrol'
  336. )
  337. );
  338. $task = new phpucProjectCleanTask();
  339. $task->setConsoleArgs( $args );
  340. $task->validate();
  341. }
  342. }