The server don't receive a Response / SendRequests - zend-framework

Iam new guy for Zend2 framework...I got an error which I didnt trace it...
Iam writing a controller named 'usertask' and in that fir index function i wrote the code like this
public function indexAction()
{
$sendRequest = new SendRequests;
$tableGrid = new DynamicTable();
$prop = array(
'customRequest' => 'GET',
'headerInformation' => array('environment: development', 'token_secret: abc')
);
$returnRequest = $sendRequest->set($prop)->requests('http://service-api/usertask');
$returnData = json_decode($returnRequest['return'],true);
$tableGrid->tableArray = $returnData['result'];
$dynamicTable = $tableGrid->tableGenerate();
$view = new ViewModel(array(
'usertask' => $dynamicTable
));
//print_r($view);exit;
return $view;
}
but it is not listing my usertasks...while Iam printing $returnRequest its giving me error message like
The server don't receive a Response / SendRequests
what it the mistake in my code...could anyone suggest me...please..iam using "zend2"

Sorry guys I found my mistake ...I got big code but I need something like
public function indexAction()
{
$view = new ViewModel(array(
'usertask' => $this->UserTable()->fetchall(),
));
return $view;
}
public function getUserTable()
{
if (!$this->userTable) {
$sm = $this->getServiceLocator();
$this->userTable = $sm->get('User\Model\UserTable');
}
return $this->userTable;
}
that's it...i got it as a list of users

Related

Silverstripe 4 Form on custom PageController submit

I have a Form in my custom PageController.
The Form renders properly in template Register.ss.
In my setup the submit - function on PageController doesn't work.
I have tried it without setting the submit-route in routes.yml
resulting in a 404.
I have tried to set the submit-route in routes.yml
and in $url_handlers which results in an error:
Uncaught ArgumentCountError: Too few arguments to function
TherapyRegisterController::submit(), 1 passed in
/var/home/xxxxx/vendor/silverstripe/framework/src/Control/RequestHandler.php
on line 323 and exactly 2 expected
How to get the submit - function to work?
//routes:
SilverStripe\Control\Director:
rules:
therapy//anmeldung/$ID: TherapyRegisterController
# therapy//submit: TherapyRegisterController
TherapyRegisterController:
class TherapyRegisterController extends PageController{
private static $allowed_actions = ['registerForm', 'submit'];
private static $url_handlers = [
'anmeldung/$ID' => 'index',
//'anmeldung/submit' => 'submit',
];
public function registerForm($id)
{
$fields = new FieldList(
TextField::create('Name', 'Name')
);
$actions = new FieldList( [
$cancelButton = FormAction::create('cancel')->setTitle('ABBRECHEN')->setAttribute('formaction', 'therapy/cancel'), // 'cancel'
$sendButton = FormAction::create('submit')->setTitle('ANMELDEN')->setAttribute('formaction', 'therapy/submit') // 'submit'
]);
$validator = new RequiredFields('');
$form = new Form($this, 'registerForm', $fields, $actions, $validator);
$form->setTemplate('Register');
return $form;
}
public function submit($data, Form $form)
{
Debug::show($data);
}
public function index(HTTPRequest $request)
{
$arrayData = array (
'ID' => $request->param('ID')
);
return $this->customise($arrayData)->renderWith(array('Anmeldung', 'Page'));
}
Register.ss :
$registerForm($ID)
The ArgumentCountError does say that the submit method was only getting 1 argument but that submit method is expecting 2 as seen in your code. I'm not sure what exact version of SilverStripe you have but I can see this on line 323 of that RequestHandler.php:
$actionRes = $this->$action($request);
That first argument is going to be a SilverStripe\Control\HTTPRequest. The form submission should be a POST. You can get that array $data using the example below:
public function submit($request)
{
$data = $request->postVars();
}
It seems that you can't get that Form $form from the arguments here.

Why my Zend-HAL implementation is not working with protected values

I am new in Zend framework, and trying to use HAL for API response generation. In the following is a simpler situation of my issues.
The class:
class Version
{
protected $data;
public function __construct($ar){
$data = $ar;
}
public function getArrayCopy(){
return $data;
}
}
$obj = new version(['major'=>1,'minor'=>2,'fix'=>3]);
When I test with hydrator, it works well as per the following:
use Zend\Hydrator\ArraySerializableHydrator;
$hydrator = new ArraySerializableHydrator();
$data = $hydrator->extract($obj);
print_r($data); // outputs ['major'=>1,'minor'=>2,'fix'=>3]
My HAL configuration is following:
MetadataMap::class => [
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => Version::class,
'route' => 'version',
'extractor' => ArraySerializableHydrator::class,
],
]
I use the following line in my Zend expressive (version 3) request handler
$resource = $this->resourceGenerator->fromObject($obj, $request);
$res = $this->responseFactory->createResponse($request, $resource);
The link is generated correctly, but the meta data (version info) is coming as empty. Any help will be much appreciated.
N.B.: My real code is complex, here I tried to generate a simpler version of the issue.
I think that when generating response the hydrate method is called. So your test does not seem to test what you meant to test.
When hydrating the hydrator works with ReflectionClass. So you need to add the indexes from $data as properties in the Version class.
e.g.
class Version
{
protected $major;
protected $minor;
protected $fix;
public function __construct($data){
foreach($data as $key => $value) {
$this->{$key} = $value;
}
}
public function getArrayCopy(){
return [
'major' => $this->major,
'minor' => $this->minor,
'fix' => $this->fix
];
}
}
$obj = new version(['major'=>1,'minor'=>2,'fix'=>3]);

