1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\Maintenance\Tests\Model; |
4
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\Maintenance\Model\Package; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\ORM\ArrayList; |
8
|
|
|
use SilverStripe\View\ArrayData; |
9
|
|
|
|
10
|
|
|
class PackageTest extends SapphireTest |
11
|
|
|
{ |
12
|
|
|
public function providePackageNamesAndTitles() |
13
|
|
|
{ |
14
|
|
|
return [ |
15
|
|
|
['pretendvendor/silverstripe-prefixedpackage', 'prefixedpackage'], |
16
|
|
|
['pretend-vendor/silverstripe-hyphen-package', 'hyphen-package'], |
17
|
|
|
['pretendvendor/somepackage', 'somepackage'], |
18
|
|
|
['pretend-vendor/silverstripepackage', 'silverstripepackage'], |
19
|
|
|
['pretendvendor/hyphenated-package', 'hyphenated-package'], |
20
|
|
|
['silverstripe/module', 'module'], |
21
|
|
|
['silverstripe/some-thing', 'some-thing'], |
22
|
|
|
['silverstripe/silverstripe-silverstripe-thing', 'silverstripe-thing'], |
23
|
|
|
['silverstripe-themes/silverstripe-theme', 'theme'], |
24
|
|
|
['silverstripe-themes/silverstripe-hyphenated-theme', 'hyphenated-theme'], |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @dataProvider providePackageNamesAndTitles |
30
|
|
|
* |
31
|
|
|
* Ensure the vendor and 'silverstripe-' is stripped from module names. |
32
|
|
|
*/ |
33
|
|
|
public function testTitleFormatsNameCorrectly($name, $title) |
34
|
|
|
{ |
35
|
|
|
$testPackage = new Package([ |
36
|
|
|
'Name' => $name |
37
|
|
|
]); |
38
|
|
|
$this->assertEquals($title, $testPackage->getTitle()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Ensure that the definition key is always the output title |
43
|
|
|
* and that the value is set as the Type. |
44
|
|
|
*/ |
45
|
|
|
public function testBadges() |
46
|
|
|
{ |
47
|
|
|
$testPackage = new Package(); |
48
|
|
|
|
49
|
|
|
// setBadges to test |
50
|
|
|
$setBadges = [ |
51
|
|
|
'A good Badge' => 'good', |
52
|
|
|
'A typeless badge' => null |
53
|
|
|
]; |
54
|
|
|
$testPackage->setBadges($setBadges); |
55
|
|
|
|
56
|
|
|
// test addBadge appends badge to the stored list |
57
|
|
|
$addedBadgeTitle = 'Integer badge'; |
58
|
|
|
$addedBadgeValue = 3; |
59
|
|
|
$testPackage->addBadge($addedBadgeTitle, $addedBadgeValue); |
60
|
|
|
|
61
|
|
|
// tests adding badges via getBadges optional parameter |
62
|
|
|
$extraBadge = ['Extra' => 'warning']; |
63
|
|
|
|
64
|
|
|
// combine the input data to test outputs against |
65
|
|
|
$badgeControlSample = array_merge($setBadges, [$addedBadgeTitle => $addedBadgeValue], $extraBadge); |
66
|
|
|
|
67
|
|
|
$badgeViewData = $testPackage->getBadges($extraBadge); |
68
|
|
|
|
69
|
|
|
// Test expected data structure is correct |
70
|
|
|
$this->assertInstanceOf(ArrayList::class, $badgeViewData); |
71
|
|
|
$this->assertContainsOnlyInstancesOf(ArrayData::class, $badgeViewData->toArray()); |
72
|
|
|
|
73
|
|
|
// Test that the output format is correct |
74
|
|
|
// and that all our input is output |
75
|
|
|
reset($badgeControlSample); |
76
|
|
|
foreach ($badgeViewData as $badgeData) { |
77
|
|
|
$title = key($badgeControlSample); |
78
|
|
|
$type = current($badgeControlSample); |
79
|
|
|
$this->assertSame( |
80
|
|
|
[ |
81
|
|
|
'Title' => $title, |
82
|
|
|
'Type' => $type, |
83
|
|
|
], |
84
|
|
|
$badgeData->toMap() |
85
|
|
|
); |
86
|
|
|
// badgeControlSample is a keyed array, so shift the pointer manually |
87
|
|
|
// (because we can't lookup by index) |
88
|
|
|
next($badgeControlSample); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|