Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/#60-add-getuser-hel…
Browse files Browse the repository at this point in the history
…per' into combine-branches-in-work

# Conflicts:
#	config/blt.php
#	src/BLTServiceProvider.php
#	src/Features/Traits/Dispatcher.php
#	src/Features/Traits/Middleware.php
#	src/Helpers/ArrayHelper.php
#	src/Helpers/RecognizeClassHelper.php
#	src/Helpers/TypesEnum.php
  • Loading branch information
JakubKermes committed Jun 27, 2024
2 parents 66df26a + eeaac5c commit 63efae6
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 32 deletions.
2 changes: 1 addition & 1 deletion codestyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Blumilk\Codestyle\Configuration\Defaults\Paths;

$config = new Config(
paths: new Paths("src", "tests", "codestyle.php"),
paths: new Paths("src", "tests", "codestyle.php", "config"),
);

return $config->config();
4 changes: 3 additions & 1 deletion config/blt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
return [
"namespaces" => [
"default" => "App\\",
"types" => [],
"types" => [
"User" => "App\Models\\",
],
],
];
103 changes: 102 additions & 1 deletion src/Features/Traits/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Blumilk\BLT\Helpers\UserHelper;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Container\BindingResolutionException;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\Response;

trait Authentication
{
Expand All @@ -19,7 +22,8 @@ trait Authentication
public function userIsAuthenticatedInSessionAs(string $value, string $field): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->login($this->getUserModel()::query()->where($field, $value)->first());
$user = UserHelper::getBy($field, $value);
$auth->login($user);
}

/**
Expand Down Expand Up @@ -52,6 +56,103 @@ public function userAuthenticatedInSessionIs(string $value, string $field): void
Assert::assertEquals($value, $auth->user()?->{$field});
}

/**
* @Given there is an authenticated user with email :email
* @throws BindingResolutionException
*/
public function thereIsAuthenticatedUserWithEmail(string $email): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->login($this->getUserModel()::query()->where("email", $email)->first());
}

/**
* @Given there are users in the database:
*/
public function thereAreUsersInTheDatabase(TableNode $table): void
{
foreach ($table->getColumnsHash() as $userData) {
$this->getUserModel()::firstOrCreate($userData);
}
}

/**
* @Given there is an unauthenticated user
* @throws BindingResolutionException
*/
public function thereIsAnUnauthenticatedUser(): void
{
$this->thereIsNoUserAuthenticatedInSession();
}

/**
* @Then the authenticated user email should be :email
* @throws BindingResolutionException
*/
public function authenticatedUsersEmailShouldBe(string $email): void
{
$auth = $this->getContainer()->make(Guard::class);
Assert::assertEquals($email, $auth->user()->email);
}

/**
* @Then the authenticated user should have the attribute :attribute with value :value
* @throws BindingResolutionException
*/
public function authenticatedUserShouldHaveAttribute(string $attribute, $value): void
{
$auth = $this->getContainer()->make(Guard::class);
$user = $auth->user();
Assert::assertEquals($value, $user->{$attribute});
}

/**
* @Given the user is authenticated with attributes:
*/
public function userIsAuthenticatedWithAttributes(TableNode $table): void
{
$attributes = $table->getRowsHash();
$user = $this->getUserModel()::firstOrCreate($attributes);
$auth = $this->getContainer()->make(Guard::class);
$auth->login($user);
}

/**
* @Then the user should be able to logout
* @throws BindingResolutionException
*/
public function userShouldBeAbleToLogout(): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->logout();
Assert::assertNull($auth->user());
}

/**
* @Then the authenticated user should be redirected to :url
* @throws BindingResolutionException
*/
public function authenticatedUserShouldBeRedirectedTo(string $url): void
{
$response = $this->response;
Assert::assertTrue(
$response->isRedirect($url),
"Authenticated user was not redirected to $url.",
);
}

/**
* @Then the user with email :email should not be able to access :url
* @throws BindingResolutionException
*/
public function userWithEmailShouldNotBeAbleToAccess(string $email, string $url): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->login($this->getUserModel()::query()->where("email", $email)->first());
$response = $this->call("GET", $url);
Assert::assertEquals(Response::HTTP_FORBIDDEN, $response->status());
}

