LibraryTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\console\command;
  9. use Phar;
  10. use lithium\console\command\Library;
  11. use lithium\core\Libraries;
  12. use lithium\console\Request;
  13. class LibraryTest extends \lithium\test\Unit {
  14. public $request;
  15. protected $_backup = array();
  16. protected $_testPath = null;
  17. public function skip() {
  18. $this->_testPath = Libraries::get(true, 'resources') . '/tmp/tests';
  19. $this->skipIf(!is_writable($this->_testPath), "Path `{$this->_testPath}` is not writable.");
  20. }
  21. public function setUp() {
  22. $this->_backup['cwd'] = getcwd();
  23. $this->_backup['_SERVER'] = $_SERVER;
  24. $_SERVER['argv'] = array();
  25. chdir($this->_testPath);
  26. Libraries::add('library_test', array(
  27. 'path' => $this->_testPath . '/library_test', 'bootstrap' => false
  28. ));
  29. Libraries::add('library_test_plugin', array(
  30. 'path' => $this->_testPath . '/library_test_plugin'
  31. ));
  32. $this->classes = array(
  33. 'service' => 'lithium\tests\mocks\console\command\MockLibraryService',
  34. 'response' => 'lithium\tests\mocks\console\MockResponse'
  35. );
  36. $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
  37. $this->library = new Library(array(
  38. 'request' => $this->request, 'classes' => $this->classes
  39. ));
  40. $this->testConf = $this->library->conf = $this->_testPath . '/library.json';
  41. }
  42. public function tearDown() {
  43. $_SERVER = $this->_backup['_SERVER'];
  44. chdir($this->_backup['cwd']);
  45. Libraries::remove('library_test');
  46. unset($this->library, $this->request);
  47. }
  48. public function testConfigServer() {
  49. $result = $this->library->config('server', 'lab.lithify.me');
  50. $this->assertTrue($result);
  51. $expected = array('servers' => array('lab.lithify.me' => true));
  52. $result = json_decode(file_get_contents($this->testConf), true);
  53. $this->assertEqual($expected, $result);
  54. //create a new object to test initialiaztion
  55. $this->request->params += array('conf' => $this->testConf);
  56. $library = new Library(array(
  57. 'request' => $this->request, 'classes' => $this->classes
  58. ));
  59. $expected = array('servers' => array(
  60. 'lab.lithify.me' => true
  61. ));
  62. $result = $this->library->config();
  63. $this->assertEqual($expected, $result);
  64. }
  65. public function testExtract() {
  66. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  67. $this->library->library = 'library_test';
  68. $result = $this->library->extract($this->_testPath . '/library_test');
  69. $this->assertTrue($result);
  70. $path = '/lithium/console/command/create/template/app.phar.gz';
  71. $expected = "library_test created in {$this->_testPath} from ";
  72. $expected .= realpath(LITHIUM_LIBRARY_PATH . $path) . "\n";
  73. $result = $this->library->response->output;
  74. $this->assertEqual($expected, $result);
  75. }
  76. /**
  77. * Tests the app extraction and replace functionality to ensure that all paths
  78. * are set correctly after the extraction. It re-uses the test extraction
  79. * generated in the last test (`LibraryTest::testExtract()`).
  80. *
  81. * Note that we can't properly test the case where the `LITHIUM_LIBRARY_PATH`
  82. * is in a different location because it's a constant and can't
  83. * be altered for testing.
  84. */
  85. public function testExtractAndReplace() {
  86. $filepath = $this->_testPath . '/library_test/config/bootstrap/libraries.php';
  87. if (dirname(LITHIUM_APP_PATH) . '/libraries' !== LITHIUM_LIBRARY_PATH) {
  88. $expected = 'define(\'LITHIUM_LIBRARY_PATH\', \'';
  89. $expected .= realpath(LITHIUM_LIBRARY_PATH) . '\')';
  90. } else {
  91. $expected = 'define(\'LITHIUM_LIBRARY_PATH\', ';
  92. $expected .= 'dirname(LITHIUM_APP_PATH) . \'/libraries\')';
  93. }
  94. $this->_assertFileContents(realpath($filepath), $expected);
  95. $filepath = $this->_testPath . '/library_test/controllers/PagesController.php';
  96. $expected = "namespace library_test\\";
  97. $this->_assertFileContents($filepath, $expected);
  98. $this->library->library = 'replace_test';
  99. $this->library->lithiumLibraryPath = 'dirname(LITHIUM_APP_PATH)';
  100. $result = $this->library->extract(
  101. 'test-app-replacements',
  102. $this->_testPath . '/replace_test'
  103. );
  104. $this->assertTrue($result);
  105. $path = '/lithium/console/command/create/template/test-app-replacements.phar.gz';
  106. $expected = "replace_test created in {$this->_testPath} from ";
  107. $expected .= realpath(LITHIUM_LIBRARY_PATH . $path) . "\n";
  108. $result = $this->library->response->output;
  109. $this->assertEqual($expected, $result);
  110. $filepath = $this->_testPath . '/replace_test/config/bootstrap/libraries.php';
  111. $expected = 'define(\'LITHIUM_LIBRARY_PATH\', dirname(LITHIUM_APP_PATH))';
  112. $this->_assertFileContents($filepath, $expected);
  113. $filepath = $this->_testPath . '/replace_test/controllers/PagesController.php';
  114. $expected = "namespace replace_test\\";
  115. $this->_assertFileContents($filepath, $expected);
  116. $filepath = $this->_testPath . '/replace_test/.htaccess';
  117. $expected = "just a test";
  118. $this->_assertFileContents($filepath, $expected);
  119. $filepath = $this->_testPath . '/replace_test/webroot/css/lithium.css';
  120. $expected = "Carbon:";
  121. $this->_assertFileContents($filepath, $expected);
  122. }
  123. protected function _assertFileContents($filepath, $expected) {
  124. $content = file_get_contents($filepath);
  125. $this->assertTrue(strpos($content, $expected));
  126. }
  127. public function testArchive() {
  128. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  129. $this->skipIf(ini_get('phar.readonly') === '1', 'INI setting phar.readonly = On');
  130. $this->library->library = 'library_test';
  131. $testPath = "{$this->_testPath}/library_test";
  132. $result = $this->library->archive($testPath, $testPath);
  133. $this->assertTrue($result);
  134. $expected = "library_test.phar.gz created in {$this->_testPath} from ";
  135. $expected .= "{$this->_testPath}/library_test\n";
  136. $result = $this->library->response->output;
  137. $this->assertEqual($expected, $result);
  138. Phar::unlinkArchive("{$this->_testPath}/library_test.phar");
  139. }
  140. public function testExtractWithFullPaths() {
  141. $fileExists = file_exists("{$this->_testPath}/library_test.phar.gz");
  142. $this->skipIf(!$fileExists, 'Depends on ' . __CLASS__ . '::testArchive()');
  143. $this->library->library = 'library_test';
  144. $result = $this->library->extract(
  145. $this->_testPath . '/library_test.phar.gz', $this->_testPath . '/new'
  146. );
  147. $this->assertTrue($result);
  148. $this->assertTrue(file_exists($this->_testPath . '/new'));
  149. $expected = "new created in {$this->_testPath} from ";
  150. $expected .= "{$this->_testPath}/library_test.phar.gz\n";
  151. $result = $this->library->response->output;
  152. $this->assertEqual($expected, $result);
  153. $result = file_exists($this->_testPath . '/new/.htaccess');
  154. $this->assertTrue($result);
  155. $result = file_exists($this->_testPath . '/new/.DS_Store');
  156. $this->assertFalse($result);
  157. Phar::unlinkArchive($this->_testPath . '/library_test.phar.gz');
  158. }
  159. public function testArchiveNoLibrary() {
  160. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  161. $this->skipIf(
  162. ini_get('phar.readonly') === '1',
  163. 'INI setting phar.readonly = On'
  164. );
  165. chdir('new');
  166. $app = new Library(array('request' => new Request(), 'classes' => $this->classes));
  167. $app->library = 'does_not_exist';
  168. $result = $app->archive();
  169. $this->assertTrue($result);
  170. $path = realpath($this->_testPath);
  171. $expected = "new.phar.gz created in {$path} from {$path}" . DIRECTORY_SEPARATOR . "new\n";
  172. $result = $app->response->output;
  173. $this->assertEqual($expected, $result);
  174. Phar::unlinkArchive($this->_testPath . '/new.phar');
  175. Phar::unlinkArchive($this->_testPath . '/new.phar.gz');
  176. $this->_cleanUp('tests/new');
  177. }
  178. public function testExtractWhenLibraryDoesNotExist() {
  179. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  180. chdir($this->_testPath);
  181. $app = new Library(array('request' => new Request(), 'classes' => $this->classes));
  182. $app->library = 'does_not_exist';
  183. $result = $app->extract();
  184. $this->assertTrue($result);
  185. $this->assertTrue(file_exists($this->_testPath . '/new'));
  186. $path = realpath($this->_testPath);
  187. $tplPath = realpath(LITHIUM_LIBRARY_PATH . '/lithium/console/command/create/template');
  188. $filePath = $tplPath . DIRECTORY_SEPARATOR . "app.phar.gz";
  189. $expected = "new created in {$path} from {$filePath}\n";
  190. $result = $app->response->output;
  191. $this->assertEqual($expected, $result);
  192. $this->_cleanUp();
  193. }
  194. public function testExtractPlugin() {
  195. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  196. $this->library->library = 'library_plugin_test';
  197. $path = $this->_testPath;
  198. $result = $this->library->extract('plugin', "{$path}/library_test_plugin");
  199. $this->assertTrue($result);
  200. $expected = "library_test_plugin created in {$path} from ";
  201. $target = '/lithium/console/command/create/template/plugin.phar.gz';
  202. $expected .= realpath(LITHIUM_LIBRARY_PATH . $target) . "\n";
  203. $result = $this->library->response->output;
  204. $this->assertEqual($expected, $result);
  205. $this->_cleanup();
  206. }
  207. public function testFormulate() {
  208. $this->library->formulate();
  209. $expected = '/please supply a name/';
  210. $result = $this->library->response->output;
  211. $this->assertPattern($expected, $result);
  212. $path = $this->_testPath . '/library_test_plugin';
  213. mkdir($path);
  214. $result = $this->library->formulate($path);
  215. $this->assertTrue($result);
  216. $result = file_exists($path . '/config/library_test_plugin.json');
  217. $this->assertTrue($result);
  218. $this->_cleanUp();
  219. }
  220. public function testFormulateWithFormula() {
  221. $path = $this->_testPath . '/library_test_plugin';
  222. mkdir($path);
  223. mkdir($path . '/config');
  224. file_put_contents(
  225. $path . '/config/library_test_plugin.json',
  226. json_encode(array(
  227. 'name' => 'library_test_plugin',
  228. 'version' => '1.0',
  229. 'summary' => 'something',
  230. 'sources' => array(
  231. 'phar' => 'http://somewhere.com/download/library_test_plugin.phar.gz'
  232. )
  233. ))
  234. );
  235. $result = $this->library->formulate($path);
  236. $this->assertTrue($result);
  237. $result = file_exists($path . '/config/library_test_plugin.json');
  238. $this->assertTrue($result);
  239. }
  240. public function testNoFormulate() {
  241. $path = $this->_testPath . '/library_test_no_plugin';
  242. $result = $this->library->formulate($path);
  243. $this->assertFalse($result);
  244. $result = file_exists($path . '/config/library_test_no_plugin.json');
  245. $this->assertFalse($result);
  246. $expected = '/Formula for library_test_no_plugin not created/';
  247. $result = $this->library->response->error;
  248. $this->assertPattern($expected, $result);
  249. }
  250. public function testFormulateNoPath() {
  251. $isWin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  252. $this->skipIf($isWin, 'Permissions cannot be modified on Windows.');
  253. $path = $this->_testPath . '/library_test_no_plugin';
  254. umask(0);
  255. mkdir($path, 655);
  256. umask(100);
  257. $this->expectException('/Permission denied/');
  258. $result = $this->library->formulate($path);
  259. $this->assertFalse($result);
  260. $result = file_exists($path . '/config/library_test_plugin.json');
  261. $this->assertFalse($result);
  262. $expected = '/Formula for library_test_no_plugin not created/';
  263. $result = $this->library->response->error;
  264. $this->assertPattern($expected, $result);
  265. umask(0);
  266. rmdir($path);
  267. }
  268. public function testPushNoName() {
  269. $this->library->push();
  270. $expected = 'please supply a name';
  271. $result = $this->library->response->output;
  272. $this->assertTrue($result);
  273. }
  274. public function testPush() {
  275. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  276. $this->skipIf(
  277. ini_get('phar.readonly') === '1',
  278. 'INI setting phar.readonly = On'
  279. );
  280. $result = file_put_contents(
  281. $this->_testPath . '/library_test_plugin/config/library_test_plugin.json',
  282. json_encode(array(
  283. 'name' => 'library_test_plugin',
  284. 'version' => '1.0',
  285. 'summary' => 'something',
  286. 'sources' => array(
  287. 'phar' => 'http://somewhere.com/download/library_test_plugin.phar.gz'
  288. )
  289. ))
  290. );
  291. $this->assertTrue($result);
  292. $result = $this->library->archive(
  293. $this->_testPath . '/library_test_plugin',
  294. $this->_testPath . '/library_test_plugin'
  295. );
  296. $this->assertTrue($result);
  297. $expected = "library_test_plugin.phar.gz created in {$this->_testPath} from ";
  298. $expected .= "{$this->_testPath}/library_test_plugin\n";
  299. $result = $this->library->response->output;
  300. $this->assertEqual($expected, $result);
  301. $result = file_exists($this->_testPath . '/library_test_plugin.phar.gz');
  302. $this->assertTrue($result);
  303. $this->library->response->output = null;
  304. $result = $this->library->push('library_test_plugin');
  305. $this->assertTrue($result);
  306. $expected = "library_test_plugin added to {$this->library->server}.\n";
  307. $expected .= "See http://{$this->library->server}/lab/plugins/view/{$result->id}\n";
  308. $result = $this->library->response->output;
  309. $this->assertEqual($expected, $result);
  310. $result = is_dir($this->_testPath . '/library_test_plugin');
  311. $this->assertTrue($result);
  312. $this->_cleanUp('tests/library_test_plugin');
  313. rmdir($this->_testPath . '/library_test_plugin');
  314. }
  315. public function testInstall() {
  316. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  317. $this->skipIf(
  318. ini_get('phar.readonly') === '1',
  319. 'Relies on ' . __CLASS__ . '::testPush()'
  320. );
  321. $this->library->path = $this->_testPath;
  322. $result = $this->library->install('library_test_plugin');
  323. $this->assertTrue($result);
  324. $result = file_exists($this->_testPath . '/library_test_plugin.phar.gz');
  325. $this->assertTrue($result);
  326. $result = is_dir($this->_testPath . '/library_test_plugin');
  327. $this->assertTrue($result);
  328. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar');
  329. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar.gz');
  330. $this->_cleanUp();
  331. }
  332. public function testNoInstall() {
  333. $result = $this->library->install('library_test_plugin');
  334. $expected = "library_test_plugin not installed.\n";
  335. $result = $this->library->response->output;
  336. $this->assertEqual($expected, $result);
  337. $this->library->response->output = null;
  338. $this->request->params += array('server' => null);
  339. $library = new Library(array(
  340. 'request' => $this->request, 'classes' => $this->classes
  341. ));
  342. $library->conf = $this->testConf;
  343. $library->config('server', 'localhost');
  344. $result = $this->library->install('library_not_a_plugin');
  345. $expected = "library_not_a_plugin not found.\n";
  346. $result = $this->library->response->error;
  347. $this->assertEqual($expected, $result);
  348. }
  349. public function testNoInstalLab() {
  350. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  351. $this->skipIf(ini_get('phar.readonly') === '1', 'Relies on ' . __CLASS__ . '::testPush()');
  352. $this->library->path = $this->_testPath;
  353. $result = $this->library->install('li3_lab');
  354. $expected = "li3_lab not installed.\n";
  355. $result = $this->library->response->output;
  356. $this->assertEqual($expected, $result);
  357. $result = is_dir($this->_testPath . '/li3_lab');
  358. $this->assertFalse($result);
  359. $this->_cleanUp();
  360. }
  361. public function testInstallDocs() {
  362. $hasGit = strpos(shell_exec('git --version'), 'git version');
  363. $this->skipIf($hasGit === false, 'Git is not installed.');
  364. $message = "No internet connection established.";
  365. $this->skipIf(!$this->_hasNetwork(), $message);
  366. $this->library->path = $this->_testPath;
  367. $result = $this->library->install('li3_docs');
  368. $this->assertTrue($result);
  369. $result = is_dir($this->_testPath . '/li3_docs');
  370. $this->assertTrue($result);
  371. $this->_cleanUp();
  372. }
  373. public function testFind() {
  374. $this->library->find();
  375. $expected = <<<'test'
  376. --------------------------------------------------------------------------------
  377. lab.lithify.me > li3_lab
  378. --------------------------------------------------------------------------------
  379. the li3 plugin client/server
  380. Version: 1.0
  381. Created: 2009-11-30
  382. --------------------------------------------------------------------------------
  383. lab.lithify.me > library_test_plugin
  384. --------------------------------------------------------------------------------
  385. an li3 plugin example
  386. Version: 1.0
  387. Created: 2009-11-30
  388. test;
  389. }
  390. public function testFindNotFound() {
  391. $this->request->params += array('server' => null);
  392. $library = new Library(array(
  393. 'request' => $this->request, 'classes' => $this->classes
  394. ));
  395. $library->conf = $this->testConf;
  396. $library->config('server', 'localhost');
  397. $library->find();
  398. $expected = "No plugins at localhost\n";
  399. $result = $library->response->output;
  400. $this->assertEqual($expected, $result);
  401. }
  402. public function testForceArchive() {
  403. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  404. $this->skipIf(
  405. ini_get('phar.readonly') === '1',
  406. 'INI setting phar.readonly = On'
  407. );
  408. $result = $this->library->extract('plugin', $this->_testPath . '/library_test_plugin');
  409. $this->assertTrue($result);
  410. $this->library->response->output = null;
  411. $result = $this->library->archive(
  412. $this->_testPath . '/library_test_plugin',
  413. $this->_testPath . '/library_test_plugin'
  414. );
  415. $this->assertTrue($result);
  416. $expected = "library_test_plugin.phar.gz created in {$this->_testPath} from ";
  417. $expected .= "{$this->_testPath}/library_test_plugin\n";
  418. $result = $this->library->response->output;
  419. $this->assertEqual($expected, $result);
  420. $this->library->response->output = null;
  421. $result = $this->library->archive(
  422. $this->_testPath . '/library_test_plugin',
  423. $this->_testPath . '/library_test_plugin'
  424. );
  425. $this->assertFalse($result);
  426. $expected = "library_test_plugin.phar already exists in {$this->_testPath}\n";
  427. $result = $this->library->response->error;
  428. $this->assertEqual($expected, $result);
  429. $this->library->force = true;
  430. $this->library->response->output = null;
  431. $result = $this->library->archive(
  432. $this->_testPath . '/library_test_plugin',
  433. $this->_testPath . '/library_test_plugin'
  434. );
  435. $this->assertTrue($result);
  436. $expected = "library_test_plugin.phar.gz created in {$this->_testPath} from ";
  437. $expected .= "{$this->_testPath}/library_test_plugin\n";
  438. $result = $this->library->response->output;
  439. $this->assertEqual($expected, $result);
  440. unlink($this->_testPath . '/library_test_plugin.phar');
  441. $this->library->force = false;
  442. $this->library->response->output = null;
  443. $this->library->response->error = null;
  444. $result = $this->library->archive(
  445. $this->_testPath . '/library_test_plugin',
  446. $this->_testPath . '/library_test_plugin'
  447. );
  448. $this->assertFalse($result);
  449. $expected = "library_test_plugin.phar.gz already exists in {$this->_testPath}\n";
  450. $result = $this->library->response->error;
  451. $this->assertEqual($expected, $result);
  452. $this->library->force = true;
  453. $this->library->response->output = null;
  454. $this->library->response->error = null;
  455. $result = $this->library->archive(
  456. $this->_testPath . '/library_test_plugin',
  457. $this->_testPath . '/library_test_plugin'
  458. );
  459. $this->assertTrue($result);
  460. $expected = "library_test_plugin.phar.gz created in {$this->_testPath} from ";
  461. $expected .= "{$this->_testPath}/library_test_plugin\n";
  462. $result = $this->library->response->output;
  463. $this->assertEqual($expected, $result);
  464. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar');
  465. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar.gz');
  466. $this->_cleanUp();
  467. }
  468. public function testPushWithAuth() {
  469. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  470. $this->skipIf(
  471. ini_get('phar.readonly') === '1',
  472. 'INI setting phar.readonly = On'
  473. );
  474. $result = $this->library->extract('plugin', $this->_testPath . '/library_test_plugin');
  475. $this->assertTrue($result);
  476. $result = file_put_contents(
  477. $this->_testPath . '/library_test_plugin/config/library_test_plugin.json',
  478. json_encode(array(
  479. 'name' => 'library_test_plugin',
  480. 'version' => '1.0',
  481. 'summary' => 'something',
  482. 'sources' => array(
  483. 'phar' => 'http://somewhere.com/download/library_test_plugin.phar.gz'
  484. )
  485. ))
  486. );
  487. $this->assertTrue($result);
  488. $result = $this->library->archive(
  489. $this->_testPath . '/library_test_plugin',
  490. $this->_testPath . '/library_test_plugin'
  491. );
  492. $this->assertTrue($result);
  493. $result = file_exists($this->_testPath . '/library_test_plugin.phar.gz');
  494. $this->assertTrue($result);
  495. $this->library->response->output = null;
  496. $this->library->username = 'gwoo';
  497. $this->library->password = 'password';
  498. $result = $this->library->push('library_test_plugin');
  499. $this->assertTrue($result);
  500. $expected = "library_test_plugin added to {$this->library->server}.\n";
  501. $expected .= "See http://{$this->library->server}/lab/plugins/view/{$result->id}\n";
  502. $result = $this->library->response->output;
  503. $this->assertEqual($expected, $result);
  504. $result = file_exists($this->_testPath . '/library_test_plugin');
  505. $this->assertTrue($result);
  506. $this->library->response->error = null;
  507. $this->library->response->output = null;
  508. $this->library->username = 'bob';
  509. $this->library->password = 'password';
  510. $result = $this->library->push('library_test_plugin');
  511. $this->assertFalse($result);
  512. $expected = "Invalid username/password.\n";
  513. $result = $this->library->response->error;
  514. $this->assertEqual($expected, $result);
  515. $result = file_exists($this->_testPath . '/library_test_plugin');
  516. $this->assertTrue($result);
  517. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar');
  518. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar.gz');
  519. $this->_cleanUp();
  520. }
  521. public function testPushNotValid() {
  522. $this->skipIf(!extension_loaded('zlib'), 'The zlib extension is not loaded.');
  523. $this->skipIf(
  524. ini_get('phar.readonly') === '1',
  525. 'INI setting phar.readonly = On'
  526. );
  527. $this->library->library = 'library_plugin_test';
  528. $path = $this->_testPath;
  529. $result = $this->library->extract('plugin', "{$path}/library_test_plugin");
  530. $this->assertTrue($result);
  531. $this->library->response->output = null;
  532. $file = $this->_testPath . '/library_test_plugin/config/library_test_plugin.json';
  533. $result = file_put_contents(
  534. $file,
  535. json_encode(array(
  536. 'name' => 'library_test_plugin',
  537. 'version' => '1.0',
  538. 'summary' => 'something'
  539. ))
  540. );
  541. $this->assertTrue($result);
  542. $result = $this->library->archive(
  543. $this->_testPath . '/library_test_plugin',
  544. $this->_testPath . '/library_test_plugin'
  545. );
  546. $this->assertTrue($result);
  547. $expected = "library_test_plugin.phar.gz created in {$this->_testPath} from ";
  548. $expected .= "{$this->_testPath}/library_test_plugin\n";
  549. $result = $this->library->response->output;
  550. $this->assertEqual($expected, $result);
  551. $result = file_exists($this->_testPath . '/library_test_plugin.phar.gz');
  552. $this->assertTrue($result);
  553. $this->library->response->output = null;
  554. $result = $this->library->push('library_test_plugin');
  555. $this->assertFalse($result);
  556. $expected = "/The formula for library_test_plugin is not valid./";
  557. $result = $this->library->response->error;
  558. $this->assertPattern($expected, $result);
  559. $result = is_dir($this->_testPath . '/library_test_plugin');
  560. $this->assertTrue($result);
  561. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar');
  562. Phar::unlinkArchive($this->_testPath . '/library_test_plugin.phar.gz');
  563. $this->_cleanUp();
  564. }
  565. public function testNoArchive() {
  566. $this->skipIf(
  567. ini_get('phar.readonly') === '1',
  568. 'INI setting phar.readonly = On'
  569. );
  570. $result = $this->library->archive(
  571. $this->_testPath . '/library_test_plugin',
  572. $this->_testPath . '/library_test_plugin'
  573. );
  574. $this->assertFalse($result);
  575. $expected = "/Could not create archive from/";
  576. $result = $this->library->response->error;
  577. $this->assertPattern($expected, $result);
  578. }
  579. }
  580. ?>