-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Context): Add and use ContextServiceContract instead of a implem…
…entation class + improve phpstan support - Add ContextServiceContractAssert - ContextServiceContract is a singleton BREAKING CHANGE: AbstractContext uses ContextServiceContract instead of a class.
- Loading branch information
Showing
23 changed files
with
710 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Context\Contracts; | ||
|
||
use Closure; | ||
use LaraStrict\Context\Contexts\AbstractContext; | ||
use LaraStrict\Context\Contexts\AbstractIsContext; | ||
use LaraStrict\Context\Values\BoolContextValue; | ||
|
||
/** | ||
* Shareable context values between logic that needs same data. Stored in memory and if context supports its it will | ||
* stores in cache repository (only usable with boot). | ||
*/ | ||
interface ContextServiceContract | ||
{ | ||
public function delete(AbstractContext $context): void; | ||
|
||
/** | ||
* Stores state to memory (and cache if supported). | ||
*/ | ||
public function set(AbstractContext $context, ContextValueContract $value): void; | ||
|
||
public function setWithoutCache(AbstractContext $context, ContextValueContract $value): void; | ||
|
||
/** | ||
* @template T of ContextValueContract | ||
* | ||
* @param Closure(mixed...):T $createState | ||
* @phpstan-param Closure(mixed,mixed,mixed,mixed,mixed,mixed):T $createState | ||
* | ||
* @return T | ||
*/ | ||
public function get(AbstractContext $context, Closure $createState): ContextValueContract; | ||
|
||
/** | ||
* Returns bool state of the context. | ||
* | ||
* @param Closure():bool $is | ||
* @phpstan-param Closure(mixed,mixed,mixed,mixed,mixed,mixed):bool $is | ||
*/ | ||
public function is(AbstractIsContext $context, Closure $is): BoolContextValue; | ||
|
||
public function getCacheKey(AbstractContext $context): string; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/Testing/Context/Contracts/ContextServiceContractAssert.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Context\Contracts; | ||
|
||
use Closure; | ||
use LaraStrict\Context\Contexts\AbstractContext; | ||
use LaraStrict\Context\Contexts\AbstractIsContext; | ||
use LaraStrict\Context\Contracts\ContextServiceContract; | ||
use LaraStrict\Context\Contracts\ContextValueContract; | ||
use LaraStrict\Context\Values\BoolContextValue; | ||
use LaraStrict\Testing\AbstractExpectationCallsMap; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class ContextServiceContractAssert extends AbstractExpectationCallsMap implements ContextServiceContract | ||
{ | ||
/** | ||
* @param array<ContextServiceContractDeleteExpectation> $delete | ||
* @param array<ContextServiceContractSetExpectation> $set | ||
* @param array<ContextServiceContractSetWithoutCacheExpectation> $setWithoutCache | ||
* @param array<ContextServiceContractGetExpectation> $get | ||
* @param array<ContextServiceContractIsExpectation> $is | ||
* @param array<ContextServiceContractGetCacheKeyExpectation> $getCacheKey | ||
*/ | ||
public function __construct( | ||
array $delete = [], | ||
array $set = [], | ||
array $setWithoutCache = [], | ||
array $get = [], | ||
array $is = [], | ||
array $getCacheKey = [], | ||
) { | ||
$this->setExpectations(ContextServiceContractDeleteExpectation::class, array_values(array_filter($delete))); | ||
$this->setExpectations(ContextServiceContractSetExpectation::class, array_values(array_filter($set))); | ||
$this->setExpectations( | ||
ContextServiceContractSetWithoutCacheExpectation::class, | ||
array_values(array_filter($setWithoutCache)) | ||
); | ||
$this->setExpectations(ContextServiceContractGetExpectation::class, array_values(array_filter($get))); | ||
$this->setExpectations(ContextServiceContractIsExpectation::class, array_values(array_filter($is))); | ||
$this->setExpectations( | ||
ContextServiceContractGetCacheKeyExpectation::class, | ||
array_values(array_filter($getCacheKey)) | ||
); | ||
} | ||
|
||
public function delete(AbstractContext $context): void | ||
{ | ||
$expectation = $this->getExpectation(ContextServiceContractDeleteExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->context, $context, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $context, $expectation); | ||
} | ||
} | ||
|
||
public function set(AbstractContext $context, ContextValueContract $value): void | ||
{ | ||
$expectation = $this->getExpectation(ContextServiceContractSetExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->context, $context, $message); | ||
Assert::assertEquals($expectation->value, $value, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $context, $value, $expectation); | ||
} | ||
} | ||
|
||
public function setWithoutCache(AbstractContext $context, ContextValueContract $value): void | ||
{ | ||
$expectation = $this->getExpectation(ContextServiceContractSetWithoutCacheExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->context, $context, $message); | ||
Assert::assertEquals($expectation->value, $value, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $context, $value, $expectation); | ||
} | ||
} | ||
|
||
public function get(AbstractContext $context, Closure $createState): ContextValueContract | ||
{ | ||
$expectation = $this->getExpectation(ContextServiceContractGetExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->context, $context, $message); | ||
Assert::assertEquals($expectation->createState, $createState, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $context, $createState, $expectation); | ||
} | ||
|
||
/** @phpstan-ignore-next-line */ | ||
return $expectation->return; | ||
} | ||
|
||
public function is(AbstractIsContext $context, Closure $is): BoolContextValue | ||
{ | ||
$expectation = $this->getExpectation(ContextServiceContractIsExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->context, $context, $message); | ||
Assert::assertEquals($expectation->is, $is, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $context, $is, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
public function getCacheKey(AbstractContext $context): string | ||
{ | ||
$expectation = $this->getExpectation(ContextServiceContractGetCacheKeyExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->context, $context, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $context, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
} |
Oops, something went wrong.