UpdaterGithubStrategyTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Humbug
  4. *
  5. * @category Humbug
  6. * @package Humbug
  7. * @subpackage UnitTests
  8. * @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
  9. * @license https://github.com/padraic/pharupdater/blob/master/LICENSE New BSD License
  10. */
  11. namespace Humbug\Test\SelfUpdate;
  12. use Humbug\SelfUpdate\Updater;
  13. use Humbug\SelfUpdate\Strategy\GithubStrategy;
  14. class UpdaterGithubStrategyTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $files;
  17. /** @var Updater */
  18. private $updater;
  19. private $tmp;
  20. private $data;
  21. public function setup()
  22. {
  23. $this->tmp = sys_get_temp_dir();
  24. $this->files = __DIR__ . '/_files';
  25. $this->updater = new Updater($this->files . '/test.phar', false, Updater::STRATEGY_GITHUB);
  26. }
  27. public function teardown()
  28. {
  29. $this->deleteTempPhars();
  30. }
  31. public function testConstruction()
  32. {
  33. $updater = new Updater(null, false, Updater::STRATEGY_GITHUB);
  34. $this->assertTrue(
  35. $updater->getStrategy() instanceof GithubStrategy
  36. );
  37. }
  38. public function testSetCurrentLocalVersion()
  39. {
  40. $this->updater->getStrategy()->setCurrentLocalVersion('1.0');
  41. $this->assertEquals(
  42. '1.0',
  43. $this->updater->getStrategy()->getCurrentLocalVersion($this->updater)
  44. );
  45. }
  46. public function testSetPharName()
  47. {
  48. $this->updater->getStrategy()->setPharName('foo.phar');
  49. $this->assertEquals(
  50. 'foo.phar',
  51. $this->updater->getStrategy()->getPharName()
  52. );
  53. }
  54. public function testSetPackageName()
  55. {
  56. $this->updater->getStrategy()->setPackageName('foo/bar');
  57. $this->assertEquals(
  58. 'foo/bar',
  59. $this->updater->getStrategy()->getPackageName()
  60. );
  61. }
  62. public function testSetStability()
  63. {
  64. $this->assertEquals(
  65. 'stable',
  66. $this->updater->getStrategy()->getStability()
  67. );
  68. $this->updater->getStrategy()->setStability('unstable');
  69. $this->assertEquals(
  70. 'unstable',
  71. $this->updater->getStrategy()->getStability()
  72. );
  73. }
  74. public function testSetStabilityThrowsExceptionOnInvalidStabilityValue()
  75. {
  76. $this->setExpectedException(
  77. 'Humbug\\SelfUpdate\\Exception\\InvalidArgumentException'
  78. );
  79. $this->updater->getStrategy()->setStability('foo');
  80. }
  81. /**
  82. * @runInSeparateProcess
  83. */
  84. public function testUpdatePhar()
  85. {
  86. if (!extension_loaded('openssl')) {
  87. $this->markTestSkipped('This test requires the openssl extension to run.');
  88. }
  89. $this->createTestPharAndKey();
  90. $this->assertEquals('old', $this->getPharOutput($this->tmp . '/old.phar'));
  91. $updater = new Updater($this->tmp . '/old.phar');
  92. $updater->setStrategyObject(new GithubTestStrategy);
  93. $updater->getStrategy()->setPharName('new.phar');
  94. $updater->getStrategy()->setPackageName('humbug/test-phar');
  95. $updater->getStrategy()->setCurrentLocalVersion('1.0.0');
  96. $this->assertTrue($updater->update());
  97. $this->assertEquals('new', $this->getPharOutput($this->tmp . '/old.phar'));
  98. }
  99. /**
  100. * Helpers
  101. */
  102. private function getPharOutput($path)
  103. {
  104. return exec('php ' . escapeshellarg($path));
  105. }
  106. private function deleteTempPhars()
  107. {
  108. @unlink($this->tmp . '/old.phar');
  109. @unlink($this->tmp . '/old.phar.pubkey');
  110. @unlink($this->tmp . '/releases/download/1.0.1/new.phar');
  111. @unlink($this->tmp . '/releases/download/1.0.1/new.phar.pubkey');
  112. @unlink($this->tmp . '/old.1c7049180abee67826d35ce308c38272242b64b8.phar');
  113. @unlink($this->tmp . '/packages.json');
  114. }
  115. private function createTestPharAndKey()
  116. {
  117. copy($this->files.'/build/old.phar', $this->tmp.'/old.phar');
  118. chmod($this->tmp.'/old.phar', 0755);
  119. copy(
  120. $this->files.'/build/old.phar.pubkey',
  121. $this->tmp.'/old.phar.pubkey'
  122. );
  123. @mkdir($this->tmp.'/releases/download/1.0.1', 0755, true);
  124. copy($this->files.'/build/new.phar', $this->tmp.'/releases/download/1.0.1/new.phar');
  125. file_put_contents($this->tmp . '/packages.json', json_encode(array(
  126. 'packages' => array(
  127. 'humbug/test-phar' => array(
  128. '1.0.1' => array(
  129. 'source' => array(
  130. 'url' => 'file://' . $this->tmp . '.git'
  131. )
  132. ),
  133. '1.0.0' => array(
  134. )
  135. )
  136. )
  137. )));
  138. }
  139. }
  140. class GithubTestStrategy extends GithubStrategy
  141. {
  142. protected function getApiUrl()
  143. {
  144. return 'file://' . sys_get_temp_dir() . '/packages.json';
  145. }
  146. }