Skip to content

Commit

Permalink
feat(Testing): Add ability to assert AppConfig (use AppConfigContract…
Browse files Browse the repository at this point in the history
… for implementation)
  • Loading branch information
pionl committed Jun 21, 2023
1 parent b0874e8 commit 8ae2d7a
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 5 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"lint:upgrade": "vendor/bin/rector process",
"lint": "composer lint:upgrade && composer lint:fix && composer lint:stan",
"test": "./vendor/bin/phpunit",
"test:stubs": "STUBS_GENERATE=true ./vendor/bin/phpunit",
"test:coverage": "./vendor/bin/phpunit --coverage-text"
},
"autoload": {
Expand Down
16 changes: 16 additions & 0 deletions src/Config/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Config;

use LaraStrict\Config\Contracts\AppConfigContract;
use LaraStrict\Config\Laravel\AppConfig;
use LaraStrict\Providers\AbstractServiceProvider;

class ConfigServiceProvider extends AbstractServiceProvider
{
public array $bindings = [
AppConfigContract::class => AppConfig::class,
];
}
27 changes: 27 additions & 0 deletions src/Config/Contracts/AppConfigContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Config\Contracts;

use LaraStrict\Enums\EnvironmentType;

interface AppConfigContract
{
/**
* Version from an app (Larastrict projects uses app.version).
*/
public function getVersion(): string;

public function getKey(): string;

public function getUrl(): string;

public function getAssetUrl(): ?string;

public function getName(): string;

public function isInDebugMode(): bool;

public function getEnvironment(): EnvironmentType|string;
}
5 changes: 3 additions & 2 deletions src/Config/Laravel/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace LaraStrict\Config\Laravel;

use LaraStrict\Config\AbstractConfig;
use LaraStrict\Config\Contracts\AppConfigContract;
use LaraStrict\Enums\EnvironmentType;

class AppConfig extends AbstractConfig
class AppConfig extends AbstractConfig implements AppConfigContract
{
final public const KeyEnv = 'env';

Expand Down Expand Up @@ -81,6 +82,6 @@ public function getEnvironment(): EnvironmentType|string

protected function getConfigFileName(): string
{
return 'app';
return self::ConfigName;
}
}
2 changes: 2 additions & 0 deletions src/Core/LaraStrictServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LaraStrict\Core;

use LaraStrict\Cache\CacheServiceProvider;
use LaraStrict\Config\ConfigServiceProvider;
use LaraStrict\Console\Contracts\ScheduleServiceContract;
use LaraStrict\Console\Services\ScheduleService;
use LaraStrict\Context\ContextServiceProvider;
Expand All @@ -31,6 +32,7 @@ public function register(): void
$this->app->bind(RunAppServiceProviderPipesActionContract::class, RunAppServiceProviderPipesAction::class);

// Register our service providers
$this->app->register(ConfigServiceProvider::class);
$this->app->register(ContextServiceProvider::class);
$this->app->register(CacheServiceProvider::class);
$this->app->register(DatabaseServiceProvider::class);
Expand Down
128 changes: 128 additions & 0 deletions src/Testing/Config/Contracts/AppConfigContractAssert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use LaraStrict\Config\Contracts\AppConfigContract;
use LaraStrict\Enums\EnvironmentType;
use LaraStrict\Testing\AbstractExpectationCallsMap;

class AppConfigContractAssert extends AbstractExpectationCallsMap implements AppConfigContract
{
/**
* @param array<AppConfigContractGetVersionExpectation|null> $getVersion
* @param array<AppConfigContractGetKeyExpectation|null> $getKey
* @param array<AppConfigContractGetUrlExpectation|null> $getUrl
* @param array<AppConfigContractGetAssetUrlExpectation|null> $getAssetUrl
* @param array<AppConfigContractGetNameExpectation|null> $getName
* @param array<AppConfigContractIsInDebugModeExpectation|null> $isInDebugMode
* @param array<AppConfigContractGetEnvironmentExpectation|null> $getEnvironment
*/
public function __construct(
array $getVersion = [],
array $getKey = [],
array $getUrl = [],
array $getAssetUrl = [],
array $getName = [],
array $isInDebugMode = [],
array $getEnvironment = [],
) {
$this->setExpectations(AppConfigContractGetVersionExpectation::class, array_values(array_filter($getVersion)));
$this->setExpectations(AppConfigContractGetKeyExpectation::class, array_values(array_filter($getKey)));
$this->setExpectations(AppConfigContractGetUrlExpectation::class, array_values(array_filter($getUrl)));
$this->setExpectations(
AppConfigContractGetAssetUrlExpectation::class,
array_values(array_filter($getAssetUrl))
);
$this->setExpectations(AppConfigContractGetNameExpectation::class, array_values(array_filter($getName)));
$this->setExpectations(
AppConfigContractIsInDebugModeExpectation::class,
array_values(array_filter($isInDebugMode))
);
$this->setExpectations(
AppConfigContractGetEnvironmentExpectation::class,
array_values(array_filter($getEnvironment))
);
}

/**
* Version from an app (Larastrict projects uses app.version).
*/
public function getVersion(): string
{
$expectation = $this->getExpectation(AppConfigContractGetVersionExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}

public function getKey(): string
{
$expectation = $this->getExpectation(AppConfigContractGetKeyExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}

public function getUrl(): string
{
$expectation = $this->getExpectation(AppConfigContractGetUrlExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}

public function getAssetUrl(): ?string
{
$expectation = $this->getExpectation(AppConfigContractGetAssetUrlExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}

public function getName(): string
{
$expectation = $this->getExpectation(AppConfigContractGetNameExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}

public function isInDebugMode(): bool
{
$expectation = $this->getExpectation(AppConfigContractIsInDebugModeExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}

public function getEnvironment(): EnvironmentType|string
{
$expectation = $this->getExpectation(AppConfigContractGetEnvironmentExpectation::class);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
}

return $expectation->return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;

final class AppConfigContractGetAssetUrlExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly ?string $return,
public readonly ?Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;
use LaraStrict\Enums\EnvironmentType;

final class AppConfigContractGetEnvironmentExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly EnvironmentType|string $return,
public readonly ?Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;

final class AppConfigContractGetKeyExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly string $return,
public readonly ?Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;

final class AppConfigContractGetNameExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly string $return,
public readonly ?Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;

final class AppConfigContractGetUrlExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly string $return,
public readonly ?Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;

final class AppConfigContractGetVersionExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly string $return,
public readonly ?Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Config\Contracts;

use Closure;

final class AppConfigContractIsInDebugModeExpectation
{
/**
* @param Closure(self):void|null $hook
*/
public function __construct(
public readonly bool $return,
public readonly ?Closure $hook = null,
) {
}
}
6 changes: 3 additions & 3 deletions src/Testing/TestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace LaraStrict\Testing;

use Illuminate\Support\ServiceProvider;
use LaraStrict\Config\Laravel\AppConfig;
use LaraStrict\Config\Contracts\AppConfigContract;
use LaraStrict\Enums\EnvironmentType;
use LaraStrict\Testing\Actions\GetBasePathForStubsAction;
use LaraStrict\Testing\Actions\GetNamespaceForStubsAction;
Expand All @@ -28,8 +28,8 @@ public function register(): void
return;
}

/** @var AppConfig $config */
$config = $this->app->make(AppConfig::class);
/** @var AppConfigContract $config */
$config = $this->app->make(AppConfigContract::class);

$environment = $config->getEnvironment();
if (in_array($environment, [EnvironmentType::Testing, EnvironmentType::Local], false) === false) {
Expand Down

0 comments on commit 8ae2d7a

Please sign in to comment.