Share blob size loading code
Share blob size loading code

<?php <?php
/** /**
* Manages creating and caching git object classes * Manages creating and caching git object classes
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2012 Christopher Han * @copyright Copyright (c) 2012 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Git * @subpackage Git
*/ */
class GitPHP_GitObjectManager implements GitPHP_Observer_Interface class GitPHP_GitObjectManager implements GitPHP_Observer_Interface
{ {
/** /**
* The project * The project
* *
* @var GitPHP_Project * @var GitPHP_Project
*/ */
protected $project; protected $project;
   
/** /**
* Cache instance * Cache instance
* *
* @var GitPHP_Cache * @var GitPHP_Cache
*/ */
protected $cache = null; protected $cache = null;
   
/** /**
* MemoryCache instance * MemoryCache instance
* *
* @var GitPHP_MemoryCache * @var GitPHP_MemoryCache
*/ */
protected $memoryCache = null; protected $memoryCache = null;
   
/** /**
* Compatibility mode * Compatibility mode
* *
* @var boolean * @var boolean
*/ */
protected $compat = false; protected $compat = false;
   
/** /**
* Executable * Executable
* *
* @var GitPHP_GitExe * @var GitPHP_GitExe
*/ */
protected $exe; protected $exe;
   
/** /**
* Object loader * Object loader
* *
* @var GitPHP_GitObjectLoader * @var GitPHP_GitObjectLoader
*/ */
protected $objectLoader; protected $objectLoader;
   
/** /**
* Constructor * Constructor
* *
* @param GitPHP_Project $project project * @param GitPHP_Project $project project
*/ */
public function __construct($project) public function __construct($project)
{ {
if (!$project) if (!$project)
throw new Exception('Project is required'); throw new Exception('Project is required');
   
$this->project = $project; $this->project = $project;
} }
   
/** /**
* Set executable * Set executable
* *
* @param GitPHP_GitExe $exe executable * @param GitPHP_GitExe $exe executable
*/ */
public function SetExe($exe) public function SetExe($exe)
{ {
$this->exe = $exe; $this->exe = $exe;
} }
   
/** /**
* Set object loader * Set object loader
* *
* @param GitPHP_GitObjectLoader $objectLoader object loader * @param GitPHP_GitObjectLoader $objectLoader object loader
*/ */
public function SetObjectLoader($objectLoader) public function SetObjectLoader($objectLoader)
{ {
$this->objectLoader = $objectLoader; $this->objectLoader = $objectLoader;
} }
   
/** /**
* Get project * Get project
* *
* @return GitPHP_Project * @return GitPHP_Project
*/ */
public function GetProject() public function GetProject()
{ {
return $this->project; return $this->project;
} }
   
/** /**
* Gets the cache instance being used * Gets the cache instance being used
* *
* @return GitPHP_Cache|null cache instance * @return GitPHP_Cache|null cache instance
*/ */
public function GetCache() public function GetCache()
{ {
return $this->cache; return $this->cache;
} }
   
/** /**
* Set the cache instance to use * Set the cache instance to use
* *
* @param GitPHP_Cache|null $cache cache instance * @param GitPHP_Cache|null $cache cache instance
*/ */
public function SetCache($cache) public function SetCache($cache)
{ {
$this->cache = $cache; $this->cache = $cache;
} }
   
/** /**
* Gets the memory cache instance being used * Gets the memory cache instance being used
* *
* @return GitPHP_MemoryCache|null memory cache instance * @return GitPHP_MemoryCache|null memory cache instance
*/ */
public function GetMemoryCache() public function GetMemoryCache()
{ {
return $this->memoryCache; return $this->memoryCache;
} }
   
/** /**
* Sets the memory cache instance to use * Sets the memory cache instance to use
* *
* @param GitPHP_MemoryCache|null $memoryCache memory cache instance * @param GitPHP_MemoryCache|null $memoryCache memory cache instance
*/ */
public function SetMemoryCache($memoryCache) public function SetMemoryCache($memoryCache)
{ {
$this->memoryCache = $memoryCache; $this->memoryCache = $memoryCache;
} }
   
/** /**
* Gets the compatibility mode * Gets the compatibility mode
* *
* @return boolean * @return boolean
*/ */
public function GetCompat() public function GetCompat()
{ {
return $this->compat; return $this->compat;
} }
   
/** /**
* Sets the compatibility mode * Sets the compatibility mode
* *
* @param boolean $compat compatibility mode * @param boolean $compat compatibility mode
*/ */
public function SetCompat($compat) public function SetCompat($compat)
{ {
$this->compat = $compat; $this->compat = $compat;
} }
   
/** /**
* Get a commit * Get a commit
* *
* @param string $hash commit hash * @param string $hash commit hash
* @return GitPHP_Commit|null commit objet * @return GitPHP_Commit|null commit objet
*/ */
public function GetCommit($hash) public function GetCommit($hash)
{ {
if (empty($hash)) if (empty($hash))
return null; return null;
   
if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash)) { if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash)) {
$fullHash = $this->project->ExpandHash($hash); $fullHash = $this->project->ExpandHash($hash);
if ($fullHash == $hash) if ($fullHash == $hash)
throw new GitPHP_InvalidHashException($hash); throw new GitPHP_InvalidHashException($hash);
$hash = $fullHash; $hash = $fullHash;
} }
   
