MediaWiki REL1_33
GitInfoTest.php
Go to the documentation of this file.
1<?php
6
7 private static $tempDir;
8
9 public static function setUpBeforeClass() {
10 self::$tempDir = wfTempDir() . '/mw-phpunit-' . wfRandomString( 8 );
11 if ( !mkdir( self::$tempDir ) ) {
12 self::$tempDir = null;
13 throw new Exception( 'Unable to create temporary directory' );
14 }
15 mkdir( self::$tempDir . '/gitrepo' );
16 mkdir( self::$tempDir . '/gitrepo/1' );
17 mkdir( self::$tempDir . '/gitrepo/2' );
18 mkdir( self::$tempDir . '/gitrepo/3' );
19 mkdir( self::$tempDir . '/gitrepo/1/.git' );
20 mkdir( self::$tempDir . '/gitrepo/1/.git/refs' );
21 mkdir( self::$tempDir . '/gitrepo/1/.git/refs/heads' );
22 file_put_contents( self::$tempDir . '/gitrepo/1/.git/HEAD',
23 "ref: refs/heads/master\n" );
24 file_put_contents( self::$tempDir . '/gitrepo/1/.git/refs/heads/master',
25 "0123456789012345678901234567890123abcdef\n" );
26 file_put_contents( self::$tempDir . '/gitrepo/1/.git/packed-refs',
27 "abcdef6789012345678901234567890123456789 refs/heads/master\n" );
28 file_put_contents( self::$tempDir . '/gitrepo/2/.git',
29 "gitdir: ../1/.git\n" );
30 file_put_contents( self::$tempDir . '/gitrepo/3/.git',
31 'gitdir: ' . self::$tempDir . "/gitrepo/1/.git\n" );
32 }
33
34 public static function tearDownAfterClass() {
35 if ( self::$tempDir ) {
36 wfRecursiveRemoveDir( self::$tempDir );
37 }
38 }
39
40 protected function setUp() {
41 parent::setUp();
42 $this->setMwGlobals( 'wgGitInfoCacheDirectory', __DIR__ . '/../data/gitinfo' );
43 }
44
45 protected function assertValidGitInfo( GitInfo $gitInfo ) {
46 $this->assertTrue( $gitInfo->cacheIsComplete() );
47 $this->assertEquals( 'refs/heads/master', $gitInfo->getHead() );
48 $this->assertEquals( '0123456789abcdef0123456789abcdef01234567',
49 $gitInfo->getHeadSHA1() );
50 $this->assertEquals( '1070884800', $gitInfo->getHeadCommitDate() );
51 $this->assertEquals( 'master', $gitInfo->getCurrentBranch() );
52 $this->assertContains( '0123456789abcdef0123456789abcdef01234567',
53 $gitInfo->getHeadViewUrl() );
54 }
55
56 public function testValidJsonData() {
57 global $IP;
58
59 $this->assertValidGitInfo( new GitInfo( "$IP/testValidJsonData" ) );
60 $this->assertValidGitInfo( new GitInfo( __DIR__ . "/../data/gitinfo/extension" ) );
61 }
62
63 public function testMissingJsonData() {
64 $dir = $GLOBALS['IP'] . '/testMissingJsonData';
65 $fixture = new GitInfo( $dir );
66
67 $this->assertFalse( $fixture->cacheIsComplete() );
68
69 $this->assertEquals( false, $fixture->getHead() );
70 $this->assertEquals( false, $fixture->getHeadSHA1() );
71 $this->assertEquals( false, $fixture->getHeadCommitDate() );
72 $this->assertEquals( false, $fixture->getCurrentBranch() );
73 $this->assertEquals( false, $fixture->getHeadViewUrl() );
74
75 // After calling all the outputs, the cache should be complete
76 $this->assertTrue( $fixture->cacheIsComplete() );
77 }
78
79 public function testReadingHead() {
80 $dir = self::$tempDir . '/gitrepo/1';
81 $fixture = new GitInfo( $dir );
82
83 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
84 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
85 }
86
87 public function testIndirection() {
88 $dir = self::$tempDir . '/gitrepo/2';
89 $fixture = new GitInfo( $dir );
90
91 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
92 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
93 }
94
95 public function testIndirection2() {
96 $dir = self::$tempDir . '/gitrepo/3';
97 $fixture = new GitInfo( $dir );
98
99 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
100 $this->assertEquals( '0123456789012345678901234567890123abcdef', $fixture->getHeadSHA1() );
101 }
102
103 public function testReadingPackedRefs() {
104 $dir = self::$tempDir . '/gitrepo/1';
105 unlink( self::$tempDir . '/gitrepo/1/.git/refs/heads/master' );
106 $fixture = new GitInfo( $dir );
107
108 $this->assertEquals( 'refs/heads/master', $fixture->getHead() );
109 $this->assertEquals( 'abcdef6789012345678901234567890123456789', $fixture->getHeadSHA1() );
110 }
111}
$GLOBALS['IP']
wfTempDir()
Tries to get the system directory for temporary files.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
wfRecursiveRemoveDir( $dir)
Remove a directory and all its content.
$IP
Definition WebStart.php:41
GitInfo.
static setUpBeforeClass()
assertValidGitInfo(GitInfo $gitInfo)
static $tempDir
static tearDownAfterClass()
getHead()
Get the HEAD of the repo (without any opening "ref: ")
Definition GitInfo.php:167
getHeadCommitDate()
Get the commit date of HEAD entry of the git code repository.
Definition GitInfo.php:223
cacheIsComplete()
Check to see if the current cache is fully populated.
Definition GitInfo.php:344
getHeadViewUrl()
Get an URL to a web viewer link to the HEAD revision.
Definition GitInfo.php:279
getHeadSHA1()
Get the SHA1 for the current HEAD of the repo.
Definition GitInfo.php:191
getCurrentBranch()
Get the name of the current branch, or HEAD if not found.
Definition GitInfo.php:261
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37