Skip to content

Commit

Permalink
Set resource manager defaults (#30)
Browse files Browse the repository at this point in the history
* Set resource manager defaults
increase to php 8

* Increase php version
  • Loading branch information
bajb committed Oct 31, 2023
1 parent 65c9041 commit 5a2bad8
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 60 deletions.
33 changes: 14 additions & 19 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ defaults: &defaults
steps:
# common php steps
- run: echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
- run: if [ -n "$ADD_PACKAGES" ]; then apk -U add $ADD_PACKAGES; fi;
- run: if [ -n "$ADD_MODULES" ]; then docker-php-ext-install $ADD_MODULES; fi;
- run: echo "date.timezone = UTC" >> $(php --ini |grep Scan |awk '{print $NF}')/timezone.ini
- run: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

# install apcu
- run: |
docker-php-source extract \
&& apk add --no-cache --virtual .phpize-deps-configure $PHPIZE_DEPS \
&& printf "\n" | pecl install apcu \
&& docker-php-ext-enable apcu \
&& apk del .phpize-deps-configure \
&& docker-php-source delete
# pre-checkout steps

# checkout
- checkout
Expand All @@ -27,28 +22,28 @@ defaults: &defaults

version: 2
jobs:
build-php71:
build-php74:
<<: *defaults
docker:
- image: php:7.1-alpine
build-php72:
- image: php:7.4-alpine
build-php80:
<<: *defaults
docker:
- image: php:7.2-alpine
build-php73:
- image: php:8.0-alpine
build-php81:
<<: *defaults
docker:
- image: php:7.3-alpine
build-php74:
- image: php:8.1-alpine
build-php82:
<<: *defaults
docker:
- image: php:7.4-alpine
- image: php:8.2-alpine

workflows:
version: 2
build:
jobs:
- build-php71
- build-php72
- build-php73
- build-php74
- build-php80
- build-php81
- build-php82
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
}
],
"require": {
"php": ">=7.1",
"packaged/helpers": "^1.0||^2.0",
"packaged/http": "~1.1",
"packaged/config": "~1.4",
Expand All @@ -20,7 +19,7 @@
},
"require-dev": {
"ext-apcu": "*",
"phpunit/phpunit": "7.5.*"
"phpunit/phpunit": "10"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 22 additions & 1 deletion src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class ResourceManager
protected $_componentPath;
protected $_options = [];

protected static $defaultOptions = [
self::OPT_THROW_ON_FILE_NOT_FOUND => true,
self::OPT_RESOURCE_STORE => null,
];

/**
* @var Dispatch|null Dispatch in use for components to calculate paths
*/
Expand Down Expand Up @@ -464,7 +469,7 @@ public function getFileHash($fullPath)

public function getOption($option, $default = null)
{
return $this->_options[$option] ?? $default;
return $this->_options[$option] ?? $this->_defaultOption($option, $default);
}

/**
Expand Down Expand Up @@ -533,4 +538,20 @@ public static function clearCache()
static::$cmc = [];
static::$_fileHashCache = [];
}

/**
* @param string $key
* @param mixed $value
*
* @return void
*/
public static function setDefaultOption(string $key, $value)
{
static::$defaultOptions[$key] = $value;
}

protected function _defaultOption(string $key, $default = null)
{
return static::$defaultOptions[$key] ?? $default;
}
}
6 changes: 3 additions & 3 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testComponent()
$request = Request::create('/' . $uri);
$response = $dispatch->handleRequest($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('body{color:red}', $response->getContent());
$this->assertStringContainsString('body{color:red}', $response->getContent());

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

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

$manager = ResourceManager::component(new ChildComponent());
$uri = $manager->getResourceUri('style.css');
Expand Down
35 changes: 19 additions & 16 deletions tests/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,37 @@ public function testHandle()
$request = Request::create('/r/bd04a611ed6d/css/test.css');
$response = $dispatch->handleRequest($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('url(r/395d1a0e8999/img/x.jpg)', $response->getContent());
$this->assertStringContainsString('url(r/395d1a0e8999/img/x.jpg)', $response->getContent());

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

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

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

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

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

Dispatch::destroy();
}
Expand All @@ -95,7 +95,10 @@ public function testBaseUri()
Dispatch::bind($dispatch);
$request = Request::create('/r/bd04a611ed6d/css/test.css');
$response = $dispatch->handleRequest($request);
$this->assertContains('url(http://assets.packaged.in/r/395d1a0e8999/img/x.jpg)', $response->getContent());
$this->assertStringContainsString(
'url(http://assets.packaged.in/r/395d1a0e8999/img/x.jpg)',
$response->getContent()
);
Dispatch::destroy();
}

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

Expand Down Expand Up @@ -166,11 +169,11 @@ public function testWebpReplacements()