/**
* @return class-string
*/
Expand Down
18 changes: 8 additions & 10 deletions src/Features/Traits/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ trait Dispatcher
{
use Application;

private BusFake $busFake;
private EventFake $eventFake;
protected BusFake $busFake;
protected EventFake $eventFake;

/**
* @Given bus is running
Expand Down Expand Up @@ -85,16 +85,14 @@ public function assertDispatched(string $objectName, int $count = 1): void
$this->resolveFaker($objectName)->assertDispatched($objectClass, $count);
}

private function resolveFaker($objectName): BusFake|EventFake
protected function resolveFaker(string $objectName): BusFake|EventFake
{
$objectType = RecognizeClassHelper::guessType($objectName);

if ($objectType === TypesEnum::JOB->value) {
return $this->busFake;
} elseif ($objectType === TypesEnum::EVENT->value) {
return $this->eventFake;
}

throw new InvalidArgumentException("Unsupported object type: $objectType");
return match ($objectType) {
TypesEnum::Job->value => $this->busFake,
TypesEnum::Event->value => $this->eventFake,
default => throw new InvalidArgumentException("Unsupported object type: $objectType"),
};
}
}
12 changes: 11 additions & 1 deletion src/Features/Traits/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Closure;
use Illuminate\Foundation\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use PHPUnit\Framework\Assert;

Expand All @@ -24,7 +25,7 @@ public function disableMiddleware(string $middleware): void
$this->getContainer()->instance(
$middleware,
new class() {
public function handle(Request $request, Closure $next)
public function handle(Request $request, Closure $next): Response
{
return $next($request);
}
Expand Down Expand Up @@ -81,4 +82,13 @@ public function requestShouldHaveUuidInField(string $field): void
"The request field $field does not contain a valid UUID.",
);
}

/**
* @Then the middleware :middleware should be applied to the request
*/
public function middlewareShouldBeAppliedToRequest(string $middleware): void
{
$request = $this->getContainer()->make(Request::class);
Assert::assertTrue($request->attributes->has($middleware));
}
}
2 changes: 1 addition & 1 deletion src/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function toArray(string|array $input, string $separator = " "): ar
return explode($separator, $input);
}

public static function toString(string|array $input, $separator = " "): string
public static function toString(string|array $input, string $separator = " "): string
{
if (is_string($input)) {
return $input;
Expand Down
12 changes: 8 additions & 4 deletions src/Helpers/RecognizeClassHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ public static function recognizeObjectClass(string $objectName): string
return $objectName;
}

$objectName = Str::ucfirst(Str::singular($objectName));
$objectName = Str::ucfirst($objectName);

return self::getObjectNamespace($objectName) . $objectName;
return self::getObjectNamespace(Str::singular($objectName)) . $objectName;
}

public static function getObjectNamespace(string $objectName): string
{
$type = self::guessType($objectName);
$typeNamespaces = config("blt.namespaces.types");

if (array_key_exists($objectName, $typeNamespaces)) {
return $typeNamespaces[$objectName];
}

if (array_key_exists($type, $typeNamespaces)) {
return $typeNamespaces[$type];
}
$type = Str::plural(Str::ucfirst($type));

$type = Str::plural(Str::ucfirst($type));
$defaultNamespace = config("blt.namespaces.default") ?? "App\\";

return $defaultNamespace . $type . "\\";
Expand All @@ -46,6 +50,6 @@ public static function guessType(string $objectName): string
}
}

return $objectName;
return TypesEnum::Model->value;
}
}
26 changes: 13 additions & 13 deletions src/Helpers/TypesEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

enum TypesEnum: string
{
case MODEL = "model";
case JOB = "job";
case POLICY = "policy";
case REQUEST = "request";
case RESOURCE = "resource";
case SEEDER = "seeder";
case ENUM = "enum";
case EVENT = "event";
case LISTENER = "listener";
case MIDDLEWARE = "middleware";
case NOTIFICATION = "notification";
case PROVIDER = "provider";
case SERVICE = "service";
case Model = "model";
case Job = "job";
case Policy = "policy";
case Request = "request";
case Resource = "resource";
case Seeder = "seeder";
case Enum = "enum";
case Event = "event";
case Listener = "listener";
case Middleware = "middleware";
case Notification = "notification";
case Provider = "provider";
case Service = "service";
}
22 changes: 22 additions & 0 deletions src/Helpers/UserHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Helpers;

class UserHelper
{
public static function getBy(string $field, string $value): ?object
{
$userClass = RecognizeClassHelper::recognizeObjectClass("User");

return $userClass::query()->where($field, $value)->first();
}

public static function getByEmail(string $value): ?object
{
$userClass = RecognizeClassHelper::recognizeObjectClass("User");

return $userClass::query()->where("email", $value)->first();
}
}

0 comments on commit 63efae6

Please sign in to comment.