Skip to content

Commit 5a2bad8

Browse files
authored
Set resource manager defaults (#30)
* Set resource manager defaults increase to php 8 * Increase php version
1 parent 65c9041 commit 5a2bad8

File tree

9 files changed

+95
-60
lines changed

9 files changed

+95
-60
lines changed

.circleci/config.yml

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@ defaults: &defaults
22
steps:
33
# common php steps
44
- run: echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
5+
- run: if [ -n "$ADD_PACKAGES" ]; then apk -U add $ADD_PACKAGES; fi;
6+
- run: if [ -n "$ADD_MODULES" ]; then docker-php-ext-install $ADD_MODULES; fi;
57
- run: echo "date.timezone = UTC" >> $(php --ini |grep Scan |awk '{print $NF}')/timezone.ini
68
- run: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
79

8-
# install apcu
9-
- run: |
10-
docker-php-source extract \
11-
&& apk add --no-cache --virtual .phpize-deps-configure $PHPIZE_DEPS \
12-
&& printf "\n" | pecl install apcu \
13-
&& docker-php-ext-enable apcu \
14-
&& apk del .phpize-deps-configure \
15-
&& docker-php-source delete
10+
# pre-checkout steps
1611

1712
# checkout
1813
- checkout
@@ -27,28 +22,28 @@ defaults: &defaults
2722

2823
version: 2
2924
jobs:
30-
build-php71:
25+
build-php74:
3126
<<: *defaults
3227
docker:
33-
- image: php:7.1-alpine
34-
build-php72:
28+
- image: php:7.4-alpine
29+
build-php80:
3530
<<: *defaults
3631
docker:
37-
- image: php:7.2-alpine
38-
build-php73:
32+
- image: php:8.0-alpine
33+
build-php81:
3934
<<: *defaults
4035
docker:
41-
- image: php:7.3-alpine
42-
build-php74:
36+
- image: php:8.1-alpine
37+
build-php82:
4338
<<: *defaults
4439
docker:
45-
- image: php:7.4-alpine
40+
- image: php:8.2-alpine
4641

4742
workflows:
4843
version: 2
4944
build:
5045
jobs:
51-
- build-php71
52-
- build-php72
53-
- build-php73
5446
- build-php74
47+
- build-php80
48+
- build-php81
49+
- build-php82

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=7.1",
1312
"packaged/helpers": "^1.0||^2.0",
1413
"packaged/http": "~1.1",
1514
"packaged/config": "~1.4",
@@ -20,7 +19,7 @@
2019
},
2120
"require-dev": {
2221
"ext-apcu": "*",
23-
"phpunit/phpunit": "7.5.*"
22+
"phpunit/phpunit": "10"
2423
},
2524
"autoload": {
2625
"psr-4": {

src/ResourceManager.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class ResourceManager
4848
protected $_componentPath;
4949
protected $_options = [];
5050

51+
protected static $defaultOptions = [
52+
self::OPT_THROW_ON_FILE_NOT_FOUND => true,
53+
self::OPT_RESOURCE_STORE => null,
54+
];
55+
5156
/**
5257
* @var Dispatch|null Dispatch in use for components to calculate paths
5358
*/
@@ -464,7 +469,7 @@ public function getFileHash($fullPath)
464469

465470
public function getOption($option, $default = null)
466471
{
467-
return $this->_options[$option] ?? $default;
472+
return $this->_options[$option] ?? $this->_defaultOption($option, $default);
468473
}
469474

470475
/**
@@ -533,4 +538,20 @@ public static function clearCache()
533538
static::$cmc = [];
534539
static::$_fileHashCache = [];
535540
}
541+
542+
/**
543+
* @param string $key
544+
* @param mixed $value
545+
*
546+
* @return void
547+
*/
548+
public static function setDefaultOption(string $key, $value)
549+
{
550+
static::$defaultOptions[$key] = $value;
551+
}
552+
553+
protected function _defaultOption(string $key, $default = null)
554+
{
555+
return static::$defaultOptions[$key] ?? $default;
556+
}
536557
}

tests/ComponentTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testComponent()
3434
$request = Request::create('/' . $uri);
3535
$response = $dispatch->handleRequest($request);
3636
$this->assertEquals(200, $response->getStatusCode());
37-
$this->assertContains('body{color:red}', $response->getContent());
37+
$this->assertStringContainsString('body{color:red}', $response->getContent());
3838

3939
Dispatch::instance()->store()->clearStore(ResourceStore::TYPE_CSS);
4040
$this->assertCount(0, Dispatch::instance()->store()->getResources(ResourceStore::TYPE_CSS));
@@ -44,7 +44,7 @@ public function testComponent()
4444
$request = Request::create('/' . $manager->getResourceUri('style.css'));
4545
$response = $dispatch->handleRequest($request);
4646
$this->assertEquals(200, $response->getStatusCode());
47-
$this->assertContains('body{color:red}', $response->getContent());
47+
$this->assertStringContainsString('body{color:red}', $response->getContent());
4848

4949
//Required for testing correct namespace validation
5050
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents\DemoComponent', 'DC');
@@ -56,7 +56,7 @@ public function testComponent()
5656
$request = Request::create('/c/3/_/MissingComponent/DemoComponent/a4197ed8/style.css');
5757
$response = $dispatch->handleRequest($request);
5858
$this->assertEquals(404, $response->getStatusCode());
59-
$this->assertContains('Component Not Found', $response->getContent());
59+
$this->assertStringContainsString('Component Not Found', $response->getContent());
6060

6161
$manager = ResourceManager::component(new ChildComponent());
6262
$uri = $manager->getResourceUri('style.css');

tests/DispatchTest.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,37 @@ public function testHandle()
5454
$request = Request::create('/r/bd04a611ed6d/css/test.css');
5555
$response = $dispatch->handleRequest($request);
5656
$this->assertEquals(200, $response->getStatusCode());
57-
$this->assertContains('url(r/395d1a0e8999/img/x.jpg)', $response->getContent());
57+
$this->assertStringContainsString('url(r/395d1a0e8999/img/x.jpg)', $response->getContent());
5858

5959
$uri = ResourceManager::public()->getResourceUri('css/placeholder.css');
6060
$request = Request::create($uri);
6161
$response = $dispatch->handleRequest($request);
62-
$this->assertContains('font-size:14px', $response->getContent());
63-
$this->assertContains('background:url(p/942e325b1780/img/test.svg#test)', $response->getContent());
62+
$this->assertStringContainsString('font-size:14px', $response->getContent());
63+
$this->assertStringContainsString('background:url(p/942e325b1780/img/test.svg#test)', $response->getContent());
6464

6565
$dispatch->addAlias('abc', 'resources/css');
6666
$request = Request::create('/a/abc/bd04a611ed6d/test.css');
6767
$response = $dispatch->handleRequest($request);
68-
$this->assertContains('url("a/abc/942e325be95f/sub/subimg.jpg")', $response->getContent());
68+
$this->assertStringContainsString('url("a/abc/942e325be95f/sub/subimg.jpg")', $response->getContent());
6969

7070
$uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css');
7171
$request = Request::create($uri);
7272
$response = $dispatch->handleRequest($request);
73-
$this->assertContains('body{background:orange}', $response->getContent());
73+
$this->assertStringContainsString('body{background:orange}', $response->getContent());
7474

7575
$dispatch->addVendorAlias('packaged', 'dispatch', 'pdsp');
7676
$uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css');
77-
self::assertContains('v/pdsp', $uri);
77+
self::assertStringContainsString('v/pdsp', $uri);
7878
$request = Request::create($uri);
7979
$response = $dispatch->handleRequest($request);
80-
$this->assertContains('body{background:orange}', $response->getContent());
80+
$this->assertStringContainsString('body{background:orange}', $response->getContent());
8181

8282
Dispatch::instance()->config()->addItem('ext.css', 'sourcemap', true);
8383
$uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css');
8484
$request = Request::create($uri);
8585
$response = $dispatch->handleRequest($request);
86-
$this->assertContains('sourceMappingURL', $response->getContent());
87-
$this->assertContains('Q1NTLU1BUAo', $response->getContent());
86+
$this->assertStringContainsString('sourceMappingURL', $response->getContent());
87+
$this->assertStringContainsString('Q1NTLU1BUAo', $response->getContent());
8888

8989
Dispatch::destroy();
9090
}
@@ -95,7 +95,10 @@ public function testBaseUri()
9595
Dispatch::bind($dispatch);
9696
$request = Request::create('/r/bd04a611ed6d/css/test.css');
9797
$response = $dispatch->handleRequest($request);
98-
$this->assertContains('url(http://assets.packaged.in/r/395d1a0e8999/img/x.jpg)', $response->getContent());
98+
$this->assertStringContainsString(
99+
'url(http://assets.packaged.in/r/395d1a0e8999/img/x.jpg)',
100+
$response->getContent()
101+
);
99102
Dispatch::destroy();
100103
}
101104

