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

4.x Remove Pimple #2587

Closed
wants to merge 7 commits into from
Closed
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
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"phpunit/phpunit": "^7.0",
"phpspec/prophecy": "^1.0",
"phpstan/phpstan": "^0.10.3",
"pimple/pimple": "^3.2",
"squizlabs/php_codesniffer": "^3.3.2"
},
"autoload": {
Expand Down
93 changes: 39 additions & 54 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
*/
namespace Slim\Tests;

use Pimple\Container as Pimple;
use Pimple\Psr11\Container as Psr11Container;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
use Slim\App;
use Slim\CallableResolver;
use Slim\Error\Renderers\HtmlErrorRenderer;
Expand All @@ -23,6 +20,7 @@
use Slim\Route;
use Slim\Router;
use Slim\Tests\Mocks\MockAction;
use Slim\Tests\Mocks\MockContainer;
use Slim\Tests\Mocks\MockMiddleware;
use Slim\Tests\Mocks\MockMiddlewareWithoutInterface;

Expand All @@ -39,8 +37,7 @@ public static function setupBeforeClass()

public function testGetContainer()
{
$pimple = new Pimple();
$container = new Psr11Container($pimple);
$container = new MockContainer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have a MockContainer object when we could be using PHPUnit's createMock() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have ArrayAccess implemented on the class unless you want to force usage of the ContainerInterface::set() method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have ArrayAccess implemented on the class

Why? Slim doesn't set anything into the container anymore does it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misread your question. We can mock a container for that specific test sure. We have a MockContainer though so why not use it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have a MockContainer though? We shouldn't need it as we should be able to use PHPUnit's Mocking functionality for every situation where Slim's classes use a container. If we can't then we need to look at that particular Slim code.

$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory, $container);

Expand All @@ -49,8 +46,7 @@ public function testGetContainer()

public function testGetCallableResolver()
{
$pimple = new Pimple();
$container = new Psr11Container($pimple);
$container = new MockContainer();
$callableResolver = new CallableResolver($container);
$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory, $container);
Expand All @@ -60,8 +56,7 @@ public function testGetCallableResolver()

public function testGetRouter()
{
$pimple = new Pimple();
$container = new Psr11Container($pimple);
$container = new MockContainer();
$callableResolver = new CallableResolver($container);
$responseFactory = $this->getResponseFactory();
$router = new Router($responseFactory, $callableResolver, $container);
Expand Down Expand Up @@ -900,10 +895,11 @@ public function testAddMiddleware()
return $responseFactory->createResponse();
});

$request = $this->createServerRequest('/');
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
return $response;
});

$request = $this->createServerRequest('/');
$app->handle($request);

$this->assertSame($called, 1);
Expand All @@ -913,19 +909,19 @@ public function testAddMiddlewareUsingDeferredResolution()
{
$responseFactory = $this->getResponseFactory();

$pimple = new Pimple();
$pimple->offsetSet(MockMiddleware::class, new MockMiddleware($responseFactory));
$container = new Psr11Container($pimple);
$container = new MockContainer();
$container[MockMiddleware::class] = new MockMiddleware($responseFactory);

$app = new App($responseFactory, $container);
$app->add(MockMiddleware::class);

$request = $this->createServerRequest('/');
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
return $response;
});

$request = $this->createServerRequest('/');
$response = $app->handle($request);

$this->assertSame('Hello World', (string) $response->getBody());
}

Expand Down Expand Up @@ -955,8 +951,9 @@ public function testAddMiddlewareOnRoute()
$appendToOutput = function (string $value) use (&$output) {
$output .= $value;
};
$request = $this->createServerRequest('/');
$request = $request->withAttribute('appendToOutput', $appendToOutput);
$request = $this
->createServerRequest('/')
->withAttribute('appendToOutput', $appendToOutput);

$app->run($request);

Expand Down Expand Up @@ -991,8 +988,9 @@ public function testAddMiddlewareOnRouteGroup()
$appendToOutput = function (string $value) use (&$output) {
$output .= $value;
};
$request = $this->createServerRequest('/foo/');
$request = $request->withAttribute('appendToOutput', $appendToOutput);
$request = $this
->createServerRequest('/foo/')
->withAttribute('appendToOutput', $appendToOutput);

