PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/AGitRepositoryTest.php

https://github.com/musterknabe/YiiGit
PHP | 164 lines | 117 code | 21 blank | 26 comment | 6 complexity | a7d288539abc080f1ff81754ad8fd59d MD5 | raw file
  1. <?php
  2. require __DIR__."/../AGitRepository.php";
  3. require __DIR__."/../AGitBranch.php";
  4. require __DIR__."/../AGitCommit.php";
  5. require __DIR__."/../AGitException.php";
  6. require __DIR__."/../AGitRemote.php";
  7. require __DIR__."/../AGitTag.php";
  8. class AGitRepositoryTest extends CTestCase {
  9. /**
  10. * The path to the test repository
  11. * @var string
  12. */
  13. public $path = "/tmp/gitrepositorytest/";
  14. /**
  15. * The git repository instance to use for testing
  16. * @var AGitRepository
  17. */
  18. protected $_repository;
  19. /**
  20. * Holds a list of example files to add to the repository
  21. * @var array
  22. */
  23. protected $_files;
  24. /**
  25. * Tests basic functionality
  26. */
  27. public function testBasics() {
  28. $repo = $this->getRepository();
  29. $repo->add($this->getFiles()); // add all the files in one go
  30. $changedFiles = $repo->status();
  31. foreach($this->getFiles() as $file) {
  32. $this->assertTrue(isset($changedFiles[$file]));
  33. }
  34. $commitMessage = "Test Commit: ".uniqid();
  35. $response = $repo->commit($commitMessage); // commit our changes
  36. $this->assertTrue(is_array($response));
  37. foreach($this->getFiles() as $file) {
  38. $this->assertTrue(isset($response[$file])); // check our files were committed
  39. }
  40. $repo->rm($this->getFiles()); // delete our files
  41. foreach($this->getFiles() as $file) {
  42. $this->assertFalse(file_exists($this->path."/".$file)); // check our removal was successful
  43. }
  44. $commitMessage = "Test Commit: ".uniqid();
  45. $response = $repo->commit($commitMessage); // commit our deletions
  46. $this->assertTrue(is_array($response));
  47. }
  48. /**
  49. * Tests the commit() method
  50. */
  51. public function testCommit() {
  52. $repo = $this->getRepository();
  53. $repo->commit("test"); // make sure there are no hidden changes
  54. $this->assertFalse($repo->commit("test")); // no changes, should fail
  55. $files = $this->getFiles();
  56. foreach($files as $file) {
  57. $repo->add($this->path."/".$file);
  58. file_put_contents($this->path."/".$file,uniqid());
  59. }
  60. $commitMessage = "Test Commit: ".uniqid();
  61. $response = $repo->commit($commitMessage,true);
  62. $this->assertTrue(is_array($response));
  63. foreach($this->getFiles() as $file) {
  64. $this->assertTrue(isset($response[$file]));
  65. }
  66. }
  67. public function testCheckout() {
  68. $repo = $this->getRepository();
  69. $this->assertEquals("master",$repo->getActiveBranch()->name);
  70. $branchName = "test-branch-".uniqid();
  71. $repo->checkout($branchName,true);
  72. $this->assertEquals($branchName,$repo->getActiveBranch()->name);
  73. $repo->checkout("master");
  74. $this->assertEquals("master",$repo->getActiveBranch()->name);
  75. }
  76. public function testBranches() {
  77. $repo = $this->getRepository();
  78. foreach($repo->getBranches() as $branch) {
  79. $this->assertTrue($branch->name != "");
  80. }
  81. foreach($repo->getActiveBranch()->getCommits() as $commit) {
  82. $this->assertTrue($commit instanceof AGitCommit);
  83. $this->assertTrue(is_array($commit->getFiles()));
  84. }
  85. }
  86. public function testTags() {
  87. $repo = new AGitRepository();
  88. $repo->path = __DIR__."/../";
  89. $tag = new AGitTag("example-tag");
  90. $tag->message = "This is an example tag that should be deleted";
  91. $repo->getActiveBranch()->removeTag($tag);
  92. $this->assertTrue($repo->getActiveBranch()->addTag($tag) instanceof AGitTag);
  93. $this->assertTrue($tag->hash != "");
  94. $this->assertTrue($repo->getActiveBranch()->hasTag("example-tag"));
  95. foreach($repo->getTags() as $t) {
  96. $this->assertTrue($t->getCommit() instanceof AGitCommit);
  97. }
  98. $this->assertTrue($repo->getActiveBranch()->removeTag($tag));
  99. }
  100. public function testRemotes() {
  101. $repo = new AGitRepository();
  102. $repo->path = __DIR__."/../";
  103. foreach($repo->getRemotes() as $remote) {
  104. $this->assertTrue($remote->name != "");
  105. $this->assertGreaterThan(0,count($remote->getBranches()));
  106. foreach($remote->getBranches() as $branch) {
  107. $this->assertTrue($branch->name != "");
  108. $this->assertGreaterThan(0,count($branch->getCommits()));
  109. }
  110. }
  111. }
  112. /**
  113. * Gets the repository to use for testing
  114. * @return AGitRepository the respository for testing
  115. */
  116. protected function getRepository() {
  117. if ($this->_repository === null) {
  118. $this->_repository = new AGitRepository();
  119. $this->_repository->setPath($this->path,true,true);
  120. $this->assertTrue(file_exists($this->path));
  121. }
  122. return $this->_repository;
  123. }
  124. /**
  125. * Gets an array of filenames that should be added to git
  126. * @return array
  127. */
  128. protected function getFiles() {
  129. if ($this->_files === null) {
  130. $files = array(
  131. "test.txt" => uniqid(),
  132. "test2.txt" => uniqid(),
  133. "test3.txt" => uniqid(),
  134. );
  135. foreach($files as $file => $content) {
  136. file_put_contents($this->path."/".$file,$content);
  137. $this->assertTrue(file_exists($this->path."/".$file));
  138. }
  139. $this->_files = array_keys($files);
  140. }
  141. return $this->_files;
  142. }
  143. }