$request = Request::create(ResourceManager::resources()->getResourceUri('css/webptest.css'));
$response = $dispatch->handleRequest($request);
$this->assertContains(
$this->assertStringContainsString(
'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png?abc=def#xyz)',
$response->getContent()
);
$this->assertNotContains(
$this->assertStringNotContainsString(
'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png.webp?abc=def#xyz)',
$response->getContent()
);
Expand All @@ -182,11 +185,11 @@ public function testWebpReplacements()
$request = Request::create(ResourceManager::resources()->getResourceUri('css/webptest.css'));
$response = $dispatch->handleRequest($request);

$this->assertNotContains(
$this->assertStringNotContainsString(
'url(http://assets.packaged.in/r/30c60da9f504-1/img/test-sample.png?abc=def#xyz)',
$response->getContent()
);
$this->assertContains(
$this->assertStringContainsString(
'url(http://assets.packaged.in/r/d6e2937fee66-1/img/test-sample.png.webp?abc=def#xyz)',
$response->getContent()
);
Expand Down
18 changes: 16 additions & 2 deletions tests/ResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testRequireJs()
{
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
ResourceManager::resources()->requireJs('js/alert.js');
$this->assertContains(
$this->assertStringContainsString(
'src="r/f417133ec50f/js/alert.js"',
Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_JS)
);
Expand All @@ -101,7 +101,7 @@ public function testRequireCss()
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
ResourceManager::resources()->includeCss('css/test.css');
ResourceManager::resources()->requireCss('css/test.css');
$this->assertContains(
$this->assertStringContainsString(
'href="r/bd04a6113c11/css/test.css"',
Dispatch::instance()->store()->generateHtmlIncludes(ResourceStore::TYPE_CSS)
);
Expand Down Expand Up @@ -243,4 +243,18 @@ public function testSetResourceStoreConfig()
$this->assertSame($store1, $manager->getResourceStore());
$this->assertNotSame($store2, $manager->getResourceStore());
}

public function testDefaultOptions()
{
$manager = ResourceManager::resources();
static::assertTrue($manager->getOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true));

ResourceManager::setDefaultOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, false);
static::assertFalse($manager->getOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true));

$manager->setOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true);
static::assertTrue($manager->getOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true));

ResourceManager::setDefaultOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true);
}
}
13 changes: 8 additions & 5 deletions tests/ResourceStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,17 @@ public function testGenerateHtmlIncludes()
$store->requireJs('js/defer.js', ['defer' => null, 'type' => 'application/javascript']);
$store->requireInlineJs("alert('hi');");
$store->requireInlineCss("body{background:red;}");
$this->assertContains('href="css/test.css"', $store->generateHtmlIncludes(ResourceStore::TYPE_CSS));
$this->assertContains('src="js/alert.js"', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
$this->assertContains(
$this->assertStringContainsString('href="css/test.css"', $store->generateHtmlIncludes(ResourceStore::TYPE_CSS));
$this->assertStringContainsString('src="js/alert.js"', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
$this->assertStringContainsString(
'src="js/defer.js" defer type="application/javascript"',
$store->generateHtmlIncludes(ResourceStore::TYPE_JS)
);
$this->assertContains('<script>alert(\'hi\');</script>', $store->generateHtmlIncludes(ResourceStore::TYPE_JS));
$this->assertContains(
$this->assertStringContainsString(
'<script>alert(\'hi\');</script>',
$store->generateHtmlIncludes(ResourceStore::TYPE_JS)
);
$this->assertStringContainsString(
'<style type=\'text/css\'>body{background:red;}</style>',
$store->generateHtmlIncludes(ResourceStore::TYPE_CSS)
);
Expand Down
22 changes: 11 additions & 11 deletions tests/Resources/AbstractDispatchableResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public function testProcessesContent()
$resource->setProcessingPath('css/test.css');
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'test.css')));
$content = $resource->getContent();
$this->assertContains('url(r/395d1a0e8999/img/x.jpg)', $content);
$this->assertContains('url(\'r/942e325b86c0/css/css.jpg\')', $content);
$this->assertContains('url("r/942e325be95f/css/sub/subimg.jpg")', $content);
$this->assertContains('url(\'http://www.example.com/background.jpg\')', $content);
$this->assertContains('url(../img/missing-file.jpg)', $content);
$this->assertContains('url(../../../img/missing-file.jpg)', $content);
$this->assertStringContainsString('url(r/395d1a0e8999/img/x.jpg)', $content);
$this->assertStringContainsString('url(\'r/942e325b86c0/css/css.jpg\')', $content);
$this->assertStringContainsString('url("r/942e325be95f/css/sub/subimg.jpg")', $content);
$this->assertStringContainsString('url(\'http://www.example.com/background.jpg\')', $content);
$this->assertStringContainsString('url(../img/missing-file.jpg)', $content);
$this->assertStringContainsString('url(../../../img/missing-file.jpg)', $content);

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

public function testJsContent()
Expand All @@ -46,9 +46,9 @@ public function testJsContent()
$resource->setFilePath($manager->getFilePath('js/url.min.js'));
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'js', 'url.min.js')));
$content = $resource->getContent();
$this->assertContains('import test from \'./test\';', $content);
$this->assertContains('import {default as alert} from \'r/f417133ec50f/js/alert.js\';', $content);
$this->assertContains('import misc from \'r/b6ccf604ae88/js/misc.js\';', $content);
$this->assertContains('"url(" + test(p) + ")"', $content);
$this->assertStringContainsString('import test from \'./test\';', $content);
$this->assertStringContainsString('import {default as alert} from \'r/f417133ec50f/js/alert.js\';', $content);
$this->assertStringContainsString('import misc from \'r/b6ccf604ae88/js/misc.js\';', $content);
$this->assertStringContainsString('"url(" + test(p) + ")"', $content);
}
}
2 changes: 1 addition & 1 deletion tests/Resources/NestedResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function testProcessesContent()
$resource->setProcessingPath('css/test.css');
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'test.css')));
$content = $resource->getContent();
$this->assertContains('url(/_r/r/395d1a0e8999/img/x.jpg?x=y&request=b)', $content);
$this->assertStringContainsString('url(/_r/r/395d1a0e8999/img/x.jpg?x=y&request=b)', $content);
}
}

0 comments on commit 5a2bad8

Please sign in to comment.