1

I'm writing tests for a controller in a plugin (Assets plugin).

This is the controller:

namespace Assets\Controller;

use Cake\Controller\Controller;

class AssetsController extends Controller
{
    public function asset($filename, $type)
    {
        $this->response->type($type);
        $this->response->file(ASSETS . DS . $filename);

        return $this->response;
    }
}

As you can see, it only sends an asset files.

This is the route:

Router::plugin('Assets', ['path' => '/assets'], function ($routes) {
    $routes->connect(
        '/:type/:filename',
        ['controller' => 'Assets', 'action' => 'asset'],
        [
            'type' => '(css|js)',
            'filename' => '[a-z0-9]+\.(css|js)',
            'pass' => ['filename', 'type'],
        ]
    );
});

And this is the test class:

namespace Assets\Test\TestCase\Controller;

use Assets\Utility\AssetsCreator;
use Cake\TestSuite\IntegrationTestCase;

class AssetsControllerTest extends IntegrationTestCase
{    
    public function testAsset()
    {
        //This is the filename
        $filename = sprintf('%s.%s', AssetsCreator::css('test'), 'css');

        $this->get(sprintf('/assets/css/%s', $filename));

        $this->assertResponseOk();
    }
}

Running the test, however, this exception is generated (full test here):

1) Assets\Test\TestCase\Controller\AssetsControllerTest::testAsset
Cake\Routing\Exception\MissingControllerException: Controller class  could not be found. in /home/mirko/Libs/Plugins/Assets/vendor/cakephp/cakephp/src/Http/ControllerFactory.php:91

I do not think is a problem of broken, because the same exception is generated by doing so:

$url = \Cake\Routing\Router::url([
    'controller' => 'Assets',
    'action' => 'asset',
    'plugin' => 'Assets',
    'type' => 'css',
    'filename' => $filename,
]);

$this->get($url);

Where am I doing wrong? Thanks.

3
  • what version of cake 3 is this? Commented Sep 20, 2016 at 2:12
  • @systematical, version 3.3.3 Commented Sep 20, 2016 at 9:06
  • It seems it can't find Cake\Controller\Controller... why? Commented Sep 20, 2016 at 17:08

1 Answer 1

3

Solved! On my tests' bootstrap, I missed:

DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');

Now it works.

Sign up to request clarification or add additional context in comments.

2 Comments

For CakePHP >=3.5, Dispatcher Filters are deprecated instead you should add Routing Middleware into your Application.php file.
Yes but I've added this comment because I had same problem and I landed on this solution which worked but was old. So if in case anybody has same problem this comment might help them! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.