Implement Blob, Tree and Lazy, add tests for Tree.
[phpgit.git] / tests / Git / TreeTest.php
blob2b17459be3353b9d32427bdf97eafc2d897732e6
1 <?php
3 class Git_TreeTest extends GitHarness {
4 protected $repo, $tree, $git;
5 public function setUp() {
6 $this->repo = new Git_Repo(GIT_REPO);
7 $this->git = $this->repo->git = new GitMock();
8 $this->tree = new Git_Tree($this->repo);
10 function testContentsShouldCache() {
11 $this->git->setReturnValue('__call', $this->fixture('ls_tree_a') . $this->fixture('ls_tree_b'));
12 $this->git->expectCallCount('__call', 2);
13 $this->git->expectAt(0, '__call', array('lsTree', array('master', array())));
14 $this->git->expectAt(1, '__call', array('lsTree', array('34868e6e7384cb5ee51c543a8187fdff2675b5a7', array())));
15 $tree = $this->repo->tree('master');
16 list($child) = array_slice($tree->contents, -1);
17 $child->contents;
18 $child->contents;
20 function testContentFromStringTreeShouldReturnTree() {
21 list($text) = array_slice(explode("\n", trim($this->fixture('ls_tree_a'))), -1);
22 $tree = $this->tree->contentFromString(null, $text);
23 $this->assertIsA($tree, 'Git_Tree');
24 $this->assertIdentical('650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44', $tree->id);
25 $this->assertIdentical('040000', $tree->mode);
26 $this->assertIdentical('test', $tree->name);
28 function testContentFromStringTreeShouldReturnBlob() {
29 list($text) = explode("\n", trim($this->fixture('ls_tree_b')));
30 $blob = $this->tree->contentFromString(null, $text);
31 $this->assertIsA($blob, 'Git_Blob');
32 $this->assertIdentical('aa94e396335d2957ca92606f909e53e7beaf3fbb', $blob->id);
33 $this->assertIdentical('100644', $blob->mode);
34 $this->assertIdentical('grit.rb', $blob->name);
36 function testContentFromStringTreeShouldReturnCommit() {
37 list($x, $text) = explode("\n", trim($this->fixture('ls_tree_commit')));
38 $tree = $this->tree->contentFromString(null, $text);
39 $this->assertIdentical($tree, null);
41 function testContentFromStringInvalidTypeShouldThrowException() {
42 $this->expectException(new Git_Exception('Invalid type: bogus'));
43 $this->tree->contentFromString(null, "040000 bogus 650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44\ttest");
45 function testGet() {
46 // Python implementation patched blob here, but it doesn't seem
47 // to ever be called. Weird. They then go on to test with zero-length
48 // files, which shouldn't have any bearing either. We have condensed
49 // those tests.
50 $this->git->setReturnValue('__call', $this->fixture('ls_tree_a'));
51 $this->git->expectOnce('__call', array('lsTree', array('master', array())));
52 $tree = $this->repo->tree('master');
53 $this->assertIdentical('aa06ba24b4e3f463b3c4a85469d0fb9e5b421cf8', $tree->get('lib')->id);
54 $this->assertIdentical('8b1e02c0fb554eed2ce2ef737a68bb369d7527df', $tree->get('README.txt')->id);
56 function testGetWithCommits() {
57 $this->git->setReturnValue('__call', $this->fixture('ls_tree_commit'));
58 $this->git->expectOnce('__call', array('lsTree', array('master', array())));
59 $tree = $this->repo->tree('master');
60 $this->assertIdentical($tree->get('bar'), null);
61 $this->assertIdentical('2afb47bcedf21663580d5e6d2f406f08f3f65f19', $tree->get('foo')->id);
62 $this->assertIdentical('f623ee576a09ca491c4a27e48c0dfe04be5f4a2e', $tree->get('baz')->id);
64 function testToString() {
65 $tree = new Git_Tree($this->repo, array('id' => 'abc'));
66 $this->assertIdentical('(Git_Tree "abc")', (string) $tree);