Fix tests

This commit is contained in:
Aaron Carlino 2019-03-20 12:32:11 +13:00
parent 39a29fa2f6
commit aa491d9294
5 changed files with 35 additions and 5 deletions

View File

@ -365,12 +365,13 @@ class Director implements TemplateGlobalProvider
};
break;
}
$start = time();
// Call the handler with the configured middlewares
$response = $this->callMiddleware($request, $handler);
$end = time();
// Note that if a different request was previously registered, this will now be lost
// In these cases it's better to use Kernel::nest() prior to kicking off a nested request
$elapsed = $end - $start;
Injector::inst()->unregisterNamedObject(HTTPRequest::class);
return $response;

View File

@ -54,12 +54,21 @@ trait HTTPMiddlewareAware
*/
protected function callMiddleware(HTTPRequest $request, callable $last)
{
$classes = array_map(function ($middle) {
return get_class($middle);
}, $this->getMiddlewares());
// Reverse middlewares
$next = $last;
/** @var HTTPMiddleware $middleware */
foreach (array_reverse($this->getMiddlewares()) as $middleware) {
$next = function ($request) use ($middleware, $next) {
return $middleware->process($request, $next);
$start = time();
$value = $middleware->process($request, $next);
$end = time();
$elapsed = $end - $start;
return $value;
};
}
return $next($request);

View File

@ -388,8 +388,9 @@ trait Extensible
if (!$strict && is_subclass_of($extension, $requiredExtension)) {
return true;
}
if (Injector::inst()->get($extension) instanceof $requiredExtension) {
return true;
$inst = Injector::inst()->get($extension);
if ($inst instanceof $requiredExtension) {
return $strict ? get_class($inst) === $requiredExtension : true;
}
}

View File

@ -11,6 +11,7 @@ use SilverStripe\Core\Tests\ObjectTest\ExtendTest1;
use SilverStripe\Core\Tests\ObjectTest\ExtendTest2;
use SilverStripe\Core\Tests\ObjectTest\ExtendTest3;
use SilverStripe\Core\Tests\ObjectTest\ExtendTest4;
use SilverStripe\Core\Tests\ObjectTest\ExtendTest5;
use SilverStripe\Core\Tests\ObjectTest\ExtensionRemoveTest;
use SilverStripe\Core\Tests\ObjectTest\ExtensionTest;
use SilverStripe\Core\Tests\ObjectTest\ExtensionTest2;
@ -290,6 +291,13 @@ class ObjectTest extends SapphireTest
"Extensions are detected with instance hasExtension() when added through add_extension()"
);
// load in a custom implementation
Injector::inst()->registerService(new ExtendTest5(), ExtendTest4::class);
$this->assertTrue(
ExtensionTest3::has_extension(ExtendTest5::class),
"Injected sub-extensions are detected with static has_extension() when added through add_extension()"
);
// @todo At the moment, this does NOT remove the extension due to parameterized naming,
// meaning the extension will remain added in further test cases
ExtensionTest::remove_extension(ExtendTest3::class);

View File

@ -0,0 +1,11 @@
<?php
namespace SilverStripe\Core\Tests\ObjectTest;
class ExtendTest5 extends ExtendTest4
{
public function extendableMethod($argument = null)
{
return "ExtendTest5($argument)";
}
}