Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
StackMiddleware example
$envelope
= Envelope::
wrap
(
$message
,
$stamps
)
;
$middlewareIterator
=
$this
->middlewareAggregate->
getIterator
(
)
;
while
(
$middlewareIterator
instanceof \IteratorAggregate
)
{
$middlewareIterator
=
$middlewareIterator
->
getIterator
(
)
;
}
$middlewareIterator
->
rewind
(
)
;
if
(
!
$middlewareIterator
->
valid
(
)
)
{
return
$envelope
;
}
$stack
=
new
StackMiddleware
(
$middlewareIterator
)
;
return
$middlewareIterator
->
current
(
)
->
handle
(
$envelope
,
$stack
)
;
}
}
return
$stack
;
}
$nextMiddleware
=
$this
->
createMock
(
MiddlewareInterface::
class
)
;
$nextMiddleware
->
expects
(
$this
->
once
(
)
)
->
method
(
'handle'
)
->
willReturnCallback
(
fn
(
Envelope
$envelope
, StackInterface
$stack
)
: Envelope =>
$envelope
)
;
return
new
StackMiddleware
(
$nextMiddleware
)
;
}
protected
function
getThrowingStackMock
(
\Throwable
$throwable
= null
)
{
$nextMiddleware
=
$this
->
createMock
(
MiddlewareInterface::
class
)
;
$nextMiddleware
->
expects
(
$this
->
once
(
)
)
->
method
(
'handle'
)
->
willThrowException
(
$throwable
??
new
\
RuntimeException
(
'Thrown from next middleware.'
)
)
;
$nextMiddleware
=
$this
->
createMock
(
MiddlewareInterface::
class
)
;
$nextMiddleware
->
expects
(
$this
->
once
(
)
)
->
method
(
'handle'
)
->
willReturnCallback
(
function
DEnvelope
$envelope
, StackInterface
$stack
)
use
(
$context
)
: Envelope
{
$this
->
assertSame
(
'symfony.com',
$context
->
getHost
(
)
)
;
return
$envelope
;
}
)
;
$middleware
->
handle
(
$envelope
,
new
StackMiddleware
(
$nextMiddleware
)
)
;
$this
->
assertSame
(
'github.com',
$context
->
getHost
(
)
)
;
}
}
'Tail on "command_bus"',
]
;
$this
->
assertSame
(
array_shift
(
$stopSeries
)
,
$name
)
;
return
$this
->
createMock
(
StopwatchEvent::
class
)
;
}
)
;
$traced
=
new
TraceableMiddleware
(
$stopwatch
,
$busId
)
;
$traced
->
handle
(
$envelope
,
new
StackMiddleware
(
new
\
ArrayIterator
(
[
null,
$middleware
]
)
)
)
;
$this
->
assertSame
(
1,
$middleware
->calls
)
;
}
public
function
testHandleWithException
(
)
{
$this
->
expectException
(
\RuntimeException::
class
)
;
$this
->
expectExceptionMessage
(
'Thrown from next middleware.'
)
;
$busId
= 'command_bus';
$middleware
=
$this
->
createMock
(
MiddlewareInterface::
class
)
;
$middleware
->
expects
(
$this
->
once
(
)
)
true,
]
;
}
public
function
testThrowsNoHandlerException
(
)
{
$this
->
expectException
(
NoHandlerForMessageException::
class
)
;
$this
->
expectExceptionMessage
(
'No handler for message "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"'
)
;
$middleware
=
new
HandleMessageMiddleware
(
new
HandlersLocator
(
[
]
)
)
;
$middleware
->
handle
(
new
Envelope
(
new
DummyMessage
(
'Hey'
)
)
,
new
StackMiddleware
(
)
)
;
}
public
function
testMessageAlreadyHandled
(
)
{
$handler
=
$this
->
createPartialMock
(
HandleMessageMiddlewareTestCallable::
class
,
[
'__invoke'
]
)
;
$middleware
=
new
HandleMessageMiddleware
(
new
HandlersLocator
(
[
DummyMessage::
class
=>
[
$handler
]
,
]
)
)
;
$envelope
=
new
Envelope
(
new
DummyMessage
(
'Hey'
)
)
;