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

testSizeRestriction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9713
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/**
4
 * Component test for SizeRestrictedPackageCache
5
 * Writes to temporary locations in the filesystem
6
 */
7
class SizeRestrictedPackageCacheTest 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
	protected $cache, $gen, $log;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
12
13
	public function setUp() {
14
		// We muck with the filesystem, create one folder that contains all the stuff we create,
15
		// and delete it entirely on tearDown()
16
		$this->tempPath = TEMP_FOLDER . '/PackageCacheTest-' . rand(1000000,9999999);
17
		mkdir($this->tempPath);
18
19
		$this->cache = new SizeRestrictedPackageCache;
20
		$this->cache->setCacheSize(3);
21
		mkdir($this->tempPath .'/cache');
22
		$this->cache->setBaseDir($this->tempPath .'/cache');
23
24
		$this->gen = new PackageCacheTest_MockGenerator;
25
		// To do: refactor so as not to be pipelinetest-specific (see also SimplePackageGeneratorTest)
26
		$this->log = new PipelineTest_MockLog(null);
27
28
	}
29
	public function tearDown() {
30
		if($this->tempPath) Filesystem::removeFolder($this->tempPath);
31
	}
32
33
34
	public function testSizeRestriction() {
35
		// This testing relies on the fact that MockGenerator won't use the gitDir
36
37
		// 2 in each project doesn't rupture the cache, all files will exist
38
		$files = array();
39
		$files['project1.a'] = $this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log);
40
		// This is needed so that a.tar.gz is the oldest
41
		sleep(1);
42
		$files['project1.b'] = $this->cache->getPackageFilename($this->gen, 'project1', 'b', null, $this->log);
43
		$files['project2.a'] = $this->cache->getPackageFilename($this->gen, 'project2', 'a', null, $this->log);
44
		$files['project2.b'] = $this->cache->getPackageFilename($this->gen, 'project2', 'b', null, $this->log);
45
46
		$this->assertFileExists($files['project1.a']);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
47
		$this->assertFileExists($files['project1.b']);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
48
		$this->assertFileExists($files['project2.a']);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
49
		$this->assertFileExists($files['project2.b']);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
50
51
		// 4 in the same project will mean the oldest is killed
52
		$files['project1.c'] = $this->cache->getPackageFilename($this->gen, 'project1', 'c', null, $this->log);
53
		$files['project1.d'] = $this->cache->getPackageFilename($this->gen, 'project1', 'd', null, $this->log);
54
55
		$this->assertFileNotExists($files['project1.a']);
0 ignored issues
show
Bug introduced by
The method assertFileNotExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
56
		$this->assertFileExists($files['project1.b']);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
57
	}
58
59
	/**
60
	 * Check that a subsequent re-fetch will mark the file as newer and so not garbage collect it
61
	 */
62
	public function testSizeRestrictionIsLastAccessed() {
63
		// 2 in each project doesn't rupture the cache, all files will exist
64
		$files = array();
65
		$files['project1.a'] = $this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log);
66
		sleep(1);
67
		$files['project1.b'] = $this->cache->getPackageFilename($this->gen, 'project1', 'b', null, $this->log);
68
		sleep(1);
69
		$files['project1.c'] = $this->cache->getPackageFilename($this->gen, 'project1', 'c', null, $this->log);
70
		// Re-fetch a, now B will be deleted before it
71
		$files['project1.a'] = $this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log);
72
		$files['project1.d'] = $this->cache->getPackageFilename($this->gen, 'project1', 'd', null, $this->log);
73
74
		$this->assertFileExists($files['project1.a']);
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
75
		$this->assertFileNotExists($files['project1.b']);
0 ignored issues
show
Bug introduced by
The method assertFileNotExists() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
76
	}
77
78
	/**
79
	 * Check that caching is actually happening
80
	 */
81
	public function testCacheDoesntRegenerateUnnecessarily() {
82
		$this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log);
83
		$this->assertTrue($this->gen->popWasCalled());
1 ignored issue
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
84
		$this->cache->getPackageFilename($this->gen, 'project2', 'a', null, $this->log);
85
		$this->assertTrue($this->gen->popWasCalled());
1 ignored issue
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
86
		$this->cache->getPackageFilename($this->gen, 'project1', 'a', null, $this->log);
87
		$this->assertFalse($this->gen->popWasCalled());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SizeRestrictedPackageCacheTest>.

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...
88
	}
89
90
}
91
92
93
/**
94
 * Stub PackageGenerator that creates an empty file
95
 */
96
class PackageCacheTest_MockGenerator extends PackageGenerator {
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
97
	protected $wasCalled = false;
98
99
	public function getIdentifier() {
100
		return "";
101
	}
102
103
	public function generatePackage($sha, $baseDir, $outputFilename, DeploynautLogFile $log) {
104
		touch($outputFilename);
105
		$this->wasCalled = true;
106
		return true;
107
	}
108
109
	public function popWasCalled() {
110
		if($this->wasCalled) {
111
			$this->wasCalled = false;
112
			return true;
113
		}
114
		return false;
115
	}
116
}