# |
Jun 10th 2021, 18:22 |
tyler.adam.lazenby |
Is there a method for taking apart a url and seperating it into its Router elements? |
# |
Jun 10th 2021, 18:06 |
tyler.adam.lazenby |
yay! finally got it to spit out the referer |
# |
Jun 10th 2021, 17:55 |
tyler.adam.lazenby |
thanks |
# |
Jun 10th 2021, 17:55 |
tyler.adam.lazenby |
I was looking for something like that |
# |
Jun 10th 2021, 17:53 |
kevin.pfeifer |
https://book.cakephp.org/4/en/development/testing.html#loading-routes-in-tests |
# |
Jun 10th 2021, 17:53 |
kevin.pfeifer |
```public function setUp(): void { parent::setUp(); $this->loadRoutes(); }``` |
# |
Jun 10th 2021, 17:53 |
kevin.pfeifer |
you need to load them in your setup function |
# |
Jun 10th 2021, 17:53 |
kevin.pfeifer |
routes are not loaded automatically in tests |
# |
Jun 10th 2021, 17:51 |
tyler.adam.lazenby |
```public function testSendEmail(): void { $referral_url = Router::url([ 'controller' => 'Contacts', 'action' => 'view', 1 ], true); $headers = ['referrer' => $referral_url]; $this->configRequest([ 'headers' => $headers ]); $this->get('contact/send-email'); $data = json_decode($this->_getBodyAsString()); dd($data); }``` |
# |
Jun 10th 2021, 17:51 |
tyler.adam.lazenby |
Cake\Routing\Exception\MissingRouteException : A route matching "array ( 'controller' => 'Contacts', 'action' => 'view', 0 => 1, 'plugin' => NULL, '_ext' => NULL, )" could not be found. |
# |
Jun 10th 2021, 17:51 |
tyler.adam.lazenby |
Well that is all well and good, until you have this error because phpunit doesn't build the router |
# |
Jun 10th 2021, 17:48 |
kevin.pfeifer |
didn’t read slack before posting :,) |
# |
Jun 10th 2021, 17:48 |
kevin.pfeifer |
well |
# |
Jun 10th 2021, 17:48 |
kevin.pfeifer |
``` $this->configRequest([ 'headers' => ['Accept' => 'application/json'] ]);``` |
# |
Jun 10th 2021, 17:45 |
tyler.adam.lazenby |
Oof thanks |
# |
Jun 10th 2021, 17:45 |
greg138 |
https://book.cakephp.org/3/en/development/testing.html#setting-up-the-request |
# |
Jun 10th 2021, 17:44 |
tyler.adam.lazenby |
Hetting the appropriate header with phpunit? |
# |
Jun 10th 2021, 17:44 |
greg138 |
That's just a matter of setting the appropriate header in your simulated call to `send-email`. |
# |
Jun 10th 2021, 17:44 |
tyler.adam.lazenby |
exactly |
# |
Jun 10th 2021, 17:44 |
kevin.pfeifer |
so you check the url / referrer of that ajax call in your controller function? |
# |
Jun 10th 2021, 17:42 |
tyler.adam.lazenby |
to `/contacts/send-email` |
# |
Jun 10th 2021, 17:42 |
tyler.adam.lazenby |
I just want to simulate for example making a get request from /appname/contacts/view/1 |
# |
Jun 10th 2021, 17:41 |
greg138 |
Simulate which part of it? There's great support for testing controller actions with any method you want and any data you care to pass to them. |
# |
Jun 10th 2021, 17:41 |
kevin.pfeifer |
now i understand |
# |
Jun 10th 2021, 17:41 |
kevin.pfeifer |
ahhhhhh |
# |
Jun 10th 2021, 17:41 |
tyler.adam.lazenby |
sorry just lots of issues and my adhd brain is developing code and asking at the same time |
# |
Jun 10th 2021, 17:40 |
tyler.adam.lazenby |
problem is how do you simulate making the request from a page with unit testing? |
# |
Jun 10th 2021, 17:40 |
greg138 |
If it's something that's going to be invariant, why wouldn't you use a unit test? It confirms your initial code, and also makes sure it keeps working forever. |
# |
Jun 10th 2021, 17:39 |
kevin.pfeifer |
why should that be janky? |
# |
Jun 10th 2021, 17:39 |
kevin.pfeifer |
ok then i totally dont get what youre asking :,) |
# |
Jun 10th 2021, 17:39 |
tyler.adam.lazenby |
Just seems janky |
# |
Jun 10th 2021, 17:38 |
tyler.adam.lazenby |
hahaha yeah yeah I know |
# |
Jun 10th 2021, 17:38 |
kevin.pfeifer |
well you can set a xdebug breakpoint in a test as well if you want :) |
# |
Jun 10th 2021, 17:38 |
tyler.adam.lazenby |
and I use PHPstorm so unit testing is very easy |
# |
Jun 10th 2021, 17:37 |
tyler.adam.lazenby |
because I use a CI |
# |
Jun 10th 2021, 17:37 |
tyler.adam.lazenby |
ok, I get that, but I also am unit testing lol |
# |
Jun 10th 2021, 17:37 |
kevin.pfeifer |
in this case where you probably just return json data and want to check what is in a variable you can also just log the variable with `Log::write()` |
# |
Jun 10th 2021, 17:35 |
tyler.adam.lazenby |
I personally make heavy use of `debug()` and `dd()` |
# |
Jun 10th 2021, 17:34 |
tyler.adam.lazenby |
without having to pass that |
# |
Jun 10th 2021, 17:34 |
tyler.adam.lazenby |
I am basically trying to make an ajax command that will check if the page making the request is contact action called view, and then check to see what the ID of the viewed item is |
# |
Jun 10th 2021, 17:34 |
kevin.pfeifer |
but I know enough people who still prefer/löve `var_dump()` aka `pr()` |