if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash)) if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash))
return null; return null;
   
$key = GitPHP_Commit::CacheKey($this->project->GetProject(), $hash); $key = GitPHP_Commit::CacheKey($this->project->GetProject(), $hash);
   
$commit = null; $commit = null;
if ($this->memoryCache) if ($this->memoryCache)
$commit = $this->memoryCache->Get($key); $commit = $this->memoryCache->Get($key);
   
if (!$commit) { if (!$commit) {
   
if ($this->cache) { if ($this->cache) {
$commit = $this->cache->Get($key); $commit = $this->cache->Get($key);
} }
   
$strategy = null; $strategy = null;
if ($this->compat) { if ($this->compat) {
$strategy = new GitPHP_CommitLoad_Git($this->exe); $strategy = new GitPHP_CommitLoad_Git($this->exe);
} else { } else {
$strategy = new GitPHP_CommitLoad_Raw($this->objectLoader, $this->exe); $strategy = new GitPHP_CommitLoad_Raw($this->objectLoader, $this->exe);
} }
   
if ($commit) { if ($commit) {
$commit->SetProject($this->project); $commit->SetProject($this->project);
$commit->SetStrategy($strategy); $commit->SetStrategy($strategy);
} else { } else {
$commit = new GitPHP_Commit($this->project, $hash, $strategy); $commit = new GitPHP_Commit($this->project, $hash, $strategy);
} }
   
$commit->AddObserver($this); $commit->AddObserver($this);
   
if ($this->memoryCache) if ($this->memoryCache)
$this->memoryCache->Set($key, $commit); $this->memoryCache->Set($key, $commit);
   
} }
   
return $commit; return $commit;
} }
   
/** /**
* Gets a single tag * Gets a single tag
* *
* @param string $tag tag to find * @param string $tag tag to find
* @param string $hash hash of tag, if known * @param string $hash hash of tag, if known
* @return GitPHP_Tag tag object * @return GitPHP_Tag tag object
*/ */
public function GetTag($tag, $hash = '') public function GetTag($tag, $hash = '')
{ {
if (empty($tag)) if (empty($tag))
return null; return null;
   
$key = GitPHP_Tag::CacheKey($this->project->GetProject(), $tag); $key = GitPHP_Tag::CacheKey($this->project->GetProject(), $tag);
   
$tagObj = null; $tagObj = null;
if ($this->memoryCache) if ($this->memoryCache)
$tagObj = $this->memoryCache->Get($key); $tagObj = $this->memoryCache->Get($key);
   
if (!$tagObj) { if (!$tagObj) {
   
if ($this->cache) { if ($this->cache) {
$tagObj = $this->cache->Get($key); $tagObj = $this->cache->Get($key);
} }
   
$strategy = null; $strategy = null;
if ($this->compat) { if ($this->compat) {
$strategy = new GitPHP_TagLoad_Git($this->exe); $strategy = new GitPHP_TagLoad_Git($this->exe);
} else { } else {
$strategy = new GitPHP_TagLoad_Raw($this->objectLoader); $strategy = new GitPHP_TagLoad_Raw($this->objectLoader);
} }
   
if ($tagObj) { if ($tagObj) {
$tagObj->SetProject($this->project); $tagObj->SetProject($this->project);
$tagObj->SetStrategy($strategy); $tagObj->SetStrategy($strategy);
} else { } else {
$tagObj = new GitPHP_Tag($this->project, $tag, $strategy, $hash); $tagObj = new GitPHP_Tag($this->project, $tag, $strategy, $hash);
} }
   
$tagObj->AddObserver($this); $tagObj->AddObserver($this);
   
if ($this->memoryCache) if ($this->memoryCache)
$this->memoryCache->Set($key, $tagObj); $this->memoryCache->Set($key, $tagObj);
} }
   
return $tagObj; return $tagObj;
} }
   