@@ -105,10 +108,10 @@ public function testStore()
105108
ResourceManager::resources()->requireCss('css/test.css');
106109
ResourceManager::resources()->requireCss('css/do-not-modify.css');
107110
$response = Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_CSS);
108-
$this->assertContains('href="http://assets.packaged.in/r/bd04a6113c11/css/test.css"', $response);
111+
$this->assertStringContainsString('href="http://assets.packaged.in/r/bd04a6113c11/css/test.css"', $response);
109112
ResourceManager::resources()->requireJs('js/alert.js');
110113
$response = Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_JS);
111-
$this->assertContains('src="http://assets.packaged.in/r/f417133ec50f/js/alert.js"', $response);
114+
$this->assertStringContainsString('src="http://assets.packaged.in/r/f417133ec50f/js/alert.js"', $response);
112115
Dispatch::destroy();
113116
}
114117

@@ -166,11 +169,11 @@ public function testWebpReplacements()
166169

167170
$request = Request::create(ResourceManager::resources()->getResourceUri('css/webptest.css'));
168171
$response = $dispatch->handleRequest($request);
169-
$this->assertContains(
172+
$this->assertStringContainsString(
170173
'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png?abc=def#xyz)',
171174
$response->getContent()
172175
);
173-
$this->assertNotContains(
176+
$this->assertStringNotContainsString(
174177
'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png.webp?abc=def#xyz)',
175178
$response->getContent()
176179
);
@@ -182,11 +185,11 @@ public function testWebpReplacements()
182185
$request = Request::create(ResourceManager::resources()->getResourceUri('css/webptest.css'));
183186
$response = $dispatch->handleRequest($request);
184187

185-
$this->assertNotContains(
188+
$this->assertStringNotContainsString(
186189
'url(http://assets.packaged.in/r/30c60da9f504-1/img/test-sample.png?abc=def#xyz)',
187190
$response->getContent()
188191
);
189-
$this->assertContains(
192+
$this->assertStringContainsString(
190193
'url(http://assets.packaged.in/r/d6e2937fee66-1/img/test-sample.png.webp?abc=def#xyz)',
191194
$response->getContent()
192195
);

tests/ResourceManagerTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testRequireJs()
7878
{
7979
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
8080
ResourceManager::resources()->requireJs('js/alert.js');
81-
$this->assertContains(
81+
$this->assertStringContainsString(
8282
'src="r/f417133ec50f/js/alert.js"',
8383
Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_JS)
8484
);
@@ -101,7 +101,7 @@ public function testRequireCss()
101101
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
102102
ResourceManager::resources()->includeCss('css/test.css');
103103
ResourceManager::resources()->requireCss('css/test.css');
104-
$this->assertContains(
104+
$this->assertStringContainsString(
105105
'href="r/bd04a6113c11/css/test.css"',
106106
Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_CSS)
107107
);
@@ -243,4 +243,18 @@ public function testSetResourceStoreConfig()
243243
$this->assertSame($store1, $manager->getResourceStore());
244244
$this->assertNotSame($store2, $manager->getResourceStore());
245245
}
246+
247+
public function testDefaultOptions()
248+
{
249+
$manager = ResourceManager::resources();
250+
static::assertTrue($manager->getOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true));
251+
252+
ResourceManager::setDefaultOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, false);
253+
static::assertFalse($manager->getOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true));
254+
255+
$manager->setOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true);
256+
static::assertTrue($manager->getOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true));
257+
258+
ResourceManager::setDefaultOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true);
259+
}
246260
}

