Search code examples
symfonybehatsylius

Testing multiple channels with Sylius / Behat - UnexpectedPageException


The test is failing when using multiple channels, when I use a single channel the @When I visit homepage step is working fine ..

Behat feature:

  Background:
    Given the store operates on a single channel
    And the store operates on another channel named "ChannelA"
    And the store operates on another channel named "ChannelB"

  @ui
  Scenario: Navigating to some channel when clicking on the shop block
    When I visit the homepage

Fails on:

/**
 * @When I visit the homepage
 */
public function iVisitTheHomePage()
{
    try {
        $this->homePage->open();
    } catch (UnexpectedPageException $e) {
        dump($e);
        die();
    }
}

The channels are available in the Test DB:

I noticed that the 'HOST' of the channel in the test database remains NULL when testing a single channel but also when using multiple channels.. so it seems that's not the problem..

1809    NULL    NULL    DEFAULT     Default     NULL    NULL    1   NULL    2016-10-31 10:20:45 2016-10-31 10:20:45 NULL    NULL    order_items_based
1810    NULL    NULL    CHANNELA    ChannelA    NULL    NULL    1   NULL    2016-10-31 10:20:45 2016-10-31 10:20:45 NULL    NULL    order_items_based
1811    NULL    NULL    CHANNELB    ChannelB    NULL    NULL    1   NULL    2016-10-31 10:20:45 2016-10-31 10:20:45 NULL    NULL    order_items_based

UnexpectedPageException:

Sylius\Behat\Page\UnexpectedPageException {#9766
  #message: "Could not open the page: "http://localhost:8080/". Received an error status code: 500"
  #code: 0
  #file: "/var/www/html/vendor/sylius/sylius/src/Sylius/Behat/Page/Page.php"
  #line: 117
  -trace: {
    35. Sylius\Behat\Page\Page->verifyStatusCode() ==> new Sylius\Behat\Page\UnexpectedPageException(): {
      src: {
        /var/www/html/vendor/sylius/sylius/src/Sylius/Behat/Page/Page.php:117: """
          \n
              throw new UnexpectedPageException($message);\n
          }\n
          """
      }
    }
    34. Sylius\Behat\Page\Page->verify() ==> Sylius\Behat\Page\Page->verifyStatusCode(): {
      src: {
        /var/www/html/vendor/sylius/sylius/src/Sylius/Behat/Page/Page.php:74: """
          {\n
              $this->verifyStatusCode();\n
              $this->verifyUrl($urlParameters);\n
          """
      }
      args: []
    }
    33. Sylius\Behat\Page\Page->open() ==> Sylius\Behat\Page\Page->verify(): {
      src: {
        /var/www/html/vendor/sylius/sylius/src/Sylius/Behat/Page/Page.php:58: """
              $this->tryToOpen($urlParameters);\n
              $this->verify($urlParameters);\n
          }\n
          """
      }
      args: array:1 [
        0 => []
      ]
    }
    32. MyProject\Behat\Context\Ui\HomePageContext->iVisitTheHomePage() ==> Sylius\Behat\Page\Page->open(): {
      src: {
        /var/www/html/src/MyProject/Behat/Context/Ui/HomePageContext.php:34: """
          try {\n
              $this->homePage->open();\n
          } catch (UnexpectedPageException $e) {\n
          """
      }
      args: []
    }
    31. call_user_func_array() ==> MyProject\Behat\Context\Ui\HomePageContext->iVisitTheHomePage(): {
      args: []
    }
    30. Behat\Testwork\Call\Handler\RuntimeCallHandler->executeCall() ==> call_user_func_array(): {
      src: {
        /var/www/html/vendor/behat/behat/src/Behat/Testwork/Call/Handler/RuntimeCallHandler.php:104: """
          try {\n
              $return = call_user_func_array($callable, $arguments);\n
          } catch (Exception $caught) {\n
          """
      }
      args: array:2 [
        0 => array:2 [
          0 => MyProject\Behat\Context\Ui\HomePageContext {#4099
            -homePage: MyProject\Behat\Page\Shop\HomePage {#4100
              #router: Symfony\Cmf\Component\Routing\ChainRouter {#4145
                -context: Symfony\Component\Routing\RequestContext {#4140
                  -baseUrl: ""
                  -pathInfo: "/"
                  -method: "GET"
                  -host: "localhost"
                  -scheme: "http"
                  -httpPort: 80
                  -httpsPort: 443
                  -queryString: ""
                  -parameters: []
                }
                -routers: array:3 [
                  100 => array:1 [
                    0 => Symfony\Bundle\FrameworkBundle\Routing\Router {#4128
                      -container: appTestDebugProjectContainer {#5073 …12}
                      #matcher: null
                      #generator: appTestUrlGenerator {#4287
                        #routes: null
                        #context: Symfony\Component\Routing\RequestContext {#4140}
                        #strictRequirements: true
                        #logger: null
                        #decodedChars: array:10 [ …10]
                      }
                      #context: Symfony\Component\Routing\RequestContext {#4140}
                      #loader: null
                      #collection: null
                      #resource: "/var/www/html/app/config/routing.yml"
                      #options: array:12 [
                        "cache_dir" => "/var/www/html/app/cache/test"
                        "debug" => true

Solution

  • Fixed it by using the following step definition from Sylius CurrentChannelContext:

    /**
     * @Given /^I am browsing (channel "([^"]*)")$/
     */
    public function iAmBrowsingChannel(ChannelInterface $channel)
    {
        $this->channelContextSetter->setChannel($channel);
    }
    

     Background:
        Given the store operates on a single channel
        And the store operates on another channel named "ChannelA"
        And the store operates on another channel named "ChannelB"
    
      @ui
      Scenario: Navigating to some channel when clicking on the shop block
        Given I am browsing channel "Default"
        When I visit the homepage