How to consume 3rd Party wsdl in magento2

I need to fetch Customer Information from a third party http://91.209.142.215:2803/SXYMAGENTO/?wsdl. I can connect it using SOAPUI and get desired response, but I am not able to connect it via Magento2. So far I tried
$requestData = [
'pageSize' => 1,
'pageNumber' => 1
];
$webservice_url = 'http://xx.xxx.xxx.xx:xxxx/MAGENTO/?wsdl';
$token = 'm31oix12hh6dfthmfmgk7j5k5dpg8mel';
$opts = array(
'http'=>array(
'header' => 'Authorization: Bearer '.$token)
);
$context = stream_context_create($opts);
$soapClient = new \SoapClient($webservice_url, ['version' => SOAP_1_2, 'context' => $context]);
$collection = $soapClient->RetrieveCollection($requestData);
print_r($collection);
die();
but this outputs Product data(maybe this is set as default), not customer data. Can anyone please point me in the right direction?
Finally, I figure this out and posting the answer so that it may help anyone craving a solution or fighting with a deadline.
Extend SoapClient and you can change Action, Request, and location
namespace Namespace\SoapModule\Api;
class CustomSoap extends \SoapClient
{
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
$action = 'Your/Action/Obtainedfrom/SOAPAction';
$this->__last_request = $request; //Optional. You should do this if you dont want to get confused when print $request somewhere else in the module
return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}
construct an object of above class wherever required
public function getCustomersDetails($page_size, $page_number)
{
$requestData = [
'pageSize' => $page_size,
'pageNumber' => $page_number
];
$data = $this->client->__soapCall('RetrieveCollection', [$requestData]);
return $this->client->__getLastResponse();
}
public function statementBalance()
{
$responsexml = $this->getCustomersDetails(1, 1);
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $responsexml);
#$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$responseArray = json_decode($json, true);
echo '<pre>';
print_r($responseArray);
}
Happy coding!

PHP-Unit Tests: how to implement a TYPO3 Extbase object interface

