forked from LastCallMedia/ComposerExtraFiles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSniffTest.php
123 lines (111 loc) · 4.98 KB
/
SniffTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace LastCall\DownloadsPlugin\Tests;
use ProcessHelper\ProcessHelper as PH;
/**
* Class SniffTest
* @package LastCall\DownloadsPlugin\Tests
*
* This is general integration test of the plugin. It creates an example project which uses the
* current/under-development plugin. The example project includes a handful of downloads which
* employ a cross-section of the configuration options.
*/
class SniffTest extends IntegrationTestCase
{
public static function getComposerJson() {
return parent::getComposerJson() + [
'name' => 'test/sniff-test',
'require' => [
'civicrm/composer-downloads-plugin' => '@dev',
],
'minimum-stability' => 'dev',
'extra' => [
'downloads' => [
'*' => [
'path' => 'extern/{$id}',
],
'README' => [
'url' => 'https://github.com/composer/composer/raw/1.9.0/README.md',
'path' => 'docs/README.md'
],
'jquery-full' => [
'url' => 'https://github.com/civicrm/jquery/archive/1.12.4-civicrm-1.2.zip',
],
'jquery-lesser' => [
'version' => '1.12.4-civicrm-1.2',
'url' => 'https://github.com/civicrm/jquery/archive/{$version}.zip',
'path' => 'extern/jquery-lesser',
'ignore' => ['Gruntfile.js']
],
'cv' => [
'type' => 'phar',
'url' => 'https://download.civicrm.org/cv/cv.phar-2019-08-20-14fe9da8',
'path' => 'bin/cv',
],
'org.civicrm.sms.twilio' => [
'type' => 'git',
'url' => 'https://github.com/civicrm/org.civicrm.sms.twilio@494728cf13bb65c228429665c158d899cd7e6dc1',
'path' => 'web/sites/default/files/civicrm/ext/org.civicrm.sms.twilio',
],
],
],
'config' => [
'allow-plugins' => [
'civicrm/composer-downloads-plugin' => true,
],
],
];
}
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
self::initTestProject(static::getComposerJson());
$composer_path = self::getComposerPath();
PH::runOk("$composer_path install -v");
}
public function getExampleChecksums() {
return [
['docs/README.md', 'docs/README.md', '1d0577cc52d55f0680b431184e898f0cbcb927e52e843a319d7122db9be72813'],
['extern/jquery-full', 'extern/jquery-full/dist/jquery.js', '5f2caf09052782caf67e1772c0abce31747ffbc7a1c50690e331b99c7d9ea8dc'],
['extern/jquery-full', 'extern/jquery-full/Gruntfile.js', '3508ff74f8ef106a80f25f28f44a20c47a2b67d84396bb141928ff978ba4012e'],
['extern/jquery-lesser', 'extern/jquery-lesser/dist/jquery.js', '5f2caf09052782caf67e1772c0abce31747ffbc7a1c50690e331b99c7d9ea8dc'],
['extern/jquery-lesser', 'extern/jquery-lesser/Gruntfile.js', NULL],
['bin/cv', 'bin/cv', 'bf162d5d7dd0bef087d7dd07f474039b2e25c4bcca328a2b2097958ac6294476'],
['web/sites/default/files/civicrm/ext/org.civicrm.sms.twilio', 'web/sites/default/files/civicrm/ext/org.civicrm.sms.twilio/info.xml', 'fce6bbd3af2315b4357393b3fc76b52ebc6ab3c27290c82ce7545f0bf2928109'],
];
}
/**
* Ensure that the file checksums match expectations with both (a) original download and (b) re-download.
*
* @param string $file
* @param string|NULL $sha256
* The expected content of the file, or NULL if the file should not exist.
* @dataProvider getExampleChecksums
*/
public function testDownloadAndRedownload($path, $file, $sha256) {
// Initial download
$this->assertFileChecksum($file, $sha256, 'Initial');
// Force re-download
if (is_dir($path)) {
self::cleanDir($path);
}
else {
unlink($path);
}
$this->assertFileDoesNotExist($file);
$composer_path = self::getComposerPath();
PH::runOk("$composer_path install -v");
// And make sure it all worked out...
$this->assertFileChecksum($file, $sha256, 'Redownload');
}
public function assertFileChecksum($file, $sha256, $message = NULL) {
if ($sha256 === NULL) {
$this->assertFileDoesNotExist($file, "($message) File should not exist");
}
else {
$this->assertFileExists($file, "($message) File should exist");
$this->assertEquals($sha256, hash('sha256', file_get_contents($file)), "($message) File should given checksum");
}
}
private static function getComposerPath() {
return realpath(__DIR__ . '/../vendor/bin/composer');
}
}