/** /**
* Gets a single head * Gets a single head
* *
* @param string $head head to find * @param string $head head to find
* @param string $hash hash of head, if known * @param string $hash hash of head, if known
* @return GitPHP_Head head object * @return GitPHP_Head head object
*/ */
public function GetHead($head, $hash = '') public function GetHead($head, $hash = '')
{ {
if (empty($head)) if (empty($head))
return null; return null;
   
$key = GitPHP_Head::CacheKey($this->project->GetProject(), $head); $key = GitPHP_Head::CacheKey($this->project->GetProject(), $head);
   
$headObj = null; $headObj = null;
if ($this->memoryCache) if ($this->memoryCache)
$headObj = $this->memoryCache->Get($key); $headObj = $this->memoryCache->Get($key);
   
if (!$headObj) { if (!$headObj) {
$headObj = new GitPHP_Head($this->project, $head, $hash); $headObj = new GitPHP_Head($this->project, $head, $hash);
   
if ($this->memoryCache) if ($this->memoryCache)
$this->memoryCache->Set($key, $headObj); $this->memoryCache->Set($key, $headObj);
} }
   
return $headObj; return $headObj;
} }
   
/** /**
* Gets a blob * Gets a blob
* *
* @param string $hash blob hash * @param string $hash blob hash
* @return GitPHP_Blob blob object * @return GitPHP_Blob blob object
*/ */
public function GetBlob($hash) public function GetBlob($hash)
{ {
if (empty($hash)) if (empty($hash))
return null; return null;
   
if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash) && !$this->compat) { if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash) && !$this->compat) {
$fullHash = $this->project->ExpandHash($hash); $fullHash = $this->project->ExpandHash($hash);
if ($fullHash == $hash) if ($fullHash == $hash)
throw new GitPHP_InvalidHashException($hash); throw new GitPHP_InvalidHashException($hash);
$hash = $fullHash; $hash = $fullHash;
} }
   
if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash)) if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash))
return null; return null;
   
$key = GitPHP_Blob::CacheKey($this->project->GetProject(), $hash); $key = GitPHP_Blob::CacheKey($this->project->GetProject(), $hash);
   
$blob = null; $blob = null;
if ($this->memoryCache) if ($this->memoryCache)
$blob = $this->memoryCache->Get($key); $blob = $this->memoryCache->Get($key);
   
if (!$blob) { if (!$blob) {
   
if ($this->cache) { if ($this->cache) {
$blob = $this->cache->Get($key); $blob = $this->cache->Get($key);
} }
   
$strategy = null; $strategy = null;
if ($this->compat) { if ($this->compat) {
$strategy = new GitPHP_BlobLoad_Git($this->exe); $strategy = new GitPHP_BlobLoad_Git($this->exe);
} else { } else {
$strategy = new GitPHP_BlobLoad_Raw($this->objectLoader); $strategy = new GitPHP_BlobLoad_Raw($this->objectLoader, $this->exe);
} }
   
if ($blob) { if ($blob) {
$blob->SetProject($this->project); $blob->SetProject($this->project);
$blob->SetStrategy($strategy); $blob->SetStrategy($strategy);
} else { } else {
$blob = new GitPHP_Blob($this->project, $hash, $strategy); $blob = new GitPHP_Blob($this->project, $hash, $strategy);
} }
   
$blob->AddObserver($this); $blob->AddObserver($this);
   
if ($this->memoryCache) if ($this->memoryCache)
$this->memoryCache->Set($key, $blob); $this->memoryCache->Set($key, $blob);
} }
   
