Skip to content

Commit

Permalink
unmockify the testsuite for Routing & View
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Sep 2, 2024
1 parent 5c0d095 commit 4adde21
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 120 deletions.
70 changes: 48 additions & 22 deletions tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace Cake\Test\TestCase\Routing\Middleware;

use Cake\Core\Configure;
use Cake\Core\HttpApplicationInterface;
use Cake\Http\ServerRequestFactory;
use Cake\Routing\Exception\MissingRouteException;
use Cake\Routing\Middleware\RoutingMiddleware;
Expand All @@ -26,8 +25,11 @@
use Cake\Routing\Router;
use Cake\Routing\RoutingApplicationInterface;
use Cake\TestSuite\TestCase;
use Closure;
use Laminas\Diactoros\Response;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TestApp\Application;
use TestApp\Http\TestRequestHandler;
use TestApp\Middleware\DumbMiddleware;
Expand Down Expand Up @@ -194,15 +196,28 @@ public function testRoutesHookCallsPluginHook(): void
Router::reload();

$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/app/articles']);
$app = $this->getMockBuilder(Application::class)
->onlyMethods(['pluginRoutes'])
->setConstructorArgs([CONFIG])
->getMock();
$app->expects($this->once())
->method('pluginRoutes')
->with($this->isInstanceOf(RouteBuilder::class));
$app = new class (CONFIG) extends Application {
public function pluginRoutes(RouteBuilder $routes): RouteBuilder
{
$routes->connect('/app/articles', ['controller' => 'Articles', 'action' => 'index']);

return $routes;
}
};
$middleware = new RoutingMiddleware($app);
$middleware->process($request, new TestRequestHandler());
$middleware->process($request, new TestRequestHandler(function ($req) {
$expected = [
'controller' => 'Articles',
'action' => 'index',
'plugin' => null,
'pass' => [],
'_ext' => null,
'_matchedRoute' => '/app/articles',
];
$this->assertEquals($expected, $req->getAttribute('params'));

return new Response();
}));
}

