Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for setting legacy components #24

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions src/Container/AurynContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author Matthew Turland <me@matthewturland.com>
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class AurynContainer extends Injector implements ContainerInterface
class AurynContainer implements ContainerInterface
{
/**
* @var array<string, boolean>
Expand All @@ -35,25 +35,11 @@
protected $items = array();

/**
* Initializes the container instance.
* NOTE: To be removed in v1.0.0. It should use \Auryn\Injector::__construct.
*
* @param \Auryn\Injector|\Auryn\Reflector|null $data
* @param \Auryn\Injector $injector
*/
public function __construct($data = null)
public function __construct(Injector $injector)
{
$injector = $data instanceof Injector;

$instance = $this;

if ($injector) $instance = $data;

if (! $injector)
{
parent::__construct($data);
}

$this->injector = $instance;
$this->injector = $injector;
}

/**
Expand Down Expand Up @@ -127,8 +113,55 @@
*/
public function set($id, $concrete)
{
if (! $concrete || is_array($concrete))
{
$params = is_array($concrete) ? $concrete : array();

$this->items[$id] = $this->injector->make($id, $params);

return $this;
}

$this->items[$id] = $concrete;

// Tries to set the instance as shareable ---
try

Check warning on line 128 in src/Container/AurynContainer.php

View check run for this annotation

Codecov / codecov/patch

src/Container/AurynContainer.php#L128

Added line #L128 was not covered by tests
{
$this->injector->share($concrete);
}
// @codeCoverageIgnoreStart
catch (\Exception $error) {}
// @codeCoverageIgnoreEnd
// ------------------------------------------

// Also create an alias if available ---
try

Check warning on line 138 in src/Container/AurynContainer.php

View check run for this annotation

Codecov / codecov/patch

src/Container/AurynContainer.php#L138

Added line #L138 was not covered by tests
{
/** @var object $concrete */
$class = get_class($concrete);

$this->injector->alias($id, $class);
}
// @codeCoverageIgnoreStart
catch (\Exception $error) {}
// @codeCoverageIgnoreEnd
// -------------------------------------

return $this;
}

/**
* Calls methods from the \Auryn\Injector instance.
*
* @param string $method
* @param mixed[] $params
* @return mixed
*/
public function __call($method, $params)
{
/** @var callable $class */
$class = array($this->injector, $method);

return call_user_func_array($class, $params);
}
}
21 changes: 15 additions & 6 deletions src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Rougin\Slytherin;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Component\Collection;
use Rougin\Slytherin\Container\Container;
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\ConfigurationInterface;
use Rougin\Slytherin\System\Handler;

Expand All @@ -18,7 +19,7 @@
*/
class System
{
const CONTAINER = 'Psr\Container\ContainerInterface';
const CONTAINER = 'Rougin\Slytherin\Container\ContainerInterface';

const DISPATCHER = 'Rougin\Slytherin\Routing\DispatcherInterface';

Expand Down Expand Up @@ -49,18 +50,26 @@ class System
/**
* Initializes the application instance.
*
* @param \Rougin\Slytherin\Container\ContainerInterface|null $container
* @param \Rougin\Slytherin\Integration\Configuration|null $config
* @param mixed|null $container
* @param \Rougin\Slytherin\Integration\Configuration|null $config
*/
public function __construct(ContainerInterface $container = null, ConfigurationInterface $config = null)
public function __construct($container = null, ConfigurationInterface $config = null)
{
if (! $config) $config = new Configuration;

$this->config = $config;

if ($container instanceof Collection)
{
$container = $container->getContainer();
}

if (! $container) $container = new Container;

$this->container = $container;
if ($container instanceof ContainerInterface)
{
$this->container = $container;
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Application/AurynContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rougin\Slytherin\Application;

use Auryn\Injector;
use Rougin\Slytherin\Container\AurynContainer;
use Rougin\Slytherin\Http\Response;
use Rougin\Slytherin\Middleware\Dispatcher as Middleware;
Expand All @@ -28,7 +29,7 @@ protected function doSetUp()
$this->markTestSkipped('Auryn is not installed.');
}

$container = new AurynContainer;
$container = new AurynContainer(new Injector);

$router = $this->router();

Expand Down
11 changes: 11 additions & 0 deletions tests/Component/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rougin\Slytherin\IoC\Vanilla\Container;
use Rougin\Slytherin\Middleware\Interop;
use Rougin\Slytherin\Middleware\VanillaMiddleware;
use Rougin\Slytherin\System;
use Rougin\Slytherin\Template\TwigLoader;
use Rougin\Slytherin\Template\TwigRenderer;
use Rougin\Slytherin\Testcase;
Expand Down Expand Up @@ -229,4 +230,14 @@ public function testSetTemplateMethod()

$this->assertEquals($expected, $actual);
}

/**
* Tests the has() method.
*
* @return void
*/
public function testHasMethod()
{
$this->assertFalse($this->components->has(System::TEMPLATE));
}
}
2 changes: 0 additions & 2 deletions tests/Component/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

