From 2765d847c0f6bc6dc0b807439da0fb32f37fcc01 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Tue, 27 Sep 2022 13:26:09 +0200 Subject: [PATCH] feat(testing): Add TestingContainer and TestingApplicationRoutes --- rector.php | 5 +- src/Testing/Laravel/TestingApplication.php | 145 +-------------- .../Laravel/TestingApplicationRoutes.php | 33 ++++ src/Testing/Laravel/TestingContainer.php | 169 ++++++++++++++++++ 4 files changed, 211 insertions(+), 141 deletions(-) create mode 100644 src/Testing/Laravel/TestingApplicationRoutes.php create mode 100644 src/Testing/Laravel/TestingContainer.php diff --git a/rector.php b/rector.php index cd9551c7..0d259266 100644 --- a/rector.php +++ b/rector.php @@ -30,7 +30,10 @@ // SKIP laravel $config->skip([ - UnSpreadOperatorRector::class => [__DIR__ . '/src/Testing/Laravel/TestingApplication.php'], + UnSpreadOperatorRector::class => [ + __DIR__ . '/src/Testing/Laravel/TestingApplication.php', + __DIR__ . '/src/Testing/Laravel/TestingContainer.php', + ], VarConstantCommentRector::class, // We want to leave the relative constant path usage __DIR__ . '/tests/Feature/Testing/Commands/MakeExpectationCommand/*.php', diff --git a/src/Testing/Laravel/TestingApplication.php b/src/Testing/Laravel/TestingApplication.php index d45f8f11..5bd00d71 100644 --- a/src/Testing/Laravel/TestingApplication.php +++ b/src/Testing/Laravel/TestingApplication.php @@ -5,17 +5,15 @@ namespace LaraStrict\Testing\Laravel; use Closure; -use Illuminate\Container\ContextualBindingBuilder; -use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Foundation\Application; /** * A testing application class that helps you to not use mocks. */ -class TestingApplication implements Application +class TestingApplication extends TestingContainer implements Application { /** - * @param array $makeBindings A map of closures that will create. + * @param array $makeBindings A map of closures that will create. * Receives make $parameters and $abstract * string * @param Closure(array,string):(object|null)|null $makeAlwaysBinding If makeBindings has no entry, it will call @@ -27,9 +25,10 @@ public function __construct( public bool $runningInConsole = false, public bool $isDownForMaintenance = false, public MaintenanceMode $maintenanceMode = new MaintenanceMode(), - private array $makeBindings = [], - private Closure|null $makeAlwaysBinding = null, + array $makeBindings = [], + Closure|null $makeAlwaysBinding = null, ) { + parent::__construct($makeBindings, $makeAlwaysBinding); } public function version() @@ -167,138 +166,4 @@ public function terminating($callback) public function terminate() { } - - public function bound($abstract) - { - return false; - } - - public function alias($abstract, $alias) - { - } - - public function tag($abstracts, $tags) - { - } - - public function tagged($tag) - { - return []; - } - - public function bind($abstract, $concrete = null, $shared = false) - { - } - - public function bindIf($abstract, $concrete = null, $shared = false) - { - } - - public function singleton($abstract, $concrete = null) - { - } - - public function singletonIf($abstract, $concrete = null) - { - } - - public function scoped($abstract, $concrete = null) - { - } - - public function scopedIf($abstract, $concrete = null) - { - } - - public function extend($abstract, Closure $closure) - { - } - - public function instance($abstract, $instance) - { - } - - public function addContextualBinding($concrete, $abstract, $implementation) - { - } - - public function when($concrete) - { - return new ContextualBindingBuilder($this, $concrete); - } - - public function factory($abstract) - { - return static function () { - }; - } - - public function flush() - { - } - - public function make($abstract, array $parameters = []) - { - $make = $this->makeBindings[$abstract] ?? null; - - if ($make === null && $this->makeAlwaysBinding !== null) { - $make = $this->makeAlwaysBinding; - } - - if ($make === null) { - throw new BindingResolutionException('Binding not set ' . $abstract); - } - - $result = $make($parameters, $abstract); - - if ($result === null) { - throw new BindingResolutionException('Failed to resolve ' . $abstract); - } - - return $result; - } - - /** - * @param Closure(array):?object $make Closure that will receive make parameters and should return an object. - */ - public function makeReturns(string $abstract, Closure $make): void - { - $this->makeBindings[$abstract] = $make; - } - - public function makeAlwaysReturn(Closure $make): void - { - $this->makeAlwaysBinding = $make; - } - - public function call($callback, array $parameters = [], $defaultMethod = null) - { - } - - public function resolved($abstract) - { - return false; - } - - public function beforeResolving($abstract, Closure $callback = null) - { - } - - public function resolving($abstract, Closure $callback = null) - { - } - - public function afterResolving($abstract, Closure $callback = null) - { - } - - public function get(string $id) - { - return null; - } - - public function has(string $id): bool - { - return false; - } } diff --git a/src/Testing/Laravel/TestingApplicationRoutes.php b/src/Testing/Laravel/TestingApplicationRoutes.php new file mode 100644 index 00000000..20137d2a --- /dev/null +++ b/src/Testing/Laravel/TestingApplicationRoutes.php @@ -0,0 +1,33 @@ +routesAreCached = $routesAreCached; + + return $this; + } + + public function routesAreCached() + { + return $this->routesAreCached; + } + + public function getCachedRoutesPath() + { + return 'routes-path'; + } +} diff --git a/src/Testing/Laravel/TestingContainer.php b/src/Testing/Laravel/TestingContainer.php new file mode 100644 index 00000000..6d526431 --- /dev/null +++ b/src/Testing/Laravel/TestingContainer.php @@ -0,0 +1,169 @@ + $makeBindings A map of closures that will create. + * Receives make $parameters and $abstract + * string + * @param Closure(array,string):(object|null)|null $makeAlwaysBinding If makeBindings has no entry, it will call + * make on this closure. Receives make + * $parameters and $abstract string + */ + public function __construct( + private array $makeBindings = [], + private Closure|null $makeAlwaysBinding = null, + ) { + } + + public function bound($abstract) + { + return false; + } + + public function alias($abstract, $alias) + { + } + + public function tag($abstracts, $tags) + { + } + + public function tagged($tag) + { + return []; + } + + public function bind($abstract, $concrete = null, $shared = false) + { + } + + public function bindIf($abstract, $concrete = null, $shared = false) + { + } + + public function singleton($abstract, $concrete = null) + { + } + + public function singletonIf($abstract, $concrete = null) + { + } + + public function scoped($abstract, $concrete = null) + { + } + + public function scopedIf($abstract, $concrete = null) + { + } + + public function extend($abstract, Closure $closure) + { + } + + public function instance($abstract, $instance) + { + } + + public function addContextualBinding($concrete, $abstract, $implementation) + { + } + + public function when($concrete) + { + return new ContextualBindingBuilder($this, $concrete); + } + + public function factory($abstract) + { + return static function () { + }; + } + + public function flush() + { + } + + public function make($abstract, array $parameters = []) + { + $make = $this->makeBindings[$abstract] ?? null; + + if (is_object($make)) { + return $make; + } + + if ($make === null && $this->makeAlwaysBinding !== null) { + $make = $this->makeAlwaysBinding; + } + + if ($make === null) { + throw new BindingResolutionException('Binding not set ' . $abstract); + } + + $result = $make($parameters, $abstract); + + if ($result === null) { + throw new BindingResolutionException('Failed to resolve ' . $abstract); + } + + return $result; + } + + /** + * @param Closure(array):?object $make Closure that will receive make parameters and should return an object. + */ + public function makeReturns(string $abstract, Closure $make): void + { + $this->makeBindings[$abstract] = $make; + } + + public function makeAlwaysReturn(Closure $make): void + { + $this->makeAlwaysBinding = $make; + } + + public function call($callback, array $parameters = [], $defaultMethod = null) + { + } + + public function resolved($abstract) + { + return false; + } + + public function beforeResolving($abstract, Closure $callback = null) + { + } + + public function resolving($abstract, Closure $callback = null) + { + } + + public function afterResolving($abstract, Closure $callback = null) + { + } + + public function get(string $id) + { + return null; + } + + public function has(string $id): bool + { + return false; + } +}