# |
May 18th 2021, 20:21 |
slackebot |
$this->mockService(FooMiddleware::class, function () { $fooMiddlewareMock = $this ->getMockBuilder(FooMiddleware::class) ->onlyMethods(['process']) ->getMock(); // ... return $fooMiddlewareMock; }); $this->get('/foo/bar'); // ... }``` |
# |
May 18th 2021, 20:21 |
slackebot |
$this->getContainer()->get(FooMiddleware::class); $routes->registerMiddleware('foo', $fooMiddleware); $routes->connect('/foo/bar', function (RouteBuilder $routes) { $routes->applyMiddleware('foo'); }); }``` ```public function testFooBar() { // this method keeps a list of mocks/objects, which is being used // to modify the container when the app dispatches the event |
# |
May 18th 2021, 20:21 |
ndm |
No problem... to close off, a quick and dirty example of what it probably somewhat would look like in 4.2, just to give you an idea: ```// event is being dispatched after this method has been invoked public function services(ContainerInterface $container): void { $container->add(FooMiddleware::class); } public function routes(RouteBuilder $routes): void { parent::routes($routes); $fooMiddleware = |
# |
May 18th 2021, 20:06 |
caseyw |
@ndm Sweet, noted, and I'll check that out in more detail. Thank you for your time. |
# |
May 18th 2021, 19:55 |
ndm |
Then you'll have to come up with something conceptually similar on your own I'm afraid. What the core does with the DIC is pretty simple, the container is built/configured in the `Application` class, and an event is being dispatched afterwards (`Application.buildContainer`), which the test environment listens to, and then modifies the container with the configured mocks. |
# |
May 18th 2021, 19:37 |
caseyw |
@ndm Thanks for the response. This project is on 3.9 currently. |
# |
May 18th 2021, 19:35 |
ndm |
You would need to obtain that middleware from a place where it can be mocked, it's not mockable out of the box. If you're on CakePHP 4.2+, you could probably use the DIC. |
# |
May 18th 2021, 19:27 |
caseyw |
Question for you all. I'm writing a test for a route, and that route is using middleware using "->setMiddleware()". I'd like to be able to inject a mock middleware inside my test, but I'm not seeing anything online so far. Any tips/hints/suggestions? |
# |
May 18th 2021, 19:10 |
paolo.bragagni |
:+1: |
# |
May 18th 2021, 18:20 |
ndm |
https://github.com/cakephp/authorization/tree/master/templates/bake ? |
# |
May 18th 2021, 18:07 |
paolo.bragagni |
A twig file |
# |
May 18th 2021, 18:01 |
ndm |
You mean a command? |
# |
May 18th 2021, 17:57 |
paolo.bragagni |
Sorry. A model for baking policies. |
# |
May 18th 2021, 17:55 |
ndm |
Sorry, I don't know what you mean by "_bake model_"? |
# |
May 18th 2021, 17:15 |
paolo.bragagni |
(there is a bake model for policy?) |
# |
May 18th 2021, 17:14 |
paolo.bragagni |
Ok thanks |
# |
May 18th 2021, 15:34 |
etibor |
i dodnt know that the default.ctp can be used for tis purposes |
# |
May 18th 2021, 15:33 |
etibor |
works a charm |
# |
May 18th 2021, 15:33 |
etibor |
thank you - thank you |
# |
May 18th 2021, 15:30 |
ndm |
At least if I understood your whole ordeal somewhat correctly. |
# |
May 18th 2021, 15:30 |
ndm |
In your default layout, accompanied by a check if the current user has the admin role. |
# |
May 18th 2021, 15:29 |
etibor |
but where should i place for Template elements? |
# |
May 18th 2021, 15:28 |
etibor |
i know for the controllers ehen i want to use a common function i have to place in AppController |
# |
May 18th 2021, 15:28 |
etibor |
if i want to use the admin menu in every admin page where should i add this: echo $this->element('admin_menu'); |
# |
May 18th 2021, 15:27 |
etibor |
just one last question |
# |
May 18th 2021, 15:27 |
etibor |
okey maybe its works id i make empty the default.ctp in the ADmin/layout direcotry |
# |
May 18th 2021, 15:19 |
ndm |
Why not? |
# |
May 18th 2021, 15:18 |
etibor |
if i place my menus to the elments insted of the layouts it can be work. the only issue is that i can not replace the default.ctp menu with admin_menu.ctp in the view |
# |
May 18th 2021, 15:14 |
ndm |
Yes. The only way to avoid that would be to create a custom command I think, one that scans for all tables, and runs the policy bake command for each table/entity. |
# |
May 18th 2021, 15:11 |
paolo.bragagni |
(all my policies) |
# |
May 18th 2021, 15:07 |
paolo.bragagni |
Si we have to edit all my actions manually?? |
# |
May 18th 2021, 15:02 |
ndm |
@paolo.bragagni That's not going to happen, what is baked by the `all` command is hardcoded in that command. |
# |
May 18th 2021, 15:01 |
ndm |
That doesn't answer my question though. |
# |
May 18th 2021, 15:00 |
etibor |
well the thing for the UsersController located in Templates/Users - without the prefix |
# |
May 18th 2021, 14:56 |
ndm |
@etibor Are you just using separate templates so that you can display different menus per role? |
# |
May 18th 2021, 14:47 |
paolo.bragagni |
it creates a LibroPolicy.php in my src\Policy |
# |
May 18th 2021, 14:46 |
paolo.bragagni |
I'd like that when I fire bin/cake bake all Libri -t BragBake |
# |
May 18th 2021, 14:44 |
paolo.bragagni |
bake template for policy? |
# |
May 18th 2021, 14:39 |
etibor |
the issue is when i navigate to the users/profile page and logged with admin for example i navigate to site-url/admin/users/profile |
# |
May 18th 2021, 14:37 |
etibor |
i have now: Template/admin/layout/admin_menu.ctp Template/clerk/layout/clerk_menu.ctp while my commonly used functions located in Controller/UsersController.php - non prefixed |
# |
May 18th 2021, 14:33 |
ndm |
Trying to substitute layouts is most likely having looots of pitfalls, I probably wouldn't even try it, rather I'd use a custom view or live with the mentioned layout subdirectory "workaround". |