0

In the below test we (the humbly well my I am) need to make sure that $utmSource class field received correct value.

<?php

class ExampleTest extends TestCase {

  public function testHttpRequestAndControllerCoordination() {

    $response = $this->call('GET', '/')->json([utm_source => 'Google']);
    $topPageController = new TopPageController();
    $topPageController->renderTopPage();

    $this->assertAttributeSame('Google', 'utm_source', $topPageController);
  }
}

class TopPageController extends Controller {

    private $utmSource;

    public function renderTopPage(){
        $this->utmSource = request()->utm_source;
    }
}

Off course, this test will not pass, because $response and $topPageController are independent, so $utmSource will be null. How we can relate $response and $topPageController?

hakre
  • 193,403
  • 52
  • 435
  • 836
Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124
  • Make a different method (or modify your current one) that accepts the `utm_source` as a parameter. That way you decouple it from the request and are able to test it – apokryfos Aug 17 '18 at 07:46
  • I guess you need to bind the controller to the test-case before you call "GET". – hakre Aug 20 '18 at 22:56
  • @hakre, thank you for the comment. Please, give me the link to some example. – Takeshi Tokugawa YD Aug 20 '18 at 23:04
  • Unfortunately I don't have any example at hand (otherwise I would have shared that for sure, I'm not mean), it's just what I can hardly imagine which is why I guessed that.Nothing in Laravell docs about controller testing? – hakre Aug 20 '18 at 23:17
  • @hakre, almost nothing for Laravel 5.6 ... – Takeshi Tokugawa YD Aug 22 '18 at 14:16

0 Answers0