$app->run($request);

Expand Down Expand Up @@ -1025,13 +1023,13 @@ public function testAddMiddlewareOnTwoRouteGroup()
return $response;
});

// Prepare request object
$output = '';
$appendToOutput = function (string $value) use (&$output) {
$output .= $value;
};
$request = $this->createServerRequest('/foo/baz/');
$request = $request->withAttribute('appendToOutput', $appendToOutput);
$request = $this
->createServerRequest('/foo/baz/')
->withAttribute('appendToOutput', $appendToOutput);

$app->run($request);

Expand Down Expand Up @@ -1074,8 +1072,9 @@ public function testAddMiddlewareOnRouteAndOnTwoRouteGroup()
$appendToOutput = function (string $value) use (&$output) {
$output .= $value;
};
$request = $this->createServerRequest('/foo/baz/');
$request = $request->withAttribute('appendToOutput', $appendToOutput);
$request = $this
->createServerRequest('/foo/baz/')
->withAttribute('appendToOutput', $appendToOutput);

$app->run($request);

Expand Down Expand Up @@ -1247,18 +1246,15 @@ public function testInvokeWithCallableRegisteredInContainer()
{
$request = $this->createServerRequest('/foo');
$response = $this->createResponse();
$response->getBody()->write('Hello');

$mock = $this->getMockBuilder('StdClass')->setMethods(['bar'])->getMock();
$mock
->method('bar')
->willReturn($response);

$pimple = new Pimple();
$pimple['foo'] = function () use ($mock, $response) {
$response->getBody()->write('Hello');
$mock
->method('bar')
->willReturn($response);
return $mock;
};
$container = new Psr11Container($pimple);
$container = new MockContainer();
$container['foo'] = $mock;

$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory, $container);
Expand All @@ -1275,39 +1271,29 @@ public function testInvokeWithCallableRegisteredInContainer()
*/
public function testInvokeWithNonExistentMethodOnCallableRegisteredInContainer()
{
$request = $this->createServerRequest('/foo');

$mock = $this->getMockBuilder('StdClass')->getMock();

$pimple = new Pimple();
$pimple['foo'] = function () use ($mock) {
return $mock;
};
$container = new Psr11Container($pimple);
$container = new MockContainer();
$container['foo'] = $mock;

$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory, $container);
$app->get('/foo', 'foo:bar');

$request = $this->createServerRequest('/foo');
$app->handle($request);
}

public function testInvokeWithCallableInContainerViaMagicMethod()
{
$request = $this->createServerRequest('/foo');

$mock = new MockAction();

$pimple = new Pimple();
$pimple['foo'] = function () use ($mock) {
return $mock;
};
$container = new Psr11Container($pimple);
$container = new MockContainer();
$container['foo'] = $mock;

$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory, $container);
$app->get('/foo', 'foo:bar');

$request = $this->createServerRequest('/foo');
$response = $app->handle($request);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand Down Expand Up @@ -1343,7 +1329,9 @@ public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArguments()
return $response;
});

$request = $this->createServerRequest('/foo/rob')->withAttribute("one", 1);
$request = $this
->createServerRequest('/foo/rob')
->withAttribute("one", 1);
$response = $app->handle($request);

$this->assertEquals('1rob', (string)$response->getBody());
Expand Down Expand Up @@ -1432,11 +1420,8 @@ public function testCanBeReExecutedRecursivelyDuringDispatch()
public function testContainerSetToRoute()
{
$mock = new MockAction();
$pimple = new Pimple();
$pimple['foo'] = function () use ($mock) {
return $mock;
};
$container = new Psr11Container($pimple);
$container = new MockContainer();
$container['foo'] = $mock;

$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory, $container);
Expand Down
24 changes: 7 additions & 17 deletions tests/CallableResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
*/
namespace Slim\Tests;

use Pimple\Container as Pimple;
use Pimple\Psr11\Container;
use Psr\Container\ContainerInterface;
use Slim\CallableResolver;
use Slim\Tests\Mocks\CallableTest;
use Slim\Tests\Mocks\InvokableTest;
use Slim\Tests\Mocks\MockContainer;
use Slim\Tests\Mocks\RequestHandlerTest;

