channa ly
channa ly

Reputation: 9937

How to test kohana model, controller

I have been working with php for more than 5 years. Lately I have worked in rubyonrails. I have done a few projects in this very nice framework. What I like best from rails and ruby: they both promote automate test and there are so many rich libs. Rspec and TestUnit are very easy to learn comparing to PhpUnit.

I have to develop a very big project in the next coming month. I am a big fan in cakephp but I realize that cakephp will not meet my project requirement. I am a quick learner. After reading doc from Kohana official website, I will use Kohana for this project.

After having done some search on Kohana, I still have a few topics to concern about

  1. The test module is lack of doc. I am not clear how to test model, controller, functional test. Could anyone provide me ideas, tutorials, examples, resources ?
  2. The application environment is not quite clear. Sorry because I am pretty family with rubyonrails . I feel like environment in rails make more sense to me. I can have one gem in a specific environment. for example I have rspec gem (for automate test ) for test environment only and I have unicorn gem for production only. For those who are new to ruby, gem is something similar to "module" in kohana. Could anyone tell me how to tell kohana to just load "unittest" in test environment only ? because I don't want to load unittest in production env.
  3. In Rails there is an app console mode called rails console. with rails console we can interact with models via console mode . Is there anything similar to this in Kohana ?

Upvotes: 2

Views: 1169

Answers (1)

matino
matino

Reputation: 17725

I can answer only 2 of your questions, still better then nothing ;)

AD2. You can set Kohana::$environment variable based on the .htaccess (setenv and getenv) / $_SERVER setting:

if (Arr::get($_SERVER, 'SERVER_NAME') !== 'localhost')
{
    // We are live!
    Kohana::$environment = Kohana::PRODUCTION;

    // Turn off notices and strict errors
    error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
}
else
{
    Kohana::$environment = Kohana::DEVELOPMENT;
    error_reporting(E_ALL | E_STRICT);
}

Then you can setup Kohana::init like this:

Kohana::init(array(
    'base_url' => '/',
    'caching' => Kohana::$environment === Kohana::PRODUCTION,
    'profile' => Kohana::$environment !== Kohana::PRODUCTION,
    'index_file' => FALSE,
    'errors' => TRUE
));

so your production application will have caching enabled and profiling disabled.
For the modules it's pretty much the same:

if (Kohana::$environment !== Kohana::PRODUCTION)
{
    Kohana::modules(array(
        'unittest' => MODPATH . 'unittest',
    ));
}

AD3. Sorry for being laconic - no, there isn't one.

Upvotes: 3

Related Questions