<?php
declare(strict_types=1);
namespace Tests\git2;
use git2\git;
use git2\git_commit;
use git2\git_object;
use git2\git_object_t;
final class ObjectTest extends GitTestCase
{
public function testCast(): void
{
$dir = $this->mkdir('cast');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($aut, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::signature_new($com, 'Jane Doe', 'jane.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $aut, $com, null, 'Hello world!', $tree);
$this->assertOK(git::object_lookup($object, $repo, $id, git_object_t::COMMIT));
$this->assertInstanceOf(git_object::class, $object);
$commit = $object->as(git_commit::class);
$this->assertInstanceOf(git_commit::class, $commit);
$commit2 = $object->as(git_commit::class);
$this->assertSame($commit, $commit2);
$objectBack = $commit->as(git_object::class);
$this->assertSame($object, $objectBack);
$this->assertSame('Hello world!', git::commit_message($object->as(git_commit::class)));
$cid = git::commit_id($commit);
$oid = git::object_id($objectBack);
$oid2 = git::object_id($object);
$oid3 = git::object_id($commit);
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($cid));
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($oid));
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($oid2));
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($oid3));
$this->assertSame($repo, git::object_owner($commit));
$this->assertOK(git::object_peel($peeled, $object, git_object_t::COMMIT));
$this->assertSame($commit->as(git_object::class), $peeled);
$this->assertOK(git::object_short_id($buf, $object));
$this->assertSame('560b089', (string) $buf);
}
public function testCast2(): void
{
$dir = $this->mkdir('cast2');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($aut, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::signature_new($com, 'Jane Doe', 'jane.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $aut, $com, null, 'Hello world!', $tree);
$this->assertOK(git::commit_lookup($commit, $repo, $id));
$this->assertInstanceOf(git_commit::class, $commit);
$object = $commit->as(git_object::class);
$this->assertInstanceOf(git_object::class, $object);
$object2 = $commit->as(git_object::class);
$this->assertInstanceOf(git_object::class, $object2);
$this->assertSame($object, $object2);
$commitBack = $object->as(git_commit::class);
$this->assertInstanceOf(git_commit::class, $commitBack);
$this->assertSame($commit, $commitBack);
$cid = git::commit_id($commit);
$cid2 = git::object_id($commitBack);
$oid = git::object_id($object);
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($cid));
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($cid2));
$this->assertSame('560b089fc8e0c7b8cf44c609c161fb53f058f76d', git::oid_tostr_s($oid));
}
public function testCast3(): void
{
$dir = $this->mkdir('cast3');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($aut, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::signature_new($com, 'Jane Doe', 'jane.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $aut, $com, null, 'Hello world!', $tree);
$this->assertOK(git::commit_lookup($commit, $repo, $id));
$this->assertInstanceOf(git_commit::class, $commit);
$this->assertOK(git::object_lookup($object, $repo, $id, git_object_t::COMMIT));
$this->assertInstanceOf(git_object::class, $object);
$this->assertSame($commit, $object->as(git_commit::class));
$this->assertSame($object, $commit->as(git_object::class));
}
public function testDup(): void
{
$dir = $this->mkdir('dup');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($aut, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::signature_new($com, 'Jane Doe', 'jane.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $aut, $com, null, 'Hello world!', $tree);
git::object_lookup($object, $repo, $id, git_object_t::COMMIT);
$this->assertOK(git::object_dup($dup, $object));
$this->assertInstanceOf(git_object::class, $dup);
}
public function testLookupByPath(): void
{
$dir = $this->mkdir('lookup_by_path');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
touch($dir . '/foo');
git::index_add_bypath($index, 'foo');
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($aut, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::signature_new($com, 'Jane Doe', 'jane.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $aut, $com, null, 'Hello world!', $tree);
$this->assertOK(git::object_lookup_bypath($object, $tree, 'foo', git_object_t::BLOB));
$this->assertInstanceOf(git_object::class, $object);
}
public function testLookupByPrefix(): void
{
$dir = $this->mkdir('lookup_by_prefix');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($aut, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::signature_new($com, 'Jane Doe', 'jane.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $aut, $com, null, 'Hello world!', $tree);
$this->assertOK(git::oid_fromstrn($prefix, '560b089'));
$this->assertOK(git::object_lookup_prefix($object, $repo, $prefix, 7, git_object_t::COMMIT));
$this->assertInstanceOf(git_object::class, $object);
}
public function testString2Type(): void
{
$this->assertSame(git_object_t::COMMIT, git::object_string2type('commit'));
$this->assertSame(git_object_t::BLOB, git::object_string2type('blob'));
$this->assertSame(git_object_t::INVALID, git::object_string2type('foo'));
}
public function testType2String(): void
{
$this->assertSame('commit', git::object_type2string(git_object_t::COMMIT));
$this->assertSame('', git::object_type2string(git_object_t::INVALID));
}
public function testTypeIsLoose(): void
{
$this->assertTrue(git::object_typeisloose(git_object_t::COMMIT));
$this->assertFalse(git::object_typeisloose(git_object_t::OFS_DELTA));
}
}