Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
Minds Backend - Engine
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
293
Merge Requests
37
CI / CD
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
Minds Backend - Engine
Commits
e8bc4ce1
Commit
e8bc4ce1
authored
14 minutes ago
by
Emiliano Balbuena
Browse files
Options
Download
(wip)(test): Router spec tests (ii)
parent
c5c09ac1
epic/modern-router
1 merge request
!342
WIP: (feat): Modernize Router (&75)
Pipeline
#101225873
passed with stages
in 6 minutes and 47 seconds
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
356 additions
and
5 deletions
+356
-5
Core/Router/Middleware/Kernel/ErrorHandlerMiddleware.php
View file @
e8bc4ce1
...
...
@@ -19,6 +19,19 @@ use function Sentry\captureException;
class
ErrorHandlerMiddleware
implements
MiddlewareInterface
{
/** @var bool */
protected
$sentryEnabled
=
true
;
/**
* @param bool $sentryEnabled
* @return ErrorHandlerMiddleware
*/
public
function
setSentryEnabled
(
bool
$sentryEnabled
)
:
ErrorHandlerMiddleware
{
$this
->
sentryEnabled
=
$sentryEnabled
;
return
$this
;
}
/**
* Process an incoming server request.
*
...
...
@@ -51,7 +64,9 @@ class ErrorHandlerMiddleware implements MiddlewareInterface
// Sentry
captureException
(
$e
);
if
(
$this
->
sentryEnabled
)
{
captureException
(
$e
);
}
}
switch
(
$request
->
getAttribute
(
'accept'
))
{
...
...
This diff is collapsed.
Core/Router/Middleware/Kernel/RequestHandlerMiddleware.php
View file @
e8bc4ce1
...
...
@@ -53,12 +53,16 @@ class RequestHandlerMiddleware implements MiddlewareInterface
// Pipe route-specific middleware
foreach
(
$requestHandler
->
getMiddleware
()
as
$middleware
)
{
if
(
!
class_exists
(
$middleware
))
{
throw
new
Exception
(
"
{
$middleware
}
does not exist"
);
if
(
is_string
(
$middleware
))
{
if
(
!
class_exists
(
$middleware
))
{
throw
new
Exception
(
"
{
$middleware
}
does not exist"
);
}
$middlewareInstance
=
new
$middleware
;
}
else
{
$middlewareInstance
=
$middleware
;
}
$middlewareInstance
=
new
$middleware
;
if
(
!
(
$middlewareInstance
instanceof
MiddlewareInterface
))
{
throw
new
Exception
(
"
{
$middleware
}
is not a middleware"
);
}
...
...
This diff is collapsed.
Spec/Core/Router/Middleware/Kernel/ErrorHandlerMiddlewareSpec.php
View file @
e8bc4ce1
...
...
@@ -2,9 +2,15 @@
namespace
Spec\Minds\Core\Router\Middleware\Kernel
;
use
Exception
;
use
Minds\Core\Router\Middleware\Kernel\ErrorHandlerMiddleware
;
use
PhpSpec\ObjectBehavior
;
use
Prophecy\Argument
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Http\Message\ServerRequestInterface
;
use
Psr\Http\Server\RequestHandlerInterface
;
use
Zend\Diactoros\Response\HtmlResponse
;
use
Zend\Diactoros\Response\JsonResponse
;
class
ErrorHandlerMiddlewareSpec
extends
ObjectBehavior
{
...
...
@@ -12,4 +18,57 @@ class ErrorHandlerMiddlewareSpec extends ObjectBehavior
{
$this
->
shouldHaveType
(
ErrorHandlerMiddleware
::
class
);
}
public
function
it_should_process
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willReturn
(
$response
);
$this
->
setSentryEnabled
(
false
)
->
process
(
$request
,
$handler
)
->
shouldReturn
(
$response
);
}
public
function
it_should_catch_during_process_and_output_html
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willThrow
(
new
Exception
(
'PHPSpec'
));
$request
->
getAttribute
(
'accept'
)
->
shouldBeCalled
()
->
willReturn
(
'html'
);
$this
->
setSentryEnabled
(
false
)
->
process
(
$request
,
$handler
)
->
shouldBeAnInstanceOf
(
HtmlResponse
::
class
);
}
public
function
it_should_catch_during_process_and_output_json
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willThrow
(
new
Exception
(
'PHPSpec'
));
$request
->
getAttribute
(
'accept'
)
->
shouldBeCalled
()
->
willReturn
(
'json'
);
$this
->
setSentryEnabled
(
false
)
->
process
(
$request
,
$handler
)
->
shouldBeAnInstanceOf
(
JsonResponse
::
class
);
}
}
This diff is collapsed.
Spec/Core/Router/Middleware/Kernel/FrameSecurityMiddlewareSpec.php
View file @
e8bc4ce1
...
...
@@ -5,6 +5,9 @@ namespace Spec\Minds\Core\Router\Middleware\Kernel;
use
Minds\Core\Router\Middleware\Kernel\FrameSecurityMiddleware
;
use
PhpSpec\ObjectBehavior
;
use
Prophecy\Argument
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Http\Message\ServerRequestInterface
;
use
Psr\Http\Server\RequestHandlerInterface
;
class
FrameSecurityMiddlewareSpec
extends
ObjectBehavior
{
...
...
@@ -12,4 +15,22 @@ class FrameSecurityMiddlewareSpec extends ObjectBehavior
{
$this
->
shouldHaveType
(
FrameSecurityMiddleware
::
class
);
}
public
function
it_should_process
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willReturn
(
$response
);
$response
->
withHeader
(
'X-Frame-Options'
,
'DENY'
)
->
shouldBeCalled
()
->
willReturn
(
$response
);
$this
->
process
(
$request
,
$handler
)
->
shouldReturn
(
$response
);
}
}
This diff is collapsed.
Spec/Core/Router/Middleware/Kernel/JsonPayloadMiddlewareSpec.php
View file @
e8bc4ce1
...
...
@@ -5,6 +5,9 @@ namespace Spec\Minds\Core\Router\Middleware\Kernel;
use
Minds\Core\Router\Middleware\Kernel\JsonPayloadMiddleware
;
use
PhpSpec\ObjectBehavior
;
use
Prophecy\Argument
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Http\Message\ServerRequestInterface
;
use
Psr\Http\Server\RequestHandlerInterface
;
class
JsonPayloadMiddlewareSpec
extends
ObjectBehavior
{
...
...
@@ -12,4 +15,30 @@ class JsonPayloadMiddlewareSpec extends ObjectBehavior
{
$this
->
shouldHaveType
(
JsonPayloadMiddleware
::
class
);
}
public
function
it_should_process
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$request
->
getHeader
(
'Content-Type'
)
->
shouldBeCalled
()
->
willReturn
([
'text/json'
]);
$request
->
getBody
()
->
shouldBeCalled
()
->
willReturn
(
json_encode
([
'phpspec'
=>
1
]));
$request
->
withParsedBody
([
'phpspec'
=>
1
])
->
shouldBeCalled
()
->
willReturn
(
$request
);
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willReturn
(
$response
);
$this
->
process
(
$request
,
$handler
)
->
shouldReturn
(
$response
);
}
}
This diff is collapsed.
Spec/Core/Router/Middleware/Kernel/OauthMiddlewareSpec.php
View file @
e8bc4ce1
...
...
@@ -12,4 +12,8 @@ class OauthMiddlewareSpec extends ObjectBehavior
{
$this
->
shouldHaveType
(
OauthMiddleware
::
class
);
}
/**
* Untestable due to the use of Session as static class
*/
}
This diff is collapsed.
Spec/Core/Router/Middleware/Kernel/RegistryEntryMiddlewareSpec.php
View file @
e8bc4ce1
...
...
@@ -2,9 +2,16 @@
namespace
Spec\Minds\Core\Router\Middleware\Kernel
;
use
Minds\Core\Di\Di
;
use
Minds\Core\Di\Ref
as
DiRef
;
use
Minds\Core\Router\Middleware\Kernel\RegistryEntryMiddleware
;
use
Minds\Core\Router\RegistryEntry
;
use
PhpSpec\ObjectBehavior
;
use
Prophecy\Argument
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Http\Message\ServerRequestInterface
;
use
Psr\Http\Message\UriInterface
;
use
Psr\Http\Server\RequestHandlerInterface
;
class
RegistryEntryMiddlewareSpec
extends
ObjectBehavior
{
...
...
@@ -12,4 +19,138 @@ class RegistryEntryMiddlewareSpec extends ObjectBehavior
{
$this
->
shouldHaveType
(
RegistryEntryMiddleware
::
class
);
}
public
function
it_should_process_and_passthru_if_no_entry
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willReturn
(
$response
);
$request
->
getAttribute
(
'_phpspec_router-registry-entry'
)
->
shouldBeCalled
()
->
willReturn
(
null
);
$this
->
setAttributeName
(
'_phpspec_router-registry-entry'
)
->
process
(
$request
,
$handler
);
}
public
function
it_should_process_di_bindings
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
,
RegistryEntry
$registryEntry
,
DiRef
$diRef
,
UriInterface
$uri
)
{
$request
->
getAttribute
(
'_phpspec_router-registry-entry'
)
->
shouldBeCalled
()
->
willReturn
(
$registryEntry
);
$registryEntry
->
getBinding
()
->
shouldBeCalled
()
->
willReturn
(
$diRef
);
$request
->
getUri
()
->
shouldBeCalled
()
->
willReturn
(
$uri
);
$uri
->
getPath
()
->
shouldBeCalled
()
->
willReturn
(
'/phpspec/1000/edit'
);
$registryEntry
->
extract
(
'/phpspec/1000/edit'
)
->
shouldBeCalled
()
->
willReturn
([
'id'
=>
'1000'
]);
$providerId
=
static
::
class
.
'Provider'
;
$diRef
->
getProvider
()
->
shouldBeCalled
()
->
willReturn
(
$providerId
);
$diRef
->
getMethod
()
->
shouldBeCalled
()
->
willReturn
(
'test'
);
Di
::
_
()
->
bind
(
$providerId
,
function
()
use
(
$response
)
{
return
(
new
class
{
protected
$response
;
public
function
setResponse
(
$response
)
{
$this
->
response
=
$response
;
return
$this
;
}
public
function
test
()
{
return
$this
->
response
;
}
})
->
setResponse
(
$response
->
getWrappedObject
());
});
$request
->
withAttribute
(
'parameters'
,
[
'id'
=>
'1000'
])
->
shouldBeCalled
()
->
willReturn
(
$request
);
$handler
->
handle
(
$request
)
->
shouldNotBeCalled
();
$this
->
setAttributeName
(
'_phpspec_router-registry-entry'
)
->
process
(
$request
,
$handler
)
->
shouldReturn
(
$response
);
Di
::
_
()
->
bind
(
$providerId
,
function
()
{
return
false
;
// Release closure bindings
});
}
public
function
it_should_process_callable
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
,
RegistryEntry
$registryEntry
,
UriInterface
$uri
)
{
$request
->
getAttribute
(
'_phpspec_router-registry-entry'
)
->
shouldBeCalled
()
->
willReturn
(
$registryEntry
);
$binding
=
function
()
use
(
$response
)
{
return
$response
->
getWrappedObject
();
};
$registryEntry
->
getBinding
()
->
shouldBeCalled
()
->
willReturn
(
$binding
);
$request
->
getUri
()
->
shouldBeCalled
()
->
willReturn
(
$uri
);
$uri
->
getPath
()
->
shouldBeCalled
()
->
willReturn
(
'/phpspec/1000/edit'
);
$registryEntry
->
extract
(
'/phpspec/1000/edit'
)
->
shouldBeCalled
()
->
willReturn
([
'id'
=>
'1000'
]);
$request
->
withAttribute
(
'parameters'
,
[
'id'
=>
'1000'
])
->
shouldBeCalled
()
->
willReturn
(
$request
);
$handler
->
handle
(
$request
)
->
shouldNotBeCalled
();
$this
->
setAttributeName
(
'_phpspec_router-registry-entry'
)
->
process
(
$request
,
$handler
)
->
shouldReturn
(
$response
);
}
}
This diff is collapsed.
Spec/Core/Router/Middleware/Kernel/RequestHandlerMiddlewareSpec.php
View file @
e8bc4ce1
...
...
@@ -2,9 +2,15 @@
namespace
Spec\Minds\Core\Router\Middleware\Kernel
;
use
Minds\Core\Router\Dispatcher
;
use
Minds\Core\Router\Middleware\Kernel\RequestHandlerMiddleware
;
use
Minds\Core\Router\RegistryEntry
;
use
PhpSpec\ObjectBehavior
;
use
Prophecy\Argument
;
use
Psr\Http\Message\ResponseInterface
;
use
Psr\Http\Message\ServerRequestInterface
;
use
Psr\Http\Server\MiddlewareInterface
;
use
Psr\Http\Server\RequestHandlerInterface
;
class
RequestHandlerMiddlewareSpec
extends
ObjectBehavior
{
...
...
@@ -12,4 +18,76 @@ class RequestHandlerMiddlewareSpec extends ObjectBehavior
{
$this
->
shouldHaveType
(
RequestHandlerMiddleware
::
class
);
}
public
function
it_should_process_and_passthru_if_no_handler
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
)
{
$request
->
getAttribute
(
'_phpspec_request-handler'
)
->
shouldBeCalled
()
->
willReturn
(
null
);
$handler
->
handle
(
$request
)
->
shouldBeCalled
()
->
willReturn
(
$response
);
$this
->
setAttributeName
(
'_phpspec_request-handler'
)
->
process
(
$request
,
$handler
);
}
public
function
it_should_process_and_dispatch_using_registry_entry
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
,
RegistryEntry
$registryEntry
,
MiddlewareInterface
$middleware
)
{
$request
->
getAttribute
(
'_phpspec_request-handler'
)
->
shouldBeCalled
()
->
willReturn
(
$registryEntry
);
$registryEntry
->
getMiddleware
()
->
shouldBeCalled
()
->
willReturn
([
$middleware
]);
$request
->
withAttribute
(
'_router-registry-entry'
,
$registryEntry
)
->
shouldBeCalled
()
->
willReturn
(
$request
);
$middleware
->
process
(
$request
,
Argument
::
type
(
Dispatcher
::
class
))
->
shouldBeCalled
()
->
willReturn
(
$response
);
$handler
->
handle
(
$request
)
->
shouldNotBeCalled
();
$this
->
setAttributeName
(
'_phpspec_request-handler'
)
->
process
(
$request
,
$handler
);
}
public
function
it_should_process_and_dispatch_using_closure
(
ServerRequestInterface
$request
,
RequestHandlerInterface
$handler
,
ResponseInterface
$response
,
RegistryEntry
$registryEntry
,
MiddlewareInterface
$middleware
)
{
$closure
=
function
()
use
(
$response
)
{
return
$response
->
getWrappedObject
();
};
$request
->
getAttribute
(
'_phpspec_request-handler'
)
->
shouldBeCalled
()
->
willReturn
(
$closure
);
$handler
->
handle
(
$request
)
->
shouldNotBeCalled
();
$this
->
setAttributeName
(
'_phpspec_request-handler'
)
->
process
(
$request
,
$handler
);
}
}
This diff is collapsed.
Please
register
or
sign in
to comment