1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery\Tests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TheCodingMachine\Discovery\Asset; |
8
|
|
|
use TheCodingMachine\Discovery\AssetOperation; |
9
|
|
|
use TheCodingMachine\Discovery\AssetType; |
10
|
|
|
|
11
|
|
|
class AssetTypeTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testAssetTypeName() |
14
|
|
|
{ |
15
|
|
|
$assetType = new AssetType('type'); |
16
|
|
|
$this->assertSame('type', $assetType->getName()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testAssetTypeAdd() |
20
|
|
|
{ |
21
|
|
|
$operation1 = new AssetOperation(AssetOperation::ADD, new Asset('a', 'package_a', 'dir_a', 0, [])); |
22
|
|
|
$operation2 = new AssetOperation(AssetOperation::ADD, new Asset('b', 'package_b', 'dir_b', -0.5, [])); |
23
|
|
|
$operation3 = new AssetOperation(AssetOperation::ADD, new Asset('c', 'package_c', 'dir_c', 0.5, [])); |
24
|
|
|
$operation4 = new AssetOperation(AssetOperation::ADD, new Asset('d', 'package_d', 'dir_d', -0.5, [])); |
25
|
|
|
$operation5 = new AssetOperation(AssetOperation::ADD, new Asset('e', 'package_e', 'dir_e', 0, [])); |
26
|
|
|
|
27
|
|
|
$assetType = new AssetType('type'); |
28
|
|
|
$assetType->addAssetOperation($operation1); |
29
|
|
|
$assetType->addAssetOperation($operation2); |
30
|
|
|
$assetType->addAssetOperation($operation3); |
31
|
|
|
$assetType->addAssetOperation($operation4); |
32
|
|
|
$assetType->addAssetOperation($operation5); |
33
|
|
|
|
34
|
|
|
$assets = $assetType->getAssets(); |
35
|
|
|
$this->assertCount(5, $assets); |
36
|
|
|
$this->assertSame('c', $assets[0]->getValue()); |
37
|
|
|
$this->assertSame('a', $assets[1]->getValue()); |
38
|
|
|
$this->assertSame('e', $assets[2]->getValue()); |
39
|
|
|
$this->assertSame('b', $assets[3]->getValue()); |
40
|
|
|
$this->assertSame('d', $assets[4]->getValue()); |
41
|
|
|
|
42
|
|
|
// Let's test again to test the "caching" of the sort. |
43
|
|
|
$assets = $assetType->getAssets(); |
44
|
|
|
$this->assertCount(5, $assets); |
45
|
|
|
|
46
|
|
|
$assetValues = $assetType->getValues(); |
47
|
|
|
$this->assertSame(['c', 'a', 'e', 'b', 'd'], $assetValues); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testAssetTypeJson() |
51
|
|
|
{ |
52
|
|
|
$operation1 = new AssetOperation(AssetOperation::ADD, new Asset('a', 'package_a', 'dir_a', 0, [])); |
53
|
|
|
|
54
|
|
|
$assetType = new AssetType('type'); |
55
|
|
|
$assetType->addAssetOperation($operation1); |
56
|
|
|
|
57
|
|
|
$array = json_decode(json_encode($assetType), true); |
58
|
|
|
$this->assertSame([ |
59
|
|
|
[ |
60
|
|
|
'value' => 'a', |
61
|
|
|
'package' => 'package_a', |
62
|
|
|
'packageDir' => 'dir_a', |
63
|
|
|
'priority' => 0, |
64
|
|
|
'metadata' => [], |
65
|
|
|
] |
66
|
|
|
], $array); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testAssetTypeRemove() |
70
|
|
|
{ |
71
|
|
|
$operation1 = new AssetOperation(AssetOperation::ADD, new Asset('a', 'package_a', 'dir_a', 0, [])); |
72
|
|
|
$operation2 = new AssetOperation(AssetOperation::ADD, new Asset('b', 'package_b', 'dir_b', -0.5, [])); |
73
|
|
|
$operation3 = new AssetOperation(AssetOperation::REMOVE, new Asset('a', 'package_b', 'dir_b', -0.5, [])); |
74
|
|
|
|
75
|
|
|
$assetType = new AssetType('type'); |
76
|
|
|
$assetType->addAssetOperation($operation1); |
77
|
|
|
$assetType->addAssetOperation($operation2); |
78
|
|
|
$assetType->addAssetOperation($operation3); |
79
|
|
|
|
80
|
|
|
$assets = $assetType->getAssets(); |
81
|
|
|
$this->assertCount(1, $assets); |
82
|
|
|
$this->assertSame('b', $assets[0]->getValue()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|