return $blob; return $blob;
} }
   
/** /**
* Gets a tree * Gets a tree
* *
* @param string $hash tree hash * @param string $hash tree hash
* @return GitPHP_Tree tree object * @return GitPHP_Tree tree object
*/ */
public function GetTree($hash) public function GetTree($hash)
{ {
if (empty($hash)) if (empty($hash))
return null; return null;
   
if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash) && !$this->compat) { if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash) && !$this->compat) {
$fullHash = $this->project->ExpandHash($hash); $fullHash = $this->project->ExpandHash($hash);
if ($fullHash == $hash) if ($fullHash == $hash)
throw new GitPHP_InvalidHashException($hash); throw new GitPHP_InvalidHashException($hash);
$hash = $fullHash; $hash = $fullHash;
} }
   
if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash)) if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash))
return null; return null;
   
$key = GitPHP_Tree::CacheKey($this->project->GetProject(), $hash); $key = GitPHP_Tree::CacheKey($this->project->GetProject(), $hash);
$tree = null; $tree = null;
if ($this->memoryCache) if ($this->memoryCache)
$tree = $this->memoryCache->Get($key); $tree = $this->memoryCache->Get($key);
   
if (!$tree) { if (!$tree) {
   
if ($this->cache) { if ($this->cache) {
$tree = $this->cache->Get($key); $tree = $this->cache->Get($key);
} }
   
$strategy = null; $strategy = null;
if ($this->compat) { if ($this->compat) {
$strategy = new GitPHP_TreeLoad_Git($this->exe); $strategy = new GitPHP_TreeLoad_Git($this->exe);
} else { } else {
$strategy = new GitPHP_TreeLoad_Raw($this->objectLoader, $this->exe); $strategy = new GitPHP_TreeLoad_Raw($this->objectLoader, $this->exe);
} }
if ($tree) { if ($tree) {
$tree->SetProject($this->project); $tree->SetProject($this->project);
$tree->SetStrategy($strategy); $tree->SetStrategy($strategy);
} else { } else {
$tree = new GitPHP_Tree($this->project, $hash, $strategy); $tree = new GitPHP_Tree($this->project, $hash, $strategy);
} }
   
$tree->AddObserver($this); $tree->AddObserver($this);
   
if ($this->memoryCache) if ($this->memoryCache)
$this->memoryCache->Set($key, $tree); $this->memoryCache->Set($key, $tree);
} }
   
return $tree; return $tree;
} }
   
/** /**
* Gets a file diff * Gets a file diff
* *
* @param string $fromHash source hash, can also be a diff-tree info line * @param string $fromHash source hash, can also be a diff-tree info line
* @param string $toHash target hash, required if $fromHash is a hash * @param string $toHash target hash, required if $fromHash is a hash
* @return GitPHP_FileDiff file diff object * @return GitPHP_FileDiff file diff object
*/ */
public function GetFileDiff($fromHash, $toHash = '') public function GetFileDiff($fromHash, $toHash = '')
{ {
if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $fromHash) && !$this->compat) { if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $fromHash) && !$this->compat) {
$fullHash = $this->project->ExpandHash($fromHash); $fullHash = $this->project->ExpandHash($fromHash);
if ($fullHash == $fromHash) if ($fullHash == $fromHash)
throw new GitPHP_InvalidHashException($fromHash); throw new GitPHP_InvalidHashException($fromHash);
$fromHash = $fullHash; $fromHash = $fullHash;
} }
   
if (!empty($toHash) && preg_match('/^[0-9A-Fa-f]{4,39}$/', $toHash) && !$this->compat) { if (!empty($toHash) && preg_match('/^[0-9A-Fa-f]{4,39}$/', $toHash) && !$this->compat) {
$fullHash = $this->project->ExpandHash($toHash); $fullHash = $this->project->ExpandHash($toHash);
if ($fullHash == $toHash) if ($fullHash == $toHash)
throw new GitPHP_InvalidHashException($toHash); throw new GitPHP_InvalidHashException($toHash);
$toHash = $fullHash; $toHash = $fullHash;
} }
   