tests/ResourceStoreTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,17 @@ public function testGenerateHtmlIncludes()
8686
$store->requireJs('js/defer.js', ['defer' => null, 'type' => 'application/javascript']);
8787
$store->requireInlineJs("alert('hi');");
8888
$store->requireInlineCss("body{background:red;}");
89-
$this->assertContains('href="css/test.css"', $store->generateHtmlIncludes(ResourceStore::TYPE_CSS));
90-
$this->assertContains('src="js/alert.js"', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
91-
$this->assertContains(
89+
$this->assertStringContainsString('href="css/test.css"', $store->generateHtmlIncludes(ResourceStore::TYPE_CSS));
90+
$this->assertStringContainsString('src="js/alert.js"', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
91+
$this->assertStringContainsString(
9292
'src="js/defer.js" defer type="application/javascript"',
9393
$store->generateHtmlIncludes(ResourceStore::TYPE_JS)
9494
);
95-
$this->assertContains('<script>alert(\'hi\');</script>', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
96-
$this->assertContains(
95+
$this->assertStringContainsString(
96+
'<script>alert(\'hi\');</script>',
97+
$store->generateHtmlIncludes(ResourceStore::TYPE_JS)
98+
);
99+
$this->assertStringContainsString(
97100
'<style type=\'text/css\'>body{background:red;}</style>',
98101
$store->generateHtmlIncludes(ResourceStore::TYPE_CSS)
99102
);

tests/Resources/AbstractDispatchableResourceTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public function testProcessesContent()
2222
$resource->setProcessingPath('css/test.css');
2323
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'test.css')));
2424
$content = $resource->getContent();
25-
$this->assertContains('url(r/395d1a0e8999/img/x.jpg)', $content);
26-
$this->assertContains('url(\'r/942e325b86c0/css/css.jpg\')', $content);
27-
$this->assertContains('url("r/942e325be95f/css/sub/subimg.jpg")', $content);
28-
$this->assertContains('url(\'http://www.example.com/background.jpg\')', $content);
29-
$this->assertContains('url(../img/missing-file.jpg)', $content);
30-
$this->assertContains('url(../../../img/missing-file.jpg)', $content);
25+
$this->assertStringContainsString('url(r/395d1a0e8999/img/x.jpg)', $content);
26+
$this->assertStringContainsString('url(\'r/942e325b86c0/css/css.jpg\')', $content);
27+
$this->assertStringContainsString('url("r/942e325be95f/css/sub/subimg.jpg")', $content);
28+
$this->assertStringContainsString('url(\'http://www.example.com/background.jpg\')', $content);
29+
$this->assertStringContainsString('url(../img/missing-file.jpg)', $content);
30+
$this->assertStringContainsString('url(../../../img/missing-file.jpg)', $content);
3131

3232
$resource->setProcessingPath('css/do-not-modify.css');
3333
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'do-not-modify.css')));
34-
$this->assertContains('url(../img/x.jpg)', $resource->getContent());
34+
$this->assertStringContainsString('url(../img/x.jpg)', $resource->getContent());
3535
}
3636

