Skip to content

Commit

Permalink
Replace alert stream with API payload
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Oct 21, 2024
1 parent 5fc153b commit dae0499
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
21 changes: 14 additions & 7 deletions app/src/Controller/Group/GroupCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use Illuminate\Database\Connection;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Alert\AlertStream;
use UserFrosting\Fortress\RequestSchema;
use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
use UserFrosting\Fortress\Transformer\RequestDataTransformer;
use UserFrosting\Fortress\Validator\ServerSideValidator;
use UserFrosting\I18n\Translator;
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface;
Expand Down Expand Up @@ -51,7 +51,7 @@ class GroupCreateAction
* Inject dependencies.
*/
public function __construct(
protected AlertStream $alert,
protected Translator $translator,
protected Authenticator $authenticator,
protected Connection $db,
protected GroupInterface $groupModel,
Expand All @@ -71,8 +71,11 @@ public function __construct(
public function __invoke(Request $request, Response $response): Response
{
$this->validateAccess();
$this->handle($request);
$payload = json_encode([], JSON_THROW_ON_ERROR);
$group = $this->handle($request);
$payload = json_encode([
'success' => true,
'message' => $this->translator->translate('GROUP.CREATION_SUCCESSFUL', $group->toArray()),
], JSON_THROW_ON_ERROR);
$response->getBody()->write($payload);

return $response->withHeader('Content-Type', 'application/json');
Expand All @@ -82,8 +85,10 @@ public function __invoke(Request $request, Response $response): Response
* Handle the request.
*
* @param Request $request
*
* @return GroupInterface
*/
protected function handle(Request $request): void
protected function handle(Request $request): GroupInterface
{
// Get POST parameters.
$params = (array) $request->getParsedBody();
Expand All @@ -105,7 +110,7 @@ protected function handle(Request $request): void

// All checks passed! log events/activities and create group
// Begin transaction - DB will be rolled back if an exception occurs
$this->db->transaction(function () use ($data, $currentUser) {
$group = $this->db->transaction(function () use ($data, $currentUser) {
// Create the group
$group = new $this->groupModel($data);
$group->save();
Expand All @@ -116,8 +121,10 @@ protected function handle(Request $request): void
'user_id' => $currentUser->id,
]);

$this->alert->addMessage('success', 'GROUP.CREATION_SUCCESSFUL', $data);
return $group;
});

return $group;
}

/**
Expand Down
18 changes: 4 additions & 14 deletions app/tests/Controller/Group/GroupCreateActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace UserFrosting\Sprinkle\Admin\Tests\Controller\Group;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use UserFrosting\Alert\AlertStream;
use UserFrosting\Sprinkle\Account\Database\Models\Group;
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Account\Testing\WithTestUser;
Expand Down Expand Up @@ -81,20 +80,17 @@ public function testPost(): void

// Assert response status & body
$this->assertResponseStatus(200, $response);
$this->assertJsonResponse([], $response);
$this->assertJsonResponse([
'success' => true,
'message' => 'Successfully created group <strong>The Foo</strong>',
], $response);

// Make sure the user is added to the db by querying it
/** @var Group */
$group = Group::where('slug', 'foo')->first();
$this->assertSame('The Foo', $group['name']);
$this->assertSame('fas fas-icon', $group['icon']);
$this->assertSame('Foo description', $group['description']);

// Test message
/** @var AlertStream */
$ms = $this->ci->get(AlertStream::class);
$messages = $ms->getAndClearMessages();
$this->assertSame('success', array_reverse($messages)[0]['type']);
}

/**
Expand Down Expand Up @@ -125,12 +121,6 @@ public function testPostForFailedValidation(): void
'description' => 'Please specify a value for <strong>Slug</strong>. Slug must be between 1 and 255 characters in length.',
'status' => 400,
], $response);

// Test message
/** @var AlertStream */
$ms = $this->ci->get(AlertStream::class);
$messages = $ms->getAndClearMessages();
$this->assertSame('danger', array_reverse($messages)[0]['type']);
}

/**
Expand Down

0 comments on commit dae0499

Please sign in to comment.