class CallableResolverTest extends TestCase
Expand All @@ -23,19 +22,13 @@ class CallableResolverTest extends TestCase
*/
private $container;

/**
* @var Pimple
*/
private $pimple;

public function setUp()
{
CallableTest::$CalledCount = 0;
InvokableTest::$CalledCount = 0;
RequestHandlerTest::$CalledCount = 0;

$this->pimple = new Pimple;
$this->container = new Container($this->pimple);
$this->container = new MockContainer();
}

public function testClosure()
Expand All @@ -57,7 +50,7 @@ function testCallable()
}
$resolver = new CallableResolver(); // No container injected
$callable = $resolver->resolve(__NAMESPACE__ . '\testCallable');

$this->assertEquals(true, $callable());
}

Expand Down Expand Up @@ -90,7 +83,7 @@ public function testSlimCallableContainer()

public function testContainer()
{
$this->pimple['callable_service'] = new CallableTest();
$this->container['callable_service'] = new CallableTest();
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('callable_service:toCall');
$callable();
Expand All @@ -100,9 +93,7 @@ public function testContainer()

public function testResolutionToAnInvokableClassInContainer()
{
$this->pimple['an_invokable'] = function ($c) {
return new InvokableTest();
};
$this->container['an_invokable'] = new InvokableTest();
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('an_invokable');
$callable();
Expand Down Expand Up @@ -142,7 +133,7 @@ public function testObjPsrRequestHandlerClass()

public function testObjPsrRequestHandlerClassInContainer()
{
$this->pimple['a_requesthandler'] = new RequestHandlerTest();
$this->container['a_requesthandler'] = new RequestHandlerTest();
$request = $this->createServerRequest('/', 'GET');
$resolver = new CallableResolver($this->container);
$callable = $resolver->resolve('a_requesthandler');
Expand All @@ -153,7 +144,6 @@ public function testObjPsrRequestHandlerClassInContainer()

public function testResolutionToAPsrRequestHandlerClassWithCustomMethod()
{
$request = $this->createServerRequest('/', 'GET');
$resolver = new CallableResolver(); // No container injected
$callable = $resolver->resolve(RequestHandlerTest::class . ':custom');
$this->assertInternalType('array', $callable);
Expand All @@ -166,7 +156,7 @@ public function testResolutionToAPsrRequestHandlerClassWithCustomMethod()
*/
public function testMethodNotFoundThrowException()
{
$this->pimple['callable_service'] = new CallableTest();
$this->container['callable_service'] = new CallableTest();
$resolver = new CallableResolver($this->container);
$resolver->resolve('callable_service:notFound');
}
Expand Down
18 changes: 7 additions & 11 deletions tests/DeferredCallableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
*/
namespace Slim\Tests;

use Pimple\Container as Pimple;
use Pimple\Psr11\Container;
use Slim\CallableResolver;
use Slim\DeferredCallable;
use Slim\Tests\Mocks\CallableTest;
use Slim\Tests\Mocks\MockContainer;

class DeferredCallableTest extends TestCase
{
public function testItResolvesCallable()
{
$pimple = new Pimple();
$pimple['CallableTest'] = new CallableTest;
$container = new Container($pimple);
$container = new MockContainer();
$container['CallableTest'] = new CallableTest;
$resolver = new CallableResolver($container);

$deferred = new DeferredCallable('CallableTest:toCall', $resolver);
Expand All @@ -31,15 +29,14 @@ public function testItResolvesCallable()

public function testItBindsClosuresToContainer()
{
$container = new MockContainer();
$resolver = new CallableResolver($container);

$assertCalled = $this->getMockBuilder('StdClass')->setMethods(['foo'])->getMock();
$assertCalled
->expects($this->once())
->method('foo');

$pimple = new Pimple();
$container = new Container($pimple);
$resolver = new CallableResolver($container);

$test = $this;
$closure = function () use ($container, $test, $assertCalled) {
$assertCalled->foo();
Expand All @@ -52,8 +49,7 @@ public function testItBindsClosuresToContainer()

public function testItReturnsInvokedCallableResponse()
{
$pimple = new Pimple();
$container = new Container($pimple);
$container = new MockContainer();
$resolver = new CallableResolver($container);

$test = $this;
Expand Down
Loading