Skip to content

Commit

Permalink
Added some tests and code cleanup [SLE-192]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Apr 30, 2024
1 parent 8ff4e32 commit ffee1fd
Show file tree
Hide file tree
Showing 57 changed files with 650 additions and 321 deletions.
4 changes: 3 additions & 1 deletion config/functions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
/**
* Autoload functions available everywhere across the application.
* Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Composer#autoload
* Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Composer#autoload.
*
* @param ?string $text
*/

/**
Expand Down
14 changes: 7 additions & 7 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
->setName('user-list');

$group // User create form is rendered by the client and loads the available dropdown options via Ajax
->get('/dropdown-options', \App\Application\Action\User\Ajax\FetchDropdownOptionsForUserCreateAction::class)
->setName('user-dropdown-options');
->get('/dropdown-options', \App\Application\Action\User\Ajax\UserCreateDropdownOptionsFetchAction::class)
->setName('user-create-dropdown');

$group->get('/activity', \App\Application\Action\User\Ajax\UserActivityFetchListAction::class)
->setName('user-get-activity');
Expand Down Expand Up @@ -100,7 +100,7 @@
// Client create form is rendered by the client and loads the available dropdown options via Ajax
$group->get(
'/dropdown-options',
\App\Application\Action\Client\Ajax\FetchDropdownOptionsForClientCreateAction::class
\App\Application\Action\Client\Ajax\ClientCreateDropdownOptionsFetchAction::class
)->setName('client-create-dropdown');

$group->post('', \App\Application\Action\Client\Ajax\ClientCreateAction::class)
Expand All @@ -123,18 +123,18 @@
'note-read-page'
);
$group->post('', \App\Application\Action\Note\Ajax\NoteCreateAction::class)->setName(
'note-submit-creation'
'note-create-submit'
);
$group->put('/{note_id:[0-9]+}', \App\Application\Action\Note\Ajax\NoteUpdateAction::class)
->setName('note-submit-modification');
->setName('note-update-submit');
$group->delete('/{note_id:[0-9]+}', \App\Application\Action\Note\Ajax\NoteDeleteAction::class)
->setName('note-submit-delete');
->setName('note-delete-submit');
})->add(UserAuthenticationMiddleware::class);

// API routes
$app->group('/api', function (RouteCollectorProxy $group) {
// Client creation API call
$group->post('/clients', \App\Application\Action\Client\Ajax\ApiClientCreateAction::class)
$group->post('/clients', \App\Application\Action\Client\Api\ApiClientCreateAction::class)
->setName('api-client-create-submit');
})// Cross-Origin Resource Sharing (CORS) middleware. Allow another domain to access '/api' routes.
// If an error occurs, the CORS middleware will not be executed and the exception caught and a response
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ parameters:
- src
- tests
ignoreErrors:
- '#Parameter \#2 \$locale of function setlocale expects array\|string\|null, int given.#'
- '#^Method .*::getTranslatedValues\(\) is unused\.$#'
- '#Parameter \#2 \$locale of function setlocale expects array\|string\|null, int given.#'
3 changes: 0 additions & 3 deletions public/assets/general/general-js/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ window.addEventListener("load", function (event) {
/** Throttle time countdown */
countDownThrottleTimer();


initCollapsible();

/** Scroll to anchor if there is any in the url */
scrollToAnchor();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
/*Start to wrap right before ending*/
100% {
white-space: break-spaces;
overflow-wrap: anywhere;
/*Takes initial max-width value which is 90%*/
}
}
Expand Down Expand Up @@ -297,6 +298,7 @@
margin: 15px 0 10px 0px;
transform: translateX(130%);
white-space: break-spaces;
overflow-wrap: anywhere;
/*Allow pointer events on the flash message after removing it on the container*/
pointer-events: auto;
}
Expand Down
3 changes: 1 addition & 2 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
/**
* Bootstrap the application
* Bootstrap the application.
*
* Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Web-Server-Config-and-Bootstrapping
*/

$app = require __DIR__ . '/../config/bootstrap.php';

$app->run();
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
);
// Pre-fill email input field for more user comfort.
if ($invalidTokenException->userData->email !== null) {
$this->templateRenderer->addPhpViewAttribute('preloadValues', ['email' => $invalidTokenException->userData->email]);
$this->templateRenderer->addPhpViewAttribute(
'preloadValues',
['email' => $invalidTokenException->userData->email]
);
}

$this->logger->error(
Expand All @@ -67,8 +70,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
// The login page is rendered, but the url is reset-password. In login-main.js the url is replaced, and
// the password-forgotten form is shown instead of the login form.
return $this->templateRenderer->render($response, 'authentication/login.html.php');
} // Validation Exception has to be caught here and not middleware as the token,
// and id have to be added to php view
} // Validation Exception has to be caught here and not middleware as the token, and id are added to php view
catch (ValidationException $validationException) {
// Render reset-password form with token, and id so that it can be submitted again
$flash->add('error', $validationException->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
'id' => $queryParams['id'],
]);
}

