Skip to content

Commit fc0e6b1

Browse files
authored
Merge pull request #4 from nswdpc/feat-ci-files
Automated updates
2 parents 29a3947 + 0bd45e2 commit fc0e6b1

8 files changed

Lines changed: 129 additions & 83 deletions

File tree

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: CI
2+
3+
on:
4+
pull_request: null
5+
6+
jobs:
7+
Silverstripe:
8+
name: "Silverstripe (bundle)"
9+
uses: nswdpc/ci-files/.github/workflows/silverstripe_53_83.yml@v-4
10+
PHPStan:
11+
name: "PHPStan (analyse)"
12+
uses: nswdpc/ci-files/.github/workflows/phpstan.silverstripe_83.yml@v-4
13+
needs: Silverstripe

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
/client/node_modules
2+
/vendor/
13
.DS_Store
24
/.php-cs-fixer.cache
5+
/app/
6+
/public/
7+
/composer.lock

.php-cs-fixer.dist.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

composer.json

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,31 @@
1111
"license": "BSD-3-Clause",
1212
"authors": [
1313
{
14-
"name": "Mark Taylor",
15-
"homepage": "https://dpc.nsw.gov.au",
16-
"role": "Bon Vivant"
14+
"name": "Mark Taylor"
1715
},
1816
{
19-
"name": "James Ellis",
20-
"homepage": "https://dpc.nsw.gov.au",
21-
"role": "Developer"
17+
"name": "James Ellis"
2218
}
2319
],
2420
"require": {
25-
"dnadesign/silverstripe-elemental": "^5"
21+
"dnadesign/silverstripe-elemental": "^5",
22+
"silverstripe/asset-admin": "^2.4"
2623
},
2724
"require-dev": {
2825
"phpunit/phpunit": "^9.5",
29-
"friendsofphp/php-cs-fixer": "^3"
26+
"friendsofphp/php-cs-fixer": "^3",
27+
"cambis/silverstripe-rector": "^2",
28+
"cambis/silverstan": "^2",
29+
"nswdpc/ci-files": "dev-v-4",
30+
"phpstan/phpstan": "^2",
31+
"phpstan/phpstan-phpunit": "^2",
32+
"rector/rector": "^2"
33+
},
34+
"scripts": {
35+
"phpstan-analyse": "./vendor/bin/phpstan analyse --ansi --no-progress --no-interaction --configuration vendor/nswdpc/ci-files/phpstan/.phpstan.silverstripe.neon src/ tests/",
36+
"rector-dryrun": "./vendor/bin/rector process --dry-run --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_53_83.php src/ tests/",
37+
"rector-process": "./vendor/bin/rector process --no-diffs --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_53_83.php src/ tests/",
38+
"phpcsfixer-fix": "./vendor/bin/php-cs-fixer fix --ansi --no-interaction --config vendor/nswdpc/ci-files/php-cs-fixer/.php-cs-fixer.php src/ tests/"
3039
},
3140
"autoload": {
3241
"psr-4": {
@@ -37,5 +46,13 @@
3746
"src/Models/Elements/"
3847
]
3948
}
49+
},
50+
"config": {
51+
"allow-plugins": {
52+
"composer/installers": true,
53+
"silverstripe/vendor-plugin": true,
54+
"silverstripe/recipe-plugin": true,
55+
"phpstan/extension-installer": true
56+
}
4057
}
4158
}

phpunit.xml.dist

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
2-
<testsuite name="nswdpc/silverstripe-elemental-image">
3-
<directory>tests/</directory>
4-
</testsuite>
5-
<filter>
6-
<whitelist addUncoveredFilesFromWhitelist="true">
7-
<directory suffix=".php">src/</directory>
8-
<exclude>
9-
<directory suffix=".php">tests/</directory>
10-
</exclude>
11-
</whitelist>
12-
</filter>
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage includeUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
<exclude>
8+
<directory suffix=".php">tests/</directory>
9+
</exclude>
10+
</coverage>
11+
<testsuite name="nswdpc/silverstripe-elemental-image">
12+
<directory>tests/</directory>
13+
</testsuite>
1314
</phpunit>

src/Models/Elements/ElementImage.php

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,127 @@
11
<?php
2+
23
namespace NSWDPC\Elemental\Models\Image;
34