$items = array();

$items[] = 'Rougin\Slytherin\Fixture\Components\CollectionComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\DebuggerComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\DispatcherComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\HttpComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\SingleComponent';

$components = Collector::get($items);

Expand Down
134 changes: 134 additions & 0 deletions tests/Previous/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Rougin\Slytherin\Previous;

use Rougin\Slytherin\Application;
use Rougin\Slytherin\ComponentCollection;
use Rougin\Slytherin\Dispatching\Dispatcher;
use Rougin\Slytherin\ErrorHandler\Whoops;
use Rougin\Slytherin\Http\Uri;
use Rougin\Slytherin\IoC\Auryn;
use Rougin\Slytherin\Middleware\StratigilityMiddleware;
use Rougin\Slytherin\Template\RendererInterface;
use Rougin\Slytherin\Template\Twig;
use Twig_Environment;
use Twig_Loader_Filesystem;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Stratigility\MiddlewarePipe;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Builder
{
/**
* @var \Rougin\Slytherin\ComponentCollection
*/
protected $collection;

public function __construct()
{
$this->collection = new ComponentCollection;
}

public function make($method = null, $uri = null)
{
// Initialize the DependencyInjectorInterface ---
$this->setContainer();
// ----------------------------------------------

// Initialize the ErrorHandlerInterface ---
$this->setDebugger();
// ----------------------------------------

// Initialize the ServerRequestInterface and ResponseInterface ---
$this->setHttp($method, $uri);
// ---------------------------------------------------------------

// Initialize the routing dispatcher interface -----
$this->setRouting();
// -------------------------------------------------

// Initialize the middleware ---
$this->setMiddleware();
// -----------------------------

return new Application($this->collection);
}

protected function setContainer()
{
// Initialize the RendererInterface ----------------------
$views = (string) realpath(__DIR__ . '/Plates');
$loader = new Twig_Loader_Filesystem($views);
$twig = new Twig(new Twig_Environment($loader));
$renderer = 'Rougin\Slytherin\Template\RendererInterface';
// -------------------------------------------------------

$auryn = new Auryn(new \Auryn\Injector);

// Create an alias for the RendererInterface ---
$auryn->share($twig);
$auryn->alias($renderer, get_class($twig));
// ---------------------------------------------

$this->collection->setDependencyInjector($auryn);

return $this;
}

protected function setDebugger()
{
$whoops = new Whoops(new \Whoops\Run);

$this->collection->setErrorHandler($whoops);

return $this;
}

protected function setHttp($method = null, $uri = null)
{
$request = ServerRequestFactory::fromGlobals();

if ($method && $uri)
{
$uri = new Uri('http://localhost:8000' . $uri);

$request = $request->withUri($uri);

$request = $request->withMethod($method);
}

$response = new Response;

$this->collection->setHttp($request, $response);

return $this;
}

protected function setMiddleware()
{
$pipe = new MiddlewarePipe;

$middleware = new StratigilityMiddleware($pipe);

$this->collection->setMiddleware($middleware);

return $this;
}

protected function setRouting()
{
$router = require realpath(__DIR__ . '/Router.php');

$response = $this->collection->getHttpResponse();

$dispatcher = new Dispatcher($router, $response);

$this->collection->setDispatcher($dispatcher);

return $this;
}
}
Loading