Can somebody help me out with a test for function in controller. Cannot test if statement in controller, so please focus on this piece of code. Other tests for this controller are good, but only code from "if statement" cannot be tested.
LanguageController.php
class LanguageController extends Controller implements IEntityViewManager{
protected $languageRepo;
public function __construct(LanguageRepositoryInterface $languageRepo)
{
$this->languageRepo = $languageRepo;
}
public function createAction(LanguagePostRequest $request)
{
$languages = $this->languageRepo->whereCharOrName($request->char, $request->name);
if(count($languages) > 0)
{
return redirect()->back()->withErrors("Already exists")->withInput();
}
$language = new Language();
$this->languageRepo->store($language, $request->all());
return redirect()->route('Admin.Language.showAllView');
}
}
Here is my Test reqirements for this test:
LanguageControllerTest.php
class LanguageControllerTest extends TestCase{
public function __construct($name = NULL, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
Mockery::close();
}
protected function setUpMock()
{
$mock = Mockery::mock(LanguageRepositoryInterface::class);
$this->app->instance(LanguageRepositoryInterface::class, $mock);
return $mock;
}
public function testInvalidInsertLanguage1()
{
$params = array(
'char' => 'en',
'name' => 'English'
);
$mock = $this->setUpMock();
// HELP ME TO TEST IF STATEMENT AND TO REDIRECT BACK WITH ERRORS AND INPUTS
// NEED CONTENT
$this->action('POST', 'Entities\LanguageController@createAction', null, $params);
}
Or maybe I should avoid if statement and put it in some other function within controller, but it is too complicated to test, because I should mock this controller ?
Aucun commentaire:
Enregistrer un commentaire