$fileDiff = new GitPHP_FileDiff($this->project, $fromHash, $toHash); $fileDiff = new GitPHP_FileDiff($this->project, $fromHash, $toHash);
$fileDiff->SetCache($this->cache); $fileDiff->SetCache($this->cache);
return $fileDiff; return $fileDiff;
} }
   
/** /**
* Notify that observable object changed * Notify that observable object changed
* *
* @param GitPHP_Observable_Interface $object object * @param GitPHP_Observable_Interface $object object
* @param int $changeType type of change * @param int $changeType type of change
* @param array $args argument array * @param array $args argument array
*/ */
public function ObjectChanged($object, $changeType, $args = array()) public function ObjectChanged($object, $changeType, $args = array())
{ {
if (!$object) if (!$object)
return; return;
   
if ($changeType !== GitPHP_Observer_Interface::CacheableDataChange) if ($changeType !== GitPHP_Observer_Interface::CacheableDataChange)
return; return;
   
if (!$this->cache) if (!$this->cache)
return; return;
   
if (!(($object instanceof GitPHP_Observable_Interface) && ($object instanceof GitPHP_Cacheable_Interface))) if (!(($object instanceof GitPHP_Observable_Interface) && ($object instanceof GitPHP_Cacheable_Interface)))
return; return;
   
$this->cache->Set($object->GetCacheKey(), $object); $this->cache->Set($object->GetCacheKey(), $object);
} }
   
} }
   
  <?php
  /**
  * Base blob load strategoy
  *
  * @author Christopher Han <xiphux@gmail.com>
  * @copyright Copyright (c) 2013 Christopher Han
  * @package GitPHP
  * @subpackage Git\Blob
  */
  abstract class GitPHP_BlobLoad_Base implements GitPHP_BlobLoadStrategy_Interface
  {
  /**
  * Executable
  *
  * @var GitPHP_GitExe
  */
  protected $exe;
 
  /**
  * Constructor
  *
  * @param GitPHP_GitExe $exe executable
  */
  public function __construct($exe)
  {
  if (!$exe)
  throw new Exception('Git exe is required');
 
  $this->exe = $exe;
  }
 
  /**
  * Load blob size using git
  *
  * @param GitPHP_Blob $blob blob
  * @return int blob size
  */
  protected function LoadSize($blob)
  {
  if (!$blob)
  return;
 
  $args = array();
  $args[] = '-s';
  $args[] = $blob->GetHash();
 
  return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
  }
  }
 
<?php <?php
/** /**
* Blob load strategy using git exe * Blob load strategy using git exe
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2012 Christopher Han * @copyright Copyright (c) 2012 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Git\Blob * @subpackage Git\Blob
*/ */
class GitPHP_BlobLoad_Git implements GitPHP_BlobLoadStrategy_Interface class GitPHP_BlobLoad_Git extends GitPHP_BlobLoad_Base
{ {
/**  
* Executable  
*  
* @var GitPHP_GitExe  
*/  
protected $exe;  
   
/**  
* Constructor  
*  
* @param GitPHP_GitExe $exe executable  
*/  
public function __construct($exe)  
{  
if (!$exe)  
throw new Exception('Git exe is required');  
   
$this->exe = $exe;  
}  
   
/** /**
* Gets the data for a blob * Gets the data for a blob
* *
* @param GitPHP_Blob $blob blob * @param GitPHP_Blob $blob blob
* @return string blob data * @return string blob data
*/ */
public function Load($blob) public function Load($blob)
{ {
if (!$blob) if (!$blob)
return; return;
   
$args = array(); $args = array();
$args[] = 'blob'; $args[] = 'blob';
$args[] = $blob->GetHash(); $args[] = $blob->GetHash();
   
return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args); return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
} }
   
/** /**
* Gets the size of a blob * Gets the size of a blob
* *
* @param GitPHP_Blob $blob blob * @param GitPHP_Blob $blob blob
* @return int blob size * @return int blob size
*/ */
public function Size($blob) public function Size($blob)
{ {
if (!$blob) return $this->LoadSize($blob);
return;  
   
$args = array();  
$args[] = '-s';  
$args[] = $blob->GetHash();  
   
return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);  
} }
} }
   
