Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
phpunit test Controllers
  • I'm using Request::forge() for controller test as follows.
    I want to disable all core class object cache (static properties).
    Isn't there a good method for that?

        public function test_action_edit_fail($test_id)            
        {            
            $_SERVER['REQUEST_METHOD'] = 'POST';
            $_POST['s_ym'] = '2015-05-11';
            $this->_assert_eq_err($test_id, 'error_msg1', '_post_name1');
                
            $_POST['s_ym'] = '2015-05-12'
            $this->_assert_eq_err($test_id, 'error_msg2', '_post_name2');
        }            

        public function test_action_edit_success($test_id)            
        {            
            $_SERVER['REQUEST_METHOD'] = 'GET';        
            $response = Request::forge('/top/index/')->execute()->response();        
            $this->assertTrue(isset($response->body->get('data')[10000]));        
            $_SERVER['REQUEST_METHOD'] = 'POST';
            $_POST['status'] = 2;        
            $this->_assert_eq_success($test_id);
        }        

        private function _assert_eq_err($test_id, $msg, $name)
        {
            Request::forge('/test/edit/'.$test_id)->execute();
            $this->assertTrue($msg == Session::get_flash('error')[$name]->get_message());
            Fieldset::reset();    // clear static::$_instances (extends core class)
            Session::destroy();   
            Input::delete();      // clear static::$input
        }
        
        private function _assert_eq_success($test_id)
        {
            Request::forge('/test/edit/'.$test_id)->execute();
            $this->assertTrue(Session::get_flash('success') != null);
            Fieldset::reset();
            Session::destroy();
            Input::delete();
            Model_Reserve::reset(); // clear static::$_cached_objects[get_called_class()]
        }

  • HarroHarro
    Accepted Answer
    I wouldn't know, I have always found statics difficult to test, and never bothered.

    Ideally you should test individual controller methods directly, in isolation, and not the entire Request/Response chain.
  • OK, Thank you :)
  • If you could reset static properties, put the code in `setUp()`.

Howdy, Stranger!

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

In this Discussion