Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
namespace modules view
  • i have this folder structure
    - app
      - modules
          - api
              - classes
                  - controller
                  - model
                      - project
                           - list.php
                      - mailer.php
              - tests
                   - model
                       - project
                            - list.php
              - views
                  - mail
                       - template1.php


    all controller/model/tests
    are using namespace Api

    my classes/model/project/list.php
    is triggering the Model_Mailer
    `return Model_Mailer::send_offer($data);`

    im my classes/model/mailer.php
    im rendering a phpview for html body of email
    `\View::forge('mail/template1');`

    this works if i trigger the model via classes\controller
    but when i try to test the model
    (triggered in the tests/model/project/list.php)

    it throws error
    Fuel\Core\FuelException: The requested view could not be found: mail/template1.php


    in my config
    (load modules path)
    'module_paths' => array(
          APPPATH.'modules'.DS
    )

    always_load -> 'modules'  => array('api'),


    why the test cannot find the template ?

    run via `php oil test`
  • This has to do with "request context".

    When you get a request for a module controller, like with Request::forge('api/some/controller'), the Framework switches it's request context to the api module, so it knows it has to look in that module for resources like views, config, lang files, etc.

    When you run tests, the test classes are instantiated like normal classes, completely outside the Fuel framework. So none of these features will work.
  • Some background:

    adding the module path to the request happens here: https://github.com/fuel/core/blob/1.9/develop/classes/request.php#L324

    The View class collects the path in it's constructor: https://github.com/fuel/core/blob/1.9/develop/classes/view.php#L138

    and feeds it to the Finder: https://github.com/fuel/core/blob/1.9/develop/classes/view.php#L459

    So you could possibly work around it by giving your test classes setup and teardown methods, and add the path manually, something like

    public static function setUpBeforeClass()
    {
        // number of dirs' up depend on the location of this class in the test tree
        // the result of the realpath() call should be the module root directory
        \Finder::instance()->flash(realpath(__DIR__.DS.'..'.DS.'..'.DS.'..'.DS.'..').DS);
    }

    public static function tearDownAfterClass()
    {
        \Finder::instance()->clear_flash();
    }

    // disclaimer: from the top of my head, not tested! ;-)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion