php - Unit test method with Silex\Application as parameter -


in project have class sessionmanager sets , clears session variables sticky forms etc. every method in class takes in silex\application object parameter. how can unit test these methods? create silex application object in each test method? i'm new silex , unit testing , not sure how handle this.

example of 1 method:

public static function setmessage($message, $messageclass, application &$app) {     // store message array, content , class keys     $app['session']->set('message', array(         'content' => $message,         'class' => $messageclass     )); } 

firstly think $app should not dependency of sessionmanager; $app['session'] should be. should dependency of object (ie: passed constructor) not of individual method.

but doesn't change tactic solving problem. need create mock of session, covers dependent method need call, eg:

// in test class // ... public function setup(){     $mockedsession = $this->getmockedsession();     $this->sessionmanager = new sessionmanager($mockedsession); }  public function testmessage(){     // test stuff here }  private function getmockedsession(){     $mockedsession = $this->getmockbuilder('\symfony\component\httpfoundation\session\session')         ->disableoriginalconstructor()         ->setmethods(['set'])         ->getmock();      $mockedsession->method('set')         ->willreturn('something testable');      return $mockedsession; } 

you need test setmessage method passes through $message , $messageclass values mocked method: that's it's doing. in light wanna have ->with('message', [$testmessage, $testclass]) or in mock too. exact implementation down how want test.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -