1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Component test for SimplePackageGenerator |
5
|
|
|
* Writes to temporary locations in the filesystem |
6
|
|
|
*/ |
7
|
|
|
class SimplePackageGeneratorTest extends SapphireTest { |
|
|
|
|
8
|
|
|
|
9
|
|
|
protected $tempPath = null; |
10
|
|
|
|
11
|
|
|
public function setUp() { |
12
|
|
|
// We muck with the filesystem, create one folder that contains all the stuff we create, |
13
|
|
|
// and delete it entirely on tearDown() |
14
|
|
|
$this->tempPath = TEMP_FOLDER . '/SimplePackageGeneratorTest-' . rand(1000000,9999999); |
15
|
|
|
mkdir($this->tempPath); |
16
|
|
|
|
17
|
|
|
} |
18
|
|
|
public function tearDown() { |
19
|
|
|
if($this->tempPath) Filesystem::removeFolder($this->tempPath); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Test that generatePackage produces a .tar.gz containing the git repo. |
24
|
|
|
* Test that the build script is executed. |
25
|
|
|
*/ |
26
|
|
|
public function testGeneratePackage() { |
27
|
|
|
// Build some precursor state for the test |
28
|
|
|
$gitPath = $this->tempPath .'/git-repo'; |
29
|
|
|
$packagePath = $this->tempPath .'/test.tar.gz'; |
30
|
|
|
$sha = $this->createTempGitRepo($gitPath); |
31
|
|
|
$this->assertNotEmpty($sha); |
|
|
|
|
32
|
|
|
|
33
|
|
|
// TO DO: DeploynautLogFile should be replaced with a generic interface (maybe PSR-3) |
34
|
|
|
// and a generic memory-log or mock-log placed in here. |
35
|
|
|
$log = new PipelineTest_MockLog(null); |
36
|
|
|
|
37
|
|
|
// Create the generator and execute the script |
38
|
|
|
$gen = new SimplePackageGenerator; |
39
|
|
|
$gen->setBuildScript("touch build-script-executed.txt"); |
40
|
|
|
$this->assertTrue($gen->generatePackage($sha, $gitPath, $packagePath, $log)); |
|
|
|
|
41
|
|
|
|
42
|
|
|
// Fetch the sorted contents of the package files |
43
|
|
|
$CLI_packagePath = escapeshellarg($packagePath); |
44
|
|
|
$packageFiles = explode("\n", trim(`tar tzf $CLI_packagePath`)); |
45
|
|
|
sort($packageFiles); |
46
|
|
|
|
47
|
|
|
// Confirm the git repo was used as the basis of the package |
48
|
|
|
// Confirm that the build script was executed |
49
|
|
|
// Confirm not junk files included |
50
|
|
|
$this->assertEquals(array( |
|
|
|
|
51
|
|
|
'test/', |
52
|
|
|
'test/README.md', |
53
|
|
|
'test/build-script-executed.txt', |
54
|
|
|
'test/index.php', |
55
|
|
|
), $packageFiles); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create a simple temporary git repo at the given path, for component testing, |
60
|
|
|
* and return the SHA of the EHAD |
61
|
|
|
*/ |
62
|
|
|
protected function createTempGitRepo($path) { |
63
|
|
|
$CLI_path = escapeshellarg($path); |
64
|
|
|
mkdir($path); |
65
|
|
|
`cd $CLI_path && git init`; |
66
|
|
|
file_put_contents("$path/README.md", "This is a test repo"); |
67
|
|
|
file_put_contents("$path/index.php", "<?php\necho 'test';"); |
68
|
|
|
`cd $CLI_path && git add README.md index.php && git commit -m 'first commit'`; |
69
|
|
|
return trim(`cd $CLI_path && git show | grep ^commit | awk ' { print \$2 } '`); |
70
|
|
|
} |
71
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.