<?php <?php
/** /**
* Blob load strategy using raw git objects * Blob load strategy using raw git objects
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2012 Christopher Han * @copyright Copyright (c) 2012 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Git\Blob * @subpackage Git\Blob
*/ */
class GitPHP_BlobLoad_Raw implements GitPHP_BlobLoadStrategy_Interface class GitPHP_BlobLoad_Raw extends GitPHP_BlobLoad_Base
{ {
/** /**
* Object loader * Object loader
* *
* @var GitPHP_GitObjectLoader * @var GitPHP_GitObjectLoader
*/ */
protected $objectLoader; protected $objectLoader;
   
/** /**
* Constructor * Constructor
* *
* @param GitPHP_GitObjectLoader $objectLoader object loader * @param GitPHP_GitObjectLoader $objectLoader object loader
  * @param GitPHP_GitExe $exe git exe
*/ */
public function __construct($objectLoader) public function __construct($objectLoader, $exe)
{ {
if (!$objectLoader) if (!$objectLoader)
throw new Exception('Git object loader is required'); throw new Exception('Git object loader is required');
   
$this->objectLoader = $objectLoader; $this->objectLoader = $objectLoader;
   
  parent::__construct($exe);
} }
   
/** /**
* Gets the data for a blob * Gets the data for a blob
* *
* @param GitPHP_Blob $blob blob * @param GitPHP_Blob $blob blob
* @return string blob data * @return string blob data
*/ */
public function Load($blob) public function Load($blob)
{ {
if (!$blob) if (!$blob)
return; return;
   
return $this->objectLoader->GetObject($blob->GetHash()); return $this->objectLoader->GetObject($blob->GetHash());
} }
   
/** /**
* Gets the size of a blob * Gets the size of a blob
* *
* @param GitPHP_Blob $blob blob * @param GitPHP_Blob $blob blob
* @return int blob size * @return int blob size
*/ */
public function Size($blob) public function Size($blob)
{ {
if (!$blob) if (!$blob)
return; return;
   
return strlen($blob->GetData()); return strlen($blob->GetData());
} }
} }
   
<?php <?php
/** /**
* Raw blob load test class * Raw blob load test class
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2012 Christopher Han * @copyright Copyright (c) 2012 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Git\Test\Blob * @subpackage Git\Test\Blob
*/ */
class GitPHP_BlobLoad_RawTest extends PHPUnit_Framework_TestCase class GitPHP_BlobLoad_RawTest extends PHPUnit_Framework_TestCase
{ {
public function testLoad() public function testLoad()
{ {
$blobmock = $this->getMockBuilder('GitPHP_Blob')->disableOriginalConstructor()->getMock(); $blobmock = $this->getMockBuilder('GitPHP_Blob')->disableOriginalConstructor()->getMock();
$blobmock->expects($this->any())->method('GetHash')->will($this->returnValue('1234567890abcdef1234567890ABCDEF12345678')); $blobmock->expects($this->any())->method('GetHash')->will($this->returnValue('1234567890abcdef1234567890ABCDEF12345678'));
$loadermock = $this->getMockBuilder('GitPHP_GitObjectLoader')->disableOriginalConstructor()->getMock(); $loadermock = $this->getMockBuilder('GitPHP_GitObjectLoader')->disableOriginalConstructor()->getMock();
$loadermock->expects($this->once())->method('GetObject')->with($this->equalTo('1234567890abcdef1234567890ABCDEF12345678'))->will($this->returnValue("blob line 1\nblob line 2")); $loadermock->expects($this->once())->method('GetObject')->with($this->equalTo('1234567890abcdef1234567890ABCDEF12345678'))->will($this->returnValue("blob line 1\nblob line 2"));
  $exemock = $this->getMock('GitPHP_GitExe');
   
$strategy = new GitPHP_BlobLoad_Raw($loadermock); $strategy = new GitPHP_BlobLoad_Raw($loadermock, $exemock);
$this->assertEquals("blob line 1\nblob line 2", $strategy->Load($blobmock)); $this->assertEquals("blob line 1\nblob line 2", $strategy->Load($blobmock));
} }
} }
   
comments