I want to unit test following class.
<?php
namespace Gpx\Handlers;
use Gpx\EntityInfrastructure\Model\Events\SessionInvalidated;
use Gpx\EntityInfrastructure\Model\Payload;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\SynchronousHandlerInterface;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\MessageHandlingContextInterface;
use Gpx\HfxEventSourcing\HfxAggregateRoot;
use Gpx\HfxEventSourcing\HfxProjectionHelper;
use Gpx\HfxEventSourcing\HfxEventMetadata;
use Gpx\HfxEventSourcing\HfxRepository;
use Gpx\Hfx\MessageTransport\Response\SendableResponse;
class BroadcastSessionInvalidated implements SynchronousHandlerInterface
{
/** #var HfxRepository */
private $repository;
/** #var HfxProjectionHelper */
private $projectionHelper;
public function __construct(HfxRepository $repository, HfxProjectionHelper $projectionHelper)
{
$this->repository = $repository;
$this->projectionHelper = $projectionHelper;
}
public function handleSynchronousMessage(MessageHandlingContextInterface $context): SendableResponse
{
$content = $context->message();
$header = $context->rawMessage()->header();
$metadata = HfxEventMetadata::fromHfxHeader($header);
$payload = Payload::fromMessageContent($content);
/** #var HfxAggregateRoot $roleAggregate */
// Gpx\HfxEventSourcing\HfxAggregateRoot
$roleAggregate = $this->repository->get($payload->id());
$roleAggregate->registerEvent(SessionInvalidated::class, $payload, $metadata);
$this->repository->save($roleAggregate);
$currentEvent = $roleAggregate->currentEvent();
$context->sendNonBlockingAsynchronous('session_invalidated',$content);
$this->projectionHelper->updateReadModel();
return SendableResponse::answerTo($context->rawMessage(), 1100, [
'responseMessage' => 'Success',
'event' => $currentEvent
]);
}
}
Test case I have written so far
<?php
namespace Gpx\Tests\Feature;
use Ramsey\Uuid\Uuid;
use Gpx\Json\JsonEncode;
use Prophecy\Argument;
use PHPUnit\Framework\TestCase;
use Gpx\HfxEventSourcing\Hfx;
use Gpx\HfxEventSourcing\HfxRepository;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\MessageHandlingContextInterface;
use Gpx\Handlers\BroadcastSessionInvalidated;
use Gpx\Hfx\MessageTransport\Message\ReceivedMessage;
use Prooph\EventSourcing\AggregateRoot;
use Gpx\HfxEventSourcing\HfxAggregateRoot;
class BroadcastSessionInvalidatedTest extends TestCase
{
/** #var HfxRepository */
private $repository;
/** #var Hfx */
private $projectionHelper;
// We have to test handleSynchronousMessage handler whether it is returning sendable response with certain properties in it.
public function testHandleSynchronousMessage()
{
// Expected return value of message() function of $context
$expectedReturnValue = [
"session_id" => "1a92-4376-a8eb-deaf208e1",
"user_id" => "we",
"access_jwt" => "C",
"access_token" => "john#gmail.com",
"refresh_token" => "C",
"refresh_token_expires" => "john#gmail.com"
];
// Expected return value of rawMessage() function of $context
$headerResponseExpected = [
'header' => [
'version' => '2.0',
'originId' => (string)Uuid::uuid4(),
'destination' => 'application/meta#1.0.0',
'sent' => '2017-12-19T10:12:37.941+00:00'
],
'content' => [
'session_id' => "8365526e-fb92-4376-a8eb-deaf208edf61",
'title' => "A task's title."
]
];
// Prophecy means prediction of the future object
// Prediction of $context object starts
$context = $this->prophesize(MessageHandlingContextInterface::class);
$context->message(Argument::any())->willReturn($expectedReturnValue);
$encodedMessage = new JsonEncode($headerResponseExpected);
$rawMessage = ReceivedMessage::fromEncodedMessage($encodedMessage->asString());
$context->rawMessage()->willReturn($rawMessage);
$context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
->shouldBeCalled();
// Prediction of $context object ends
// Repository Mocking Starts
$ravenAggregateRoot = $this->getMockBuilder(HfxAggregateRoot::class)
->disableOriginalConstructor()
->getMock();
$this->ravenRepository = $this->prophesize(HfxRepository::class);
$this->ravenRepository->get('1a92-4376-a8eb-deaf208e1')->shouldBeCalled()->willReturn($ravenAggregateRoot);
$this->ravenRepository->save(Argument::any())->shouldBeCalled();
// Repository Mocking Ends
// Mocking Hfx and calling the method updateReadModel which will return the string UpdateReadModel
$this->projectionHelper = $this->createMock(Hfx::class);
$this->projectionHelper->method('updateReadModel')
->willReturn('UpdateReadModel');
// Actual calling
$broadcastPlatformSessionInvalidated = new BroadcastSessionInvalidated($this->ravenRepository->reveal(), $this->projectionHelper);
//$broadcastPlatformSessionInvalidated = new BroadcastSessionInvalidated($this->ravenRepository, $this->projectionHelper);
$response = $broadcastPlatformSessionInvalidated->handleSynchronousMessage($context->reveal());
$this->assertInstanceOf('Gpx\Hfx\MessageTransport\Response\SendableResponse', $response);
$this->assertArrayHasKey("responseMessage", $response->content()->data());
$this->assertArrayHasKey("event", $response->content()->data());
$this->assertEquals("Success", $response->content()->data()['responseMessage']);
}
}
When I am executing the test it throws an error
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
.E 2 / 2 (100%)
Time: 506 ms, Memory: 6.00MB
There was 1 error:
1) Gpx\Tests\Feature\BroadcastSessionInvalidatedTest::testHandleSynchronousMessage
Prophecy\Exception\Call\UnexpectedCallException: Method call:
- sendNonBlockingAsynchronous("session_invalidated", ["session_id" => "1a92-4376-a8eb-deaf208e1", "user_id" => "we", "access_jwt" => "C", "access_token" => "john#gmail.com", "refresh_token" => "C", "refresh_token_expires" => "john#gmail.com"])
on Double\MessageHandlingContextInterface\P3 was not expected, expected calls were:
- message(*)
- rawMessage()
- sendNonBlockingAsynchronous(exact("platform_session_initiated"), type(array))
/vagrant/services/sessions-stream/app/src/Gpx/Handlers/BroadcastSessionInvalidated.php:54
/vagrant/services/sessions-stream/app/tests/Feature/BroadcastPlatformSessionInvalidatedTest.php:107
ERRORS!
Tests: 2, Assertions: 6, Errors: 1.
What I am doing wrong here?
The Exception tells you what's wrong: you use shouldBeCalled() to tell PHPUnit to check that the method sendNonBlockingAsynchronous() gets called with first parameter platform_session_initiated and an array in the second parameter.
// BroadcastSessionInvalidatedTest
$context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
->shouldBeCalled();
The second condition is met, but in your actual method the first parameter is session_invalidated:
// BroadcastSessionInvalidated
$context->sendNonBlockingAsynchronous('session_invalidated',$content);
So change the expectation to be
// BroadcastSessionInvalidatedTest
$context->sendNonBlockingAsynchronous('session_invalidated', Argument::type("array"))
->shouldBeCalled();
And it will work.
Related
I'm just wondering how you'd mock a service in Symfony.
This is what I've ended up with so far and it's not working as expected. I expect the getVenuesData() method from the NetballFeedService to dump the mocked data in the test (I've set a DD in the command). But when I run the test, it dumps the real time data being pulled from the NetballFeedService.
Test:
public function it_imports_venues(): void
{
$kernel = self::bootKernel();
$netballAPIMock = $this->createMock(NetballFeedService::class);
$netballAPIMock->method('getVenuesData')->willReturn([
800 => ['name' => 'Venue 1', 'location' => 'MEL'],
801 => ['name' => 'Venue 2', 'location' => 'ADE']
]);
$this->getContainer()->set('netballAPI', $netballAPIMock);
$container = $kernel->getContainer();
$container->set('netballAPI', $netballAPIMock);
$application = new Application($kernel);
$command = $application->find('app:feed:import-venues');
$commandTester = new CommandTester($command);
$commandTester->execute([]);
$commandTester->assertCommandIsSuccessful();
}
Command being tested:
protected static $defaultName = 'app:feed:import-venues';
public function __construct(
private readonly NetballFeedService $netballFeedService,
private readonly VenueService $venueService
)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$data = $this->netballFeedService->getVenuesData();
dd($data);
$this->venueService->updateVenuesDataFromFeed($data);
return 0;
}
Class method expected to return mocked data:
public function getVenuesData(): array
{
$response = $this->client->request('GET', self::FIXTURES_URL);
$rawData = json_decode($response->getBody()->getContents(), true);
$rounds = $rawData['data']['results'];
$parsedVenuesData = [];
foreach ($rounds as $round) {
foreach ($round['matches'] as $matches) {
$venueId = $matches['venueId'];
if (array_key_exists($venueId, $matches)) {
continue;
}
$parsedVenuesData[$matches['venueId']] = [
'name' => $matches['venueName'],
'location' => $matches['venueLocation']
];
}
}
ksort($parsedVenuesData);
return $parsedVenuesData;
}
I'm trying to replicate my Laravel test code in Symfony:
/** #test */
public function it_imports_venues()
{
$this->withExceptionHandling();
$this->instance(
NetballAPI::class,
Mockery::mock(NetballAPI::class, function (MockInterface $mock) {
$mock->shouldReceive('getVenuesData')->once()
->andReturn([
800 => [
'name' => 'Venue 1',
'location' => 'MEL'
],
801 => [
'name' => 'Venue 2',
'location' => 'ADE'
],
]);
})
);
$this->artisan('import:venues');
$this->assertDatabaseHas('venues', [
'id' => 800,
'name' => 'Venue 1',
'location' => 'MEL'
]);
$this->assertDatabaseHas('venues', [
'id' => 801,
'name' => 'Venue 2',
'location' => 'ADE'
]);
services_test.yml:
services:
_defaults:
public: true
App\Service\NetballFeedService:
public: true
alias: netballAPI
Just wondering how I can mock the NetballFeedService class in the test instance? Thanks in advance.
I was configuring the service_test.yaml wrong. It should be:
services:
App\Service\NetballFeedService:
public: true
app.netballFeedService:
alias: App\Service\NetballFeedService
public: true
Also, I'm not really used to using YAML files for configs. It's not very readable especially compared to it's PHP file counterpart in this instance. So I'll be experimenting on using both YAML & PHP files for configs/routes etc.
In my laravel application there's need to format a message I need to send into slack. Hence I set a slack log channel into config/logging.php:
'slack' => [
'driver' => 'slack',
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],
Also as seen on documentation I can do a monolog formater, hence I did the following:
namespace App\Logging;
class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->setFormatter(...);
}
}
}
And specified it as tap into my log:
'slack' => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],
But in my formater where do I process the log entry itself? I mean:
The $handler->setFormatter does not seem to be a method of \Illuminate\Log\Logger class.
I cannot find out what method I need to override when I need to provide a custom format. I mean I have the invoke method then afterwards what?
Dimitrios is almost right (or perhaps this worked on older versions) but as Xavier said, if you copy that code exactly you will get the error
Return value of Monolog\Handler\AbstractProcessingHandler::processRecord() must be of the type array, null returned
You are going to want to do something like this instead :
<?php
namespace App\Logging;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SlackWebhookHandler;
use Request;
class CustomiseFormatter
{
protected $request;
public function __construct(Request $request = null)
{
$this->request = $request;
}
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$handler->setFormatter(new LineFormatter(
'[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
));
$handler->pushProcessor([$this, 'processLogRecord']);
}
}
}
public function processLogRecord(array $record): array
{
$record['extra'] += [
'url' => env("APP_URL"),
];
return $record;
}
}
Equally, as with how extra has be modified, you can change any other value.
The formatter for slack should be the following:
namespace App\Logging;
use Monolog\Formatter\LineFormatter;
class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$format=""; // Look on the Monolog's Line formatter documentation
$formatter= new LineFormatter($format,"Y-m-d H:i:s");
$handler->pushProcessor(function ($record) {
//Append extra info of formatting here
});
$handler->setFormatter($formatter);
}
}
}
}
And config the slack not to send attachment nessesary for the formatter to work:
'slack' => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
'attachment' => FALSE,
],
The setFormatter method takes a new Monolog Formatter as seen in: https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#formatters
Also the pushProcessor allows you to populate extra fields on your message eg. Showing an emoji on your log message:
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$format="%emoji% %message%";
$formatter= new LineFormatter($format,"Y-m-d H:i:s");
$handler->pushProcessor(function ($record) {
$record['emoji']=":poop:";
});
$handler->setFormatter($formatter);
}
}
}
I am writing a unit test and I want to check whether the method publish() was called one or more times. This is a snippet from my whole test class:
<?php
namespace App\Tests\Unit;
use Aws\Sns\SnsClient;
use Exception;
use PHPUnit\Framework\TestCase;
class MyClassTest extends TestCase
{
/** #var SnsClient */
private $snsClient;
public function setUp(): void
{
$this->snsClient = $this->getMockBuilder(SnsClient::class)->disableOriginalConstructor()->getMock();
}
/**
* #throws Exception
*/
public function testNoCaseIdentifierSns()
{
$this->snsClient->expects($this->once())->method('publish')->with([
[
'doc_id' => 1,
'bucket' => 'some_bucket',
'key' => 'test.tiff/0.png'
],
'topic_arn'
]);
}
}
But when I run the code above I got the following error:
Trying to configure method "publish" which cannot be configured
because it does not exist, has not been specified, is final, or is
static
I guess the problem here is the method in AWS is defined as #method (see here):
* #method \Aws\Result publish(array $args = [])
It is possible to mock that method? What I am missing here?
UPDATE
After following comments suggestions I transformed my code into the following:
$this->snsClient->expects($this->once())->method('__call')->with([
'Message' => json_encode([
'doc_id' => 1,
'bucket' => 'some_bucket',
'key' => 'test.tiff/0.png'
]),
'TopicArn' => 'topic-arn'
]);
But now I am getting this other error:
Expectation failed for method name is equal to '__call' when invoked 1
time(s) Parameter 0 for invocation Aws\AwsClient::__call('publish',
Array (...)) does not match expected value. 'publish' does not match
expected type "array".
Why? publish() method signature is an array of args
From the thrown exception, we see that the __call function is called with the name of the target function (i.e. 'publish') and an array containing all arguments. As a result, here is how the mock setup can be updated:
$event = [
'Message' => json_encode([
'doc_id' => 1,
'bucket' => 'some_bucket',
'key' => 'test.tiff/0.png'
]),
'TopicArn' => 'topic-arn'
];
$this->snsClient
->expects($this->once())
->method('__call')
->with('publish', [$event]);
I have to test this class for failure condition
<?php
namespace Gpx\Handlers;
use Gpx\EntityInfrastructure\Model\Events\PlatformSessionInitiated;
use Gpx\EntityInfrastructure\Model\Payload;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\SynchronousHandlerInterface;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\MessageHandlingContextInterface;
use Gpx\HfxEventSourcing\HfxAggregateRoot;
use Gpx\HfxEventSourcing\HfxEventMetadata;
use Gpx\HfxEventSourcing\HfxProjectionHelper;
use Gpx\HfxEventSourcing\HfxRepository;
use Gpx\Hfx\MessageTransport\Response\SendableResponse;
use Gpx\Exceptions\IdNotDefinedException;
use Gpx\Exceptions\AggregateNotFoundException;
class BroadcastPlatformSessionInitiated implements SynchronousHandlerInterface
{
/** #var HfxRepository */
private $repository;
/** #var HfxProjectionHelper */
private $projectionHelper;
public function __construct(HfxRepository $repository, HfxProjectionHelper $projectionHelper)
{
$this->repository = $repository;
$this->projectionHelper = $projectionHelper;
}
/*
* #param MessageHandlingContextInterface $context
* #return SendableResponse
*/
public function handleSynchronousMessage(MessageHandlingContextInterface $context): SendableResponse
{
try {
$content = $context->message();
$header = $context->rawMessage()->header();
$metadata = HfxEventMetadata::fromHfxHeader($header);
$payload = Payload::fromMessageContent($content);
$roleAggregate = HfxAggregateRoot::createEntityFromEvent(PlatformSessionInitiated::class, $payload, $metadata);
$this->repository->save($roleAggregate);
$currentEvent = $roleAggregate->currentEvent();
$context->sendNonBlockingAsynchronous('platform_session_initiated', $currentEvent);
$this->projectionHelper->updateReadModel(HfxAggregateRoot::class);
return SendableResponse::answerTo($context->rawMessage(), 1000, [
'responseMessage' => 'Success',
'event' => $currentEvent
]);
} catch (IdNotDefinedException $e) {
return SendableResponse::answerTo($context->rawMessage(), 2000, [
'responseMessage' => 'Failure. Session Id is not defined.'
]);
}
}
}
Following is the test case I have written
<?php
namespace Gpx\Tests\Handlers;
use Ramsey\Uuid\Uuid;
use Gpx\Json\JsonEncode;
use Prophecy\Argument;
use PHPUnit\Framework\TestCase;
use Gpx\HfxEventSourcing\HfxProjectionHelper;
use Gpx\HfxEventSourcing\HfxRepository;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\MessageHandlingContextInterface;
use Gpx\Handlers\BroadcastPlatformSessionInitiated;
use Gpx\Hfx\MessageTransport\Message\ReceivedMessage;
use Gpx\Exceptions\IdNotDefinedException;
class BroadcastPlatformSessionInitiatedTest extends TestCase
{
/** #var HfxRepository */
private $repository;
/** #var HfxProjectionHelper */
private $projectionHelper;
/** #var MessageHandlingContext */
private $context;
/**
* This will run before each test
*/
public function setUp()
{
// Expected return value of message() function of $this->context
$expectedReturnValue = [
"session_id" => "1a92-4376-a8eb-deaf208ssess11",
"user_id" => "we",
"access_jwt" => "C",
"access_token" => "john#gmail.com",
"refresh_token" => "C",
"refresh_token_expires" => "john#gmail.com"
];
// Expected return value of rawMessage() function of $this->context
$headerResponseExpected = [
'header' => [
'version' => '2.0',
'originId' => (string)Uuid::uuid4(),
'destination' => 'application/meta#1.0.0',
'sent' => '2017-12-19T10:12:37.941+00:00'
],
'content' => [
'session_id' => null,
'title' => "A task's title."
]
];
// Prediction of $this->context object starts
$this->context = $this->prophesize(MessageHandlingContextInterface::class);
$this->context->message(Argument::any())->willReturn($expectedReturnValue);
$encodedMessage = new JsonEncode($headerResponseExpected);
$rawMessage = ReceivedMessage::fromEncodedMessage($encodedMessage->asString());
$this->context->rawMessage()->willReturn($rawMessage);
$this->context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
->shouldBeCalled();
// Prediction of $this->context object ends
}
// We have to test handleSynchronousMessage handler whether it is returning sendable response with certain properties in it.
public function testHandleSynchronousMessageForSuccess()
{
// Prophecy means prediction of the future object
$this->ravenRepository = $this->prophesize(HfxRepository::class);
$this->ravenRepository->save(Argument::any())
->shouldBeCalled();
// Mocking HfxProjectionHelper and calling the method updateReadModel which will return the string UpdateReadModel
$this->projectionHelper = $this->createMock(HfxProjectionHelper::class);
$this->projectionHelper->method('updateReadModel')
->willReturn('UpdateReadModel');
// Actual calling
$broadcastPlatformSessionInitiated = new BroadcastPlatformSessionInitiated($this->ravenRepository->reveal(), $this->projectionHelper);
$response = $broadcastPlatformSessionInitiated->handleSynchronousMessage($this->context->reveal());
$this->assertInstanceOf('Gpx\Hfx\MessageTransport\Response\SendableResponse', $response);
$this->assertArrayHasKey("responseMessage", $response->content()->data());
$this->assertArrayHasKey("event", $response->content()->data());
$this->assertEquals("Success", $response->content()->data()['responseMessage']);
}
// We have to test handleSynchronousMessage handler whether it is returning sendable response with certain properties in it.
public function testHandleSynchronousMessageForFailure()
{
// Expected return value of message() function of $this->context
$expectedReturnValue = [
"session_id" => null,
"user_id" => "we",
"access_jwt" => "C",
"access_token" => "john#gmail.com",
"refresh_token" => "C",
"refresh_token_expires" => "john#gmail.com"
];
// Expected return value of rawMessage() function of $this->context
$headerResponseExpected = [
'header' => [
'version' => '2.0',
'originId' => (string)Uuid::uuid4(),
'destination' => 'application/meta#1.0.0',
'sent' => '2017-12-19T10:12:37.941+00:00'
],
'content' => [
'session_id' => '1a92-4376-a8eb-deaf208ssess11',
'title' => "A task's title."
]
];
// Prediction of $this->context object starts
$this->context = $this->prophesize(MessageHandlingContextInterface::class);
$this->context->message(Argument::any())->willReturn($expectedReturnValue);
$encodedMessage = new JsonEncode($headerResponseExpected);
$rawMessage = ReceivedMessage::fromEncodedMessage($encodedMessage->asString());
$this->context->rawMessage()->willReturn($rawMessage);
$this->context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))->shouldNotBeCalled();
// Prediction of $this->context object ends
// Prophecy means prediction of the future object
$this->ravenRepository = $this->prophesize(HfxRepository::class);
// Mocking HfxProjectionHelper and calling the method updateReadModel which will return the string UpdateReadModel
$this->projectionHelper = $this->createMock(HfxProjectionHelper::class);
$this->projectionHelper->method('updateReadModel')->willReturn('UpdateReadModel');
// Actual calling
$broadcastPlatformSessionInitiated = new BroadcastPlatformSessionInitiated($this->ravenRepository->reveal(), $this->projectionHelper);
$response = $broadcastPlatformSessionInitiated->handleSynchronousMessage($this->context->reveal());
$this->assertInstanceOf('Gpx\Hfx\MessageTransport\Response\SendableResponse', $response);
$this->assertArrayHasKey("responseMessage", $response->content()->data());
$this->assertEquals("Failure. Session Id is not defined.", $response->content()->data()['responseMessage']);
}
}
This is the failure I am getting for testHandleSynchronousMessageForFailure
Gpx\Tests\Handlers\BroadcastPlatformSessionInitiatedTest::testHandleSynchronousMessageForFailure
Some predictions failed:
Double\MessageHandlingContextInterface\P3:
No calls have been made that match:
Double\MessageHandlingContextInterface\P3->sendNonBlockingAsynchronous(exact("platform_session_initiated"), type(array))
but expected at least one.
FAILURES!
Tests: 3, Assertions: 18, Failures: 1.
Could any one please clarify what I am doing wrong here?
Your setUp() method gets called before every test, so testHandleSynchronousMessageForFailure() is also expecting sendNonBlockingAsynchronous() to be called:
$this->context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
->shouldBeCalled();
Even if you call shouldNotBeCalled() on it in the failure test. So, move the shouldBeCalled() call to the testHandleSynchronousMessageForSuccess(), that way it will expect it to be called in the success test, and not to be called in the failure test.
You should also tell PHPUnit to expect an IdNotDefinedException in the failure test:
$this->expectException(Gpx\Exceptions\IdNotDefinedException::class);
Here is my code:
// Yoda namespace
namespace Yoda\Application\Config\Feature;
// use zend config
use Zend\Config\Config;
// CacheConfig class
class CacheConfig extends Config
{
/**
* Default cache type for now
*
* #var string
*/
const DEFAULT_CACHE_TYPE = 'filesystem';
/**
* Default cache ttl for now
*
* #var integer
*/
const DEFAULT_CACHE_TTL = 3600;
/**
* Constructor. Creates config data for caching
*/
public function __construct()
{
$config=[
'name'=> static::DEFAULT_CACHE_TYPE,
'options' => [
'ttl' => static::DEFAULT_CACHE_TTL,
'cache_dir' => '/var/www/html/yoda/data/cache'
]
];
parent::__construct($config,true);
}
}
When I use this code the application breaks and says The localhost page isn't working however when I just pass the config array into a standard Zend Config object it works fine.
Here's my usage code:
$config=[
'name'=> 'filesystem',
'options' => [
'ttl' => 3600,
'cache_dir' => '/var/www/html/yoda/data/cache'
]
];
//works fine
$configCache = new Config($config);
//breaks
$configCache = new CacheConfig();
Not sure whats wrong here.
It's because in the Config class the constructor loads a static instance of itself. WHen I did this:
public function __construct()
{
$config=[
'name'=> static::DEFAULT_CACHE_TYPE,
'options' => [
'ttl' => static::DEFAULT_CACHE_TTL,
'cache_dir' => yoda::registry('cache_dir')
]
];
$this->allowModifications = true;
foreach ($config as $key => $value) {
if (is_array($value)) {
$this->data[$key] = new Config($value, $this->allowModifications);
} else {
$this->data[$key] = $value;
}
}
}
It seems to work when I replace it with Config
Instead of modifying zend config class you can do following in your configCache constructor. When config class will call cache class with array you pass the control back to config class with received array. It will then set the config object properly.The error is because of Static Bindings.
/**
* Constructor. Creates config data for caching
*/
public function __construct( $arr = [])
{
$config=[
'name'=> static::DEFAULT_CACHE_TYPE,
'options' => [
'ttl' => static::DEFAULT_CACHE_TTL,
'cache_dir' => '/var/www/html/yoda/data/cache'
]
];
if (count($arr) > 0)
{
parent::__construct($arr,true);
}
else
{
parent::__construct($config,true);
}
}
$configCache = new CacheConfig();
print_r($configCache);