# |
Jun 10th 2021, 18:42 |
admad |
That's essentially what's used to parse the current request url too :) |
# |
Jun 10th 2021, 18:38 |
greg138 |
In 3.x, at least, it seems that `loadRoutes` is called by any router function that might need them, like `url`. |
# |
Jun 10th 2021, 18:38 |
kevin.pfeifer |
thats cool, didn’t know that :thinking_face: |
# |
Jun 10th 2021, 18:36 |
kevin.pfeifer |
routes are definitely not loaded by default |
# |
Jun 10th 2021, 18:35 |
tyler.adam.lazenby |
```public function sendEmail() { $this->Authorization->skipAuthorization(); $referrer = $this->request->referer(); $params = Router::getRouteCollection()->parse($referrer); return $this->response->withDisabledCache() ->withType('application/json') ->withStringBody(json_encode([ 'url' => $referrer, 'params' => $params ])); }``` |
# |
Jun 10th 2021, 18:34 |
kevin.pfeifer |
`Cake\TestSuite\TestCase` yes |
# |
Jun 10th 2021, 18:34 |
tyler.adam.lazenby |
but I think I just figured out a way |
# |
Jun 10th 2021, 18:34 |
tyler.adam.lazenby |
Just the refering url |
# |
Jun 10th 2021, 18:33 |
greg138 |
I've never needed to do this. Are you extending Cake's `TestCase` class? |
# |
Jun 10th 2021, 18:29 |
kevin.pfeifer |
if so then you have everything in this array ```$params = $this->getRequest()->getAttribute('params');``` |
# |
Jun 10th 2021, 18:28 |
kevin.pfeifer |
do you have a request object where you want to do that? |
# |
Jun 10th 2021, 18:22 |
tyler.adam.lazenby |
I know you can reverse an array into a string, but I can't seem to figure out how to take the referer url and make it something my system can use |
# |
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? |