3737
public function testJsContent()
@@ -46,9 +46,9 @@ public function testJsContent()
4646
$resource->setFilePath($manager->getFilePath('js/url.min.js'));
4747
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'js', 'url.min.js')));
4848
$content = $resource->getContent();
49-
$this->assertContains('import test from \'./test\';', $content);
50-
$this->assertContains('import {default as alert} from \'r/f417133ec50f/js/alert.js\';', $content);
51-
$this->assertContains('import misc from \'r/b6ccf604ae88/js/misc.js\';', $content);
52-
$this->assertContains('"url(" + test(p) + ")"', $content);
49+
$this->assertStringContainsString('import test from \'./test\';', $content);
50+
$this->assertStringContainsString('import {default as alert} from \'r/f417133ec50f/js/alert.js\';', $content);
51+
$this->assertStringContainsString('import misc from \'r/b6ccf604ae88/js/misc.js\';', $content);
52+
$this->assertStringContainsString('"url(" + test(p) + ")"', $content);
5353
}
5454
}

tests/Resources/NestedResourceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function testProcessesContent()
2020
$resource->setProcessingPath('css/test.css');
2121
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'test.css')));
2222
$content = $resource->getContent();
23-
$this->assertContains('url(/_r/r/395d1a0e8999/img/x.jpg?x=y&request=b)', $content);
23+
$this->assertStringContainsString('url(/_r/r/395d1a0e8999/img/x.jpg?x=y&request=b)', $content);
2424
}
2525
}

0 commit comments

Comments
 (0)