45
use DNADesign\Elemental\Models\BaseElement;
56
use SilverStripe\AssetAdmin\Forms\UploadField;
67
use SilverStripe\Assets\Image;
78
use SilverStripe\Forms\DropdownField;
89
use SilverStripe\Forms\TextareaField;
10+
use SilverStripe\Forms\CheckboxField;
911

1012
/**
1113
* ElementImage adds an image with some config
14+
* @property ?string $Width
15+
* @property ?string $Height
16+
* @property ?string $Caption
17+
* @property int $ImageID
18+
* @method \SilverStripe\Assets\Image Image()
19+
* @property bool $HideCaption
1220
*/
1321
class ElementImage extends BaseElement
1422
{
15-
private static $icon = "font-icon-image";
23+
private static string $icon = "font-icon-image";
24+
25+
private static string $table_name = "ElementImage";
26+
27+
private static string $title = "Image";
28+
29+
private static string $description = "Display an image";
30+
31+
private static string $singular_name = "Image";
32+
33+
private static string $plural_name = "Images";
34+
35+
private static array $allowed_file_types = ["jpg", "jpeg", "gif", "png", "webp"];
1636

17-
private static $table_name = "ElementImage";
37+
public const WIDTH_FULL = 'full';
1838

19-
private static $title = "Image";
20-
private static $description = "Display an image";
39+
public const WIDTH_CONTAINER = 'container';
2140

22-
private static $singular_name = "Image";
23-
private static $plural_name = "Images";
41+
public const HEIGHT_SMALL = 'small';
2442

25-
private static $allowed_file_types = ["jpg", "jpeg", "gif", "png", "webp"];
43+
public const HEIGHT_MEDIUM = 'medium';
2644

27-
const WIDTH_FULL = 'full';
28-
const WIDTH_CONTAINER = 'container';
45+
public const HEIGHT_LARGE = 'large';
2946

30-
const HEIGHT_SMALL = 'small';
31-
const HEIGHT_MEDIUM = 'medium';
32-
const HEIGHT_LARGE = 'large';
33-
const HEIGHT_ORIGINAL = 'original';
47+
public const HEIGHT_ORIGINAL = 'original';
3448

49+
#[\Override]
3550
public function getType()
3651
{
37-
return _t(__CLASS__ . ".BlockType", "Image");
52+
return _t(self::class . ".BlockType", "Image");
3853
}
3954

40-
private static $db = [
55+
private static array $db = [
4156
"Width" => "Varchar",
4257
"Height" => "Varchar",
4358
'Caption' => 'Text',
59+
"HideCaption" => "Boolean"
4460
];
4561

46-
private static $has_one = [
62+
private static array $has_one = [
4763
"Image" => Image::class,
4864
];
4965

50-
private static $summary_fields = [
66+
private static array $defaults = [
67+
"HideCaption" => 0
68+
];
69+
70+
private static array $summary_fields = [
5171
"Image.CMSThumbnail" => "Image",
5272
"Title" => "Title",
5373
];
5474

55-
private static $owns = ["Image"];
75+
private static array $owns = ["Image"];
5676

57-
public function getAllowedFileTypes()
77+
public function ShowCaption(): bool
78+
{
79+
return $this->HideCaption == 0;
80+
}
81+
82+
public function getAllowedFileTypes(): array
5883
{
5984
$types = $this->config()->get("allowed_file_types");
6085
if (empty($types)) {
6186
$types = ['jpg', 'jpeg', 'gif', 'png', 'webp'];
6287
}
63-
$types = array_unique($types);
64-
return $types;
88+
89+
return array_unique($types);
6590
}
6691

92+
#[\Override]
6793
public function getCMSFields()
6894
{
6995

70-
$this->beforeUpdateCMSFields(function ($fields) {
96+
$this->beforeUpdateCMSFields(function ($fields): void {
7197
$fields->addFieldsToTab("Root.Main", [
7298
DropdownField::create(
7399
"Width",
74-
_t(__CLASS__ . ".WIDTH", "Width"),
100+
_t(self::class . ".WIDTH", "Width"),
75101
[
76-
self::WIDTH_CONTAINER => _t(__CLASS__ . ".CONTAINER_WIDTH", "Content width"),
77-
self::WIDTH_FULL => _t(__CLASS__ . ".BROWSER_WIDTH", "Browser width")
102+
self::WIDTH_CONTAINER => _t(self::class . ".CONTAINER_WIDTH", "Content width"),
103+
self::WIDTH_FULL => _t(self::class . ".BROWSER_WIDTH", "Browser width")
78104
]
79105
),
80106
DropdownField::create(
81107
"Height",
82-
_t(__CLASS__ . ".HEIGHT", "Height"),
108+
_t(self::class . ".HEIGHT", "Height"),
83109
[
84-
self::HEIGHT_SMALL => _t(__CLASS__ . ".HEIGHT_SMALL", "Small"),
85-
self::HEIGHT_MEDIUM => _t(__CLASS__ . ".HEIGHT_MEDIUM", "Medium"),
86-
self::HEIGHT_LARGE => _t(__CLASS__ . ".HEIGHT_LARGE", "Large"),
87-
self::HEIGHT_ORIGINAL => _t(__CLASS__ . ".HEIGHT_ORIGINAL", "Original")
110+
self::HEIGHT_SMALL => _t(self::class . ".HEIGHT_SMALL", "Small"),
111+
self::HEIGHT_MEDIUM => _t(self::class . ".HEIGHT_MEDIUM", "Medium"),
112+
self::HEIGHT_LARGE => _t(self::class . ".HEIGHT_LARGE", "Large"),
113+
self::HEIGHT_ORIGINAL => _t(self::class . ".HEIGHT_ORIGINAL", "Original")
88114
]
89115
),
90116
UploadField::create(
91117
"Image",
92-
_t(__CLASS__ . ".SLIDE_IMAGE", "Image")
118+
_t(self::class . ".SLIDE_IMAGE", "Image")
93119
)
94120
->setAllowedExtensions($this->getAllowedFileTypes())
95121
->setIsMultiUpload(false)
96122
->setDescription(
97123
_t(
98-
__CLASS__ . "ALLOWED_FILE_TYPES",
124+
self::class . "ALLOWED_FILE_TYPES",
99125
"Allowed file types: {types}",
100126
[
101127
'types' => implode(",", $this->getAllowedFileTypes())
@@ -104,7 +130,11 @@ public function getCMSFields()
104130
),
105131
TextareaField::create(
106132
'Caption',
107-
_t(__CLASS__ . ".CAPTION", "Caption")
133+
_t(self::class . ".CAPTION", "Caption")
134+
),
135+
CheckboxField::create(
136+
"HideCaption",
137+
_t(self::class . ".HIDE_CAPTION", "Hide Caption")
108138
)
109139
]);
110140
});

templates/NSWDPC/Elemental/Models/Image/ElementImage.ss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="image">
66
{$Image}
77
</div>
8-
<% if $Caption %>
8+
<% if $Caption && $ShowCaption %>
99
<p class="caption">{$Caption}</p>
1010
<% end_if %>
1111
<% end_if %>

tests/ElementImageTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace NSWDPC\Elemental\Models\Image\Tests;
46

57
use SilverStripe\Dev\SapphireTest;
@@ -8,10 +10,9 @@
810

911
class ElementImageTest extends SapphireTest
1012
{
11-
1213
protected $usesDatabase = true;
1314

14-
public function testAllowedFileTypes()
15+
public function testAllowedFileTypes(): void
1516
{
1617
$allowed = ["jpg", "jpeg"];
1718
Config::modify()->set(
@@ -20,28 +21,28 @@ public function testAllowedFileTypes()
2021
$allowed
2122
);
2223
$element = ElementImage::create();
23-
$this->assertEquals( $allowed, $element->getAllowedFileTypes() );
24+
$this->assertEquals($allowed, $element->getAllowedFileTypes());
2425
}
2526

26-
public function testDimensions()
27+
public function testDimensions(): void
2728
{
2829
$width = ElementImage::WIDTH_CONTAINER;
2930
$height = ElementImage::HEIGHT_LARGE;
3031
$element = ElementImage::create([
3132
'Width' => $width,
3233
'Height' => $height
3334
]);
34-
$this->assertEquals( $width, $element->Width );
35-
$this->assertEquals( $height, $element->Height );
35+
$this->assertEquals($width, $element->Width);
36+
$this->assertEquals($height, $element->Height);
3637
}
3738

38-
public function testCaption()
39+
public function testCaption(): void
3940
{
4041
$caption = 'Test caption';
4142
$element = ElementImage::create([
4243
'Caption' => $caption
4344
]);
44-
$this->assertEquals( $caption, $element->Caption );
45+
$this->assertEquals($caption, $element->Caption);
4546
}
4647

4748
}

0 commit comments

Comments
 (0)