I have some problems with the PHP UnitTests for my Controller:
this is the function in my controller code that has to be tested
public function newAction(\ReRe\Rere\Domain\Model\Fach $newFach = NULL) {
// Holt die übergebene Modulnummer
if ($this->request->hasArgument('modul')) {
// Holt das Modul-Objekt aus dem Repository
$modul = $this->modulRepository->findByUid($this->request->getArgument('modul'));
}
// Ausgabe in der View
$this->view->assignMultiple(array(
'newFach' => $newFach, self::MODULUID => $modul->getUid(), 'modulname' => $modul->getModulname(), 'modulnummer' => $modul->getModulnr(), 'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
));
}
this is the PHPUnit-Test code for the function
public function newActionAssignsTheGivenFachToView() {
$fach = new \ReRe\Rere\Domain\Model\Fach();
$modul = array('');
$MockGetArgument = $this->getMock('ReRe\Rere\Domain\Repository\ModulRepository', array('getArgument'), array(), '', FALSE);
$MockGetArgument->expects($this->any())->method('getArgument')->with('modul');
$mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request');
$mockRequest->expects($this->any())->method('hasArgument')->with('modul');
$this->inject($this->subject, 'request', $mockRequest);
$objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectManager', array(), array(), '', FALSE);
$this->inject($this->subject, 'objectManager', $objectManager);
$modulRepository = $this->getMock('ReRe\\Rere\\Domain\\Repository\\ModulRepository');
$modulRepository->expects($this->any())->method('findByUid')->will($this->returnValue($modul));
$this->inject($this->subject, 'modulRepository', $modulRepository);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->any())->method(self::ASSIGN)->with('newFach', $fach);
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
}
I keep getting this error as I run the test
Error in test case newActionAssignsTheGivenFachToView
File: /Applications/MAMP/typo3_src/typo3/sysext/extbase/Classes/Mvc/Controller/AbstractController.php
Line: 162
Argument 1 passed to TYPO3\CMS\Extbase\Mvc\Controller\AbstractController::injectObjectManager() must implement interface TYPO3\CMS\Extbase\Object\ObjectManagerInterface, instance of Mock_ObjectManager_fa2fde18 given, called in /Applications/MAMP/typo3_src/typo3/sysext/core/Tests/BaseTestCase.php on line 260 and defined
And this is the line from AbstractController.php that was called
public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $ObjectManager) {
$this->ObjectManager = $ObjectManager;
$this->arguments = $this->ObjectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments');
}
How can I implement this interface TYPO3\CMS\Extbase\Object\ObjectManagerInterface ?
I really appreciate every answers ! I have been trying and looking for answers for weeks :(
UPDATE 18.02.2015: PROBLEM SOLVED
CONFIGURED CODE:
$fach = new \ReRe\Rere\Domain\Model\Fach();
$modul = new \ReRe\Rere\Domain\Model\Modul();
$request = $this->getMock(self::REQUEST, array(), array(), '', FALSE);
$request->expects($this->once())->method('hasArgument')->will($this->returnValue($this->subject));
$modulRepository = $this->getMock(self::MODULREPOSITORY, array('findByUid'), array(), '', FALSE);
$modulRepository->expects($this->once())->method('findByUid')->will($this->returnValue($modul));
$this->inject($this->subject, 'modulRepository', $modulRepository);
$request->expects($this->once())->method('getArgument')->will($this->returnValue($this->subject));
$this->inject($this->subject, 'request', $request);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->once())->method('assignMultiple')->with(array(
'newFach' => $fach,
'moduluid' => $modul->getUid(),
'modulname' => $modul->getModulname(),
'modulnummer' => $modul->getModulnr(),
'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
));
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
You are mocking the wrong (inexistent) ObjectManager. The correct namespace is TYPO3\\CMS\\Extbase\\Object\\ObjectManager.
So the correct line should be $objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManager', ...);
Otherwise I wonder why you mock the ObjectManager at all. You don't use it in your method.
One side note: Your code will fail if there is no 'modul' inside the request. Then $modul is not set and you will call getUid(), getModulname(), ... on an non-object.

not able to login using zend framework

I am new to zend. I am trying to create login form using zend framework. But its creating problem. Below is my function
public function loginAction()
{
$db = $this->_getParam('db');
$form = new Application_Form_Login();
$this->view->form = $form;
if($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData))
{
$adapter = new Zend_Auth_Adapter_DbTable(
$db,
'users',
'emailaddress',
'password',
'MD5(CONCAT(?, password_salt))'
);
$adapter->setIdentity($form->getValue('email'));
$adapter->setCredential($form->getValue('password'));
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
$this->_helper->FlashMessenger('Successful Login');
$this->_redirect('/');
return;
}
}
}
}
its giving error on following line -> $result = $auth->authenticate($adapter);
Error is -> Message: The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce a valid sql statement, please check table and column names for validity.
my table name is 'users' and it has columns(id,firstname,lastname,age,emailaddress,password).
You need field named 'password_salt' in your table which will contain salt or just change this
'MD5(CONCAT(?, password_salt))'
to
'MD5(?)'