// Replace token from query params with ***
$queryParams['token'] = '***';
// Log error
$this->logger->error(
'GET request malformed: ' . json_encode($queryParams, JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR)
);
Expand Down
6 changes: 2 additions & 4 deletions src/Application/Action/Client/Ajax/ClientCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ public function __invoke(
): ResponseInterface {
$clientValues = (array)$request->getParsedBody();

// Validation and Forbidden exception caught in respective middlewares
$insertId = $this->clientCreator->createClient($clientValues);

if (0 !== $insertId) {
return $this->jsonResponder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201);
}
$response = $this->jsonResponder->encodeAndAddToResponse($response, [

return $this->jsonResponder->encodeAndAddToResponse($response, [
'status' => 'warning',
'message' => 'Client not created',
]);

return $response->withAddedHeader('Warning', 'The client could not be created');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final readonly class FetchDropdownOptionsForClientCreateAction
final readonly class ClientCreateDropdownOptionsFetchAction
{
public function __construct(
private JsonResponder $jsonResponder,
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Action/Client/Ajax/ClientDeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __invoke(
): ResponseInterface {
$clientId = (int)$args['client_id'];

// Delete client logic
// Delete client
$deleted = $this->clientDeleter->deleteClient($clientId);

$flash = $this->session->getFlash();
Expand All @@ -38,11 +38,11 @@ public function __invoke(

$response = $this->jsonResponder->encodeAndAddToResponse(
$response,
['status' => 'warning', 'message' => 'Client not deleted.']
['status' => 'warning', 'message' => 'Client has not been deleted.']
);
// If not deleted, inform user
$flash->add('warning', 'The client was not deleted');

return $response->withAddedHeader('Warning', 'The client was not deleted');
return $response->withAddedHeader('Warning', 'The client has not been deleted.');
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace App\Application\Action\Client\Ajax;
namespace App\Application\Action\Client\Api;

use App\Application\Responder\JsonResponder;
use App\Domain\Client\Service\ClientCreatorFromApi;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* Api action for external domains.
* Api action to create client.
*/
final readonly class ApiClientCreateAction
{
Expand All @@ -31,11 +31,9 @@ public function __invoke(
return $this->jsonResponder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201);
}

$response = $this->jsonResponder->encodeAndAddToResponse($response, [
return $this->jsonResponder->encodeAndAddToResponse($response, [
'status' => 'warning',
'message' => 'Client not created',
'message' => 'Client was not created',
]);

return $response->withAddedHeader('Warning', 'The client could not be created');
}
}
8 changes: 8 additions & 0 deletions src/Application/Action/Common/TranslateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public function __construct(
) {
}

/**
* Returns strings provided in requests as translated strings.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$queryParams = $request->getQueryParams();
Expand Down
24 changes: 0 additions & 24 deletions src/Application/Action/Dashboard/PhpDevTestAction.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Application/Action/Note/Ajax/NoteDeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __invoke(

$response = $this->jsonResponder->encodeAndAddToResponse(
$response,
['status' => 'warning', 'message' => 'Note not deleted.']
['status' => 'warning', 'message' => 'Note has not been deleted.']
);

return $response->withAddedHeader('Warning', 'The note was not deleted');
Expand Down
14 changes: 10 additions & 4 deletions src/Application/Action/User/Ajax/UserCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

if ($insertId !== false) {
$this->logger->info('User "' . $userValues['email'] . '" created');
} else {
$this->logger->info('Account creation tried with existing email: "' . $userValues['email'] . '"');
$response = $response->withAddedHeader('Warning', 'The user was not created');

return $this->jsonResponder->encodeAndAddToResponse(
$response,
['status' => 'success', 'data' => null],
201
);
}

return $this->jsonResponder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201);
return $this->jsonResponder->encodeAndAddToResponse($response, [
'status' => 'warning',
'message' => 'User not created',
]);
} catch (TransportExceptionInterface $e) {
// Flash message has to be added in the frontend as form is submitted via Ajax
$this->logger->error('Mailer exception: ' . $e->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final readonly class FetchDropdownOptionsForUserCreateAction
final readonly class UserCreateDropdownOptionsFetchAction
{
public function __construct(
private JsonResponder $jsonResponder,
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Action/User/Ajax/UserFetchListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __invoke(

return $this->jsonResponder->encodeAndAddToResponse($response, [
'userResultDataArray' => $userResultDataArray,
'statuses' => UserStatus::toTranslatedNamesArray(),
'statuses' => UserStatus::getAllDisplayNames(),
]);
}
}
75 changes: 0 additions & 75 deletions src/Application/Middleware/NonFatalErrorHandlerMiddleware.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Application/Middleware/PhpViewMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
* Check if the user is allowed to see the user list.
*
* @param int $loggedInUserId
*
* @return bool
*/
private function checkUserListAuthorization(int $loggedInUserId): bool
Expand Down
Loading

0 comments on commit ffee1fd

Please sign in to comment.