Completed
Pull Request — master (#513)
by Helpful
04:05
created

SimplePackageGeneratorTest::createTempGitRepo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
/**
4
 * Component test for SimplePackageGenerator
5
 * Writes to temporary locations in the filesystem
6
 */
7
class SimplePackageGeneratorTest extends SapphireTest {
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<SimplePackageGeneratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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));
1 ignored issue
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SimplePackageGeneratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SimplePackageGeneratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}