PHP code example of t4web / actioninjections
1. Go to this page and download the library: Download t4web/actioninjections library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
t4web / actioninjections example snippets
class AjaxController extends Zend\Mvc\Controller\AbstractActionController
{
public function deleteTimesheetAction() {
$view = new ViewModel();
if (!$this->getRequest()->isPost()) {
return $view;
}
$timesheetDeleteService = $this->getServiceLocator()->get('Timesheet\Timesheet\Service\Delete');
$timesheetId = $this->getRequest()->getPost()->get('id', 0);
if (!$timesheetDeleteService->delete($timesheetId)) {
$view->errors = $timesheetDeleteService->getErrors();
}
return $view;
}
//...
}
class AjaxController extends Zend\Mvc\Controller\AbstractActionController
{
/**
* @var BaseFinder
*/
private $timesheetFinder;
/**
* @var BaseFinder
*/
private $calendarFinder;
/**
* @var CreateInterface
*/
private $createService;
/**
* @var UpdateInterface
*/
private $updateService;
/**
* @var DeleteInterface
*/
private $deleteService;
/**
* @var AjaxViewModel
*/
private $view;
public function __construct(
BaseFinder $timesheetFinder,
BaseFinder $calendarFinder,
CreateInterface $timesheetCreateService,
UpdateInterface $timesheetUpdateService,
DeleteInterface $timesheetDeleteService,
AjaxViewModel $view)
{
$this->timesheetFinder = $timesheetFinder;
$this->calendarFinder = $calendarFinder;
$this->createService = $timesheetCreateService;
$this->updateService = $timesheetUpdateService;
$this->deleteService = $timesheetDeleteService;
$this->view = $view;
}
public function deleteTimesheetAction() {
$view = new ViewModel();
if (!$this->getRequest()->isPost()) {
return $this->view;
}
$timesheetId = $this->getRequest()->getPost()->get('id', 0);
if (!$this->deleteService->delete($timesheetId)) {
$this->view->errors = $this->deleteService->getErrors();
}
return $this->view;
}
// ...
}
class AjaxControllerFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$serviceManager = $serviceLocator->getServiceLocator();
return new AjaxController(
$serviceManager->get('Timesheet\Timesheet\Service\Finder'),
$serviceManager->get('Calendar\Calendar\Service\Finder'),
$serviceManager->get('Timesheet\Timesheet\Service\Create'),
$serviceManager->get('Timesheet\Timesheet\Service\Update'),
$serviceManager->get('Timesheet\Timesheet\Service\Delete'),
$serviceManager->get('Timesheet\Controller\ViewModel\AjaxViewModel')
);
}
}
'controller_action_injections' => array(
'Timesheet\Controller\User\AjaxController' => array(
'deleteTimesheetAction' => array(
'request',
'Timesheet\Controller\ViewModel\AjaxViewModel',
'Timesheet\Timesheet\Service\Delete',
),
),
),
class AjaxController extends Zend\Mvc\Controller\AbstractActionController
{
public function deleteTimesheetAction(HttpRequest $request, AjaxViewModel $view, DeleteInterface $timesheetDeleteService) {
if (!$request->isPost()) {
return $view;
}
$timesheetId = $request->getPost()->get('id', 0);
if (!$timesheetDeleteService->delete($timesheetId)) {
$view->setErrors($timesheetDeleteService->getErrors());
}
return $view;
}
//...
}
class AjaxControllerTest extends \PHPUnit_Framework_TestCase {
public function testDeleteTimesheetAction_Delete_ReturnView() {
$requestMock = $this->getMockBuilder('Zend\Http\PhpEnvironment\Request')->disableOriginalConstructor()->getMock();
$timesheetDeleteServiceMock = $this->getMockBuilder('T4webBase\Domain\Service\Delete')->disableOriginalConstructor()->getMock();
$ajaxViewModel = new AjaxViewModel();
$timesheetId = 1;
$parameters = new Parameters(array('id' => $timesheetId));
$requestMock->expects($this->once())
->method('isPost')
->will($this->returnValue(true));
$requestMock->expects($this->once())
->method('getPost')
->will($this->returnValue($parameters));
$timesheetDeleteServiceMock->expects($this->once())
->method('delete')
->with($this->equalTo($timesheetId))
->will($this->returnValue(true));
$controller = new AjaxController();
/** @var $result AjaxViewModel */
$result = $controller->deleteTimesheetAction($requestMock, $ajaxViewModel, $timesheetDeleteServiceMock);
$this->assertEquals($ajaxViewModel, $result);
}
//...
}
bash
$ php composer.phar update