Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

feat: List messages in a conversation. #23

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions module/Olcs/src/Controller/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Common\FeatureToggle;
use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\ByConversation as ByConversationQuery;
use Dvsa\Olcs\Transfer\Query\Messaging\Conversations\ByOrganisation as ByOrganisationQuery;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
use Laminas\View\Model\ViewModel;
Expand Down Expand Up @@ -66,4 +67,31 @@ public function indexAction(): ViewModel

return $view;
}

public function viewAction(): ViewModel
{
$params = [
'page' => $this->params()->fromQuery('page', 1),
'limit' => $this->params()->fromQuery('limit', 10),
'conversation' => $this->params()->fromRoute('conversationId'),
'query' => $this->params()->fromQuery(),
];

$response = $this->handleQuery(ByConversationQuery::create($params));

if ($response->isOk()) {
$messages = $response->getResult();
} else {
$this->flashMessengerHelper->addErrorMessage('unknown-error');
$messages = [];
}

$table = $this->tableFactory
->buildTable('messages-view', $messages, $params);

$view = new ViewModel(['table' => $table]);
$view->setTemplate('messages-view');

return $view;
}
}
29 changes: 29 additions & 0 deletions module/Olcs/src/Table/Tables/messages-view.table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Common\Service\Table\Formatter\ExternalConversationMessage;

return [
'attributes' => [
'class' => 'no-row-border-separator'
],
'variables' => [
'id' => 'messages-list-table',
'title' => 'Messages',
'empty_message' => 'There are no message records linked to this conversation to display'
],
'settings' => [
'paginate' => [
'limit' => [
'options' => [10, 25, 50],
],
],
],
'columns' => [
[
'name' => 'id',
'formatter' => ExternalConversationMessage::class,
],
],
];
27 changes: 27 additions & 0 deletions module/Olcs/view/messages-view.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
echo $this->partial(
'partials/page-header-simple',
[
'pageTitle' => $this->translate('dashboard.messages.view.title'),
],
);
?>

<div class="row">
<div class="dashboard two-thirds js-body">
<?php
echo $this->flashMessengerAll();

/* @var \Laminas\View\Helper\Navigation\Menu $menu */
$menu = $this->navigation($this->navigation('navigation')->getContainer()->findBy('id', 'dashboard-licences-applications'))->menu();

echo $menu->setMinDepth(0)
->setMaxDepth(0)
->setPartial('partials/tabs-nav');

echo $this->table;
?>
</div>

<?php echo $this->partial('partials/dashboard-right-column'); ?>
</div>
94 changes: 94 additions & 0 deletions test/Olcs/src/Controller/ConversationsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace OlcsTest\Controller;

use Common\Controller\Plugin\HandleQuery;
use Common\Service\Cqrs\Response;
use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
use Laminas\Mvc\Controller\Plugin\Params;
use Laminas\View\Model\ViewModel;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase as TestCase;
use Olcs\Controller\ConversationsController as Sut;
use ReflectionClass;
use LmcRbacMvc\Service\AuthorizationService;

class ConversationsControllerTest extends TestCase
{
protected $sut;

public function setUp(): void
{
$this->mockNiTextTranslationUtil = m::mock(NiTextTranslation::class)->makePartial();
$this->mockAuthService = m::mock(AuthorizationService::class)->makePartial();
$this->mockFlashMessengerHelper = m::mock(FlashMessengerHelperService::class)->makePartial();
$this->mockTableFactory = m::mock(TableFactory::class)->makePartial();

wadedvsa marked this conversation as resolved.
Show resolved Hide resolved
$this->sut = m::mock(Sut::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

$reflectionClass = new ReflectionClass(Sut::class);
$this->setMockedProperties($reflectionClass, 'niTextTranslationUtil', $this->mockNiTextTranslationUtil);
$this->setMockedProperties($reflectionClass, 'authService', $this->mockAuthService);
$this->setMockedProperties($reflectionClass, 'flashMessengerHelper', $this->mockFlashMessengerHelper);
$this->setMockedProperties($reflectionClass, 'tableFactory', $this->mockTableFactory);
}

public function setMockedProperties(ReflectionClass $reflectionClass, string $property, $value): void
{
$reflectionProperty = $reflectionClass->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->sut, $value);
}

public function testViewAction(): void
{
$mockResponse = m::mock(Response::class);
$mockResponse->shouldReceive('isOk')
->andReturn(true);
$mockResponse->shouldReceive('getResult')
->andReturn([]);

$mockHandleQuery = m::mock(HandleQuery::class)
->makePartial();
$mockHandleQuery->shouldReceive('__invoke')
->andReturn($mockResponse);

$mockParams = m::mock(Params::class);
$mockParams->shouldReceive('fromQuery')
->with('page', 1)
->andReturn(1);
$mockParams->shouldReceive('fromQuery')
->with('limit', 10)
->andReturn(10);
$mockParams->shouldReceive('fromQuery')
->withNoArgs()
->andReturn([]);
$mockParams->shouldReceive('fromRoute')
->with('conversationId')
->andReturn(1);

$this->sut->shouldReceive('params')
->andReturn($mockParams);
$this->sut->shouldReceive('plugin')
->with('handleQuery')
->andReturn($mockHandleQuery);

$table = '<table/>';

$this->mockTableFactory->shouldReceive('buildTable')
->with(
'messages-view',
[],
['page' => 1, 'limit' => 10, 'conversation' => 1, 'query' => []],
)
->andReturn($table);

$view = $this->sut->viewAction();
$this->assertInstanceOf(ViewModel::class, $view);
$this->assertEquals($table, $view->getVariable('table'));
}
}
Loading