We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Codeception - Functional Tests: how to access variables inside a view

In the functional test below I want to access the variable $current_page but get an "unknown variable error". How can I access variables inside views with codeception?

I have a volt template with this inside

           ...
            <form action="/listlab/{{ current_page }}" accept-charset="utf-8" method="post">
              <table>
                <thead>
                <tr>
                  <th>Key</th>
                  <th>Name</th>
                  <th>Description</th>
                  <th>Actions</th>
                </tr>
                </thead>
                <tbody>
                {% if pagination is defined %}
                  {% for row in pagination.items %}
                    <tr>
                      <td>{{ row.key }}</td>
                      <td>{{ row.name }}</td>
                      <td>{{ row.description }}</td>
                      <td class="actions">
                        <a href="/showlab//{{ row.label_id }}" class="button small">View</a>
                      </td>
                    </tr>
                  {% endfor %}
                {% endif %}
                </tbody>
              </table>
            </form>
           ...

And this in my functional test

$I = new FunctionalTester($scenario);
$I->wantTo("Test Label Overview");
$I->amOnPage("/index/");
$I->seeLink("Overview");
$I->click("Overview");
$I->see("List labels");
$I->seeElement('a', ['name' => 'listLabels']);
$I->click('a', ['name' => 'listLabels']);
$I->see("List Labels");
$I->seeElement("table"); /tables of object (labels)
$I->seeElement("td");
$I->see("my_label");
// works until here

// does not work:
$I->assertNotEmpty($current_page);
// gives the error

The error when the last line of the test is included

Functional Tests (2)
-----------------------------------------------------------------
E LabelOverviewCept: Test Label overview (0.06s)
-----------------------------------------------------------------

Time: 12 ms, Memory: 14.00MB

There was 1 error:

---------
1) LabelOverviewCept: Test Label overview
 Test  tests/functional/LabelOverviewCept.php

  [PHPUnit\Framework\Exception] Undefined variable: current_page  


77.7k
Accepted
answer
edited Jun '18

Afaik, codeception operates on rendered html, so you will never be able to access runtime php variables on the output html.

You would have to mock the controller instance, and test the view variables from there.

Or you could use grabTextFrom and do simply assert if you know what expected value should be?

Afaik, codeception operates on rendered html, so you will never be able to access runtime php variables on the output html.

You would have to mock the controller instance, and test the view variables from there.

Thanks.