/**
Expand Down Expand Up @@ -446,8 +461,11 @@ public static function scopedMiddlewareUrlProvider(): array
*/
public function testAppWithoutContainerApplicationInterface(): void
{
/** @var \Cake\Core\HttpApplicationInterface|\PHPUnit\Framework\MockObject\MockObject $app */
$app = $this->createMock(RoutingApplicationInterface::class);
$app = new class implements RoutingApplicationInterface {
public function routes(RouteBuilder $routes): void
{
}
};
$this->builder->scope('/', function (RouteBuilder $routes): void {
$routes->connect('/testpath', ['controller' => 'Articles', 'action' => 'index']);
});
Expand Down Expand Up @@ -483,19 +501,27 @@ public function testAppWithContainerApplicationInterface(): void
*
* @param callable|null $handleCallback Callback for "handle" method.
*/
protected function app($handleCallback = null): HttpApplicationInterface
protected function app(?callable $handleCallback = null): Application
{
$mock = $this->createMock(Application::class);
$mock->method('routes')
->willReturnCallback(function (RouteBuilder $routes) {
return $routes;
});
$app = new class (CONFIG) extends Application {
public ?Closure $handleCallback;

public function routes(RouteBuilder $routes): void
{
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
if ($this->handleCallback) {
return ($this->handleCallback)($request);
}

return parent::handle($request); // TODO: Change the autogenerated stub
}
};

if ($handleCallback) {
$mock->method('handle')
->willReturnCallback($handleCallback);
}
$app->handleCallback = $handleCallback;

return $mock;
return $app;
}
}
16 changes: 6 additions & 10 deletions tests/TestCase/Routing/Route/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,16 +1038,12 @@ public function testQueryStringGeneration(): void
*/
public function testParseRequestDelegates(): void
{
/** @var \Cake\Routing\Route\Route|\PHPUnit\Framework\MockObject\MockObject $route */
$route = $this->getMockBuilder(Route::class)
->onlyMethods(['parse'])
->setConstructorArgs(['/forward', ['controller' => 'Articles', 'action' => 'index']])
->getMock();

$route->expects($this->once())
->method('parse')
->with('/forward', 'GET')
->willReturn(['works!']);
$route = new class ('/forward', ['controller' => 'Articles', 'action' => 'index']) extends Route {
public function parse(string $url, string $method): ?array
{
return ['works!'];
}
};

$request = new ServerRequest([
'environment' => [
Expand Down
17 changes: 7 additions & 10 deletions tests/TestCase/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2989,20 +2989,17 @@ public function testGetRequest(): void
*/
public function testUrlFullUrlReturnFromRoute(): void
{
$url = 'http://example.com/posts/view/1';

$route = $this->getMockBuilder(Route::class)
->onlyMethods(['match'])
->setConstructorArgs(['/{controller}/{action}/*'])
->getMock();
$route->expects($this->any())
->method('match')
->willReturn($url);
$route = new class ('/{controller}/{action}/*') extends Route {
public function match(array $url, array $context = []): ?string
{
return 'http://example.com/posts/view/1';
}
};

Router::createRouteBuilder('/')->connect($route);

$result = Router::url(['controller' => 'Posts', 'action' => 'view', 1]);
$this->assertSame($url, $result);
$this->assertSame('http://example.com/posts/view/1', $result);
}

/**
Expand Down
133 changes: 95 additions & 38 deletions tests/TestCase/TestSuite/Fixture/FixtureHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace Cake\Test\TestCase\TestSuite;

use Cake\Core\Exception\CakeException;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionInterface;
use Cake\Datasource\ConnectionManager;
use Cake\Test\Fixture\ArticlesFixture;
use Cake\TestSuite\Fixture\FixtureHelper;
Expand Down Expand Up @@ -106,15 +108,26 @@ public function testLoadDulicateFixtures(): void
*/
public function testPerConnection(): void
{
$fixture1 = $this->createMock(TestFixture::class);
$fixture1->expects($this->once())
->method('connection')
->willReturn('test1');
$fixture1 = new class extends TestFixture {
public function connection(): string
{
return 'test1';
}

$fixture2 = $this->createMock(TestFixture::class);
$fixture2->expects($this->once())
->method('connection')
->willReturn('test2');
protected function _schemaFromReflection(): void
{
}
};
$fixture2 = new class extends TestFixture {
public function connection(): string
{
return 'test2';
}

protected function _schemaFromReflection(): void
{
}
};

ConnectionManager::alias('test', 'test1');
ConnectionManager::alias('test', 'test2');
Expand Down Expand Up @@ -152,23 +165,45 @@ public function testInsertFixtures(): void
*/
public function testInsertFixturesException(): void
{
$fixture = $this->getMockBuilder(TestFixture::class)->getMock();
$fixture->expects($this->once())
->method('connection')
->willReturn('test');
$fixture->expects($this->once())
->method('insert')
->will($this->throwException(new PDOException('Missing key')));

$helper = $this->getMockBuilder(FixtureHelper::class)
->onlyMethods(['sortByConstraint'])
->getMock();
$helper->expects($this->any())
->method('sortByConstraint')
->willReturn([$fixture]);
$fixture = new class extends TestFixture {
public function connection(): string
{
return 'test';
}

protected function _schemaFromReflection(): void
{
}

public function insert(ConnectionInterface $connection): bool
{
throw new PDOException('Missing key');
}
};

$helper = new class extends FixtureHelper {
public function sortByConstraint(Connection $connection, array $fixtures): array
{
return [new class extends TestFixture {
public function connection(): string
{
return 'test';
}

protected function _schemaFromReflection(): void
{
}

public function insert(ConnectionInterface $connection): bool
{
throw new PDOException('Missing key');
}
}];
}
};

$this->expectException(CakeException::class);
$this->expectExceptionMessage('Unable to insert rows for table ``');
$this->expectExceptionMessage('Unable to insert rows for table `');
$helper->insert([$fixture]);
}

Expand Down Expand Up @@ -197,23 +232,45 @@ public function testTruncateFixtures(): void
*/
public function testTruncateFixturesException(): void
{
$fixture = $this->getMockBuilder(TestFixture::class)->getMock();
$fixture->expects($this->once())
->method('connection')
->willReturn('test');
$fixture->expects($this->once())
->method('truncate')
->will($this->throwException(new PDOException('Missing key')));

$helper = $this->getMockBuilder(FixtureHelper::class)
->onlyMethods(['sortByConstraint'])
->getMock();
$helper->expects($this->any())
->method('sortByConstraint')
->willReturn([$fixture]);
$fixture = new class extends TestFixture {
public function connection(): string
{
return 'test';
}

protected function _schemaFromReflection(): void
{
}

public function truncate(ConnectionInterface $connection): bool
{
throw new PDOException('Missing key');
}
};

$helper = new class extends FixtureHelper {
public function sortByConstraint(Connection $connection, array $fixtures): array
{
return [new class extends TestFixture {
public function connection(): string
{
return 'test';
}

protected function _schemaFromReflection(): void
{
}

public function truncate(ConnectionInterface $connection): bool
{
throw new PDOException('Missing key');
}
}];
}
};

$this->expectException(CakeException::class);
$this->expectExceptionMessage('Unable to truncate table ``');
$this->expectExceptionMessage('Unable to truncate table `');
$helper->truncate([$fixture]);
}
}
Loading

0 comments on commit 4adde21

Please sign in to comment.