When i am posting data from test script to controller in below way
> $this->getRequest()
->setMethod('POST')
->setPost(new Parameters($_POST));
$this->dispatch('/default/register');
$this->assertModuleName('default');
//assertActionName
$this->assertResponseStatusCode(200);
$this->assertControllerName(MyDefaultController::class);
$this->assertControllerClass('MyDefaultController');
$this->assertActionName('register');
$this->assertMatchedRouteName('register')
and the MyDefaultController below code is not working. Giving this code name #sample1
> $prg = $this->prg();
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
return $prg;
} elseif ($prg === false) {
return $this->redirect()->toRoute('widget/login');
}
$this->registerForm->setData($prg);
I edit the above prg code this way which is working
if($this->getRequest()->isPost()) {
$this->registerForm->setData($this->params()->fromPost());
}
I wish run the code in prg way (name as #sample1 ), please suggest how can i achieve this.
I have posted same on here php - PHPUnit - Zend framework 3 - testing stubs that redirect - Stack Overflow
I have 2 questions for you…
What is the error you are getting or what is (is not) happening?
What is hiding behind prg()
? Is this a local method or is it something injected through the constructor?
Oops… just learned something new
I am getting 303 error code, actually i am writing unittest in zf3 and posting data as POST METHOD to controller
PRG is working fine while posting data via browser but same is not working when i am executing the data on command prompt
This not an error code. 3xx is for redirections and therefore correct.
See:
This is a list of Hypertext Transfer Protocol (HTTP) response status codes. Status codes are issued by a server in response to a client's request made to the server. It includes codes from IETF Request for Comments (RFCs), other specifications, and some additional codes used in some common applications of the HTTP. The first digit of the status code specifies one of five standard classes of responses. The optional message phrases shown are typical, but any human-readable alternative may be prov...
if (false === $redirectToUrl) {
$response = $redirector->toRoute($redirect, $params, $options, $reuseMatchedParams);
$response->setStatusCode(303);
return $response;
}
// Redirect to specific URL
$response = $redirector->toUrl($redirect);
$response->setStatusCode(303);
return $response;
Actually, My Question was why the PRG is not working while testing on command line
that i have posted as my intial Question - same has posted here
Please help
I asked on the on stack before here
https://stackoverflow.com/questions/54023659/zf3-unittest-prg-issue
no answer thought have to find more expert so here i am.
I am facing same and for the reference i shared the link where i am using zend framework 3 and Apache
Ok agree 303 is not error - stick with you.
It will helpful if i get solution on my posted #1
Thaks
You test on the response code 200, this should be checked. If the prg plugin creates a redirect, then 200 is wrong.
yes prg is redirecting that is fine on browser case but on CLI its issue, can you please let me know how can i mange PRG in case of CLI
What is your goal on CLI? Which behaviour do you expect on CLI?
I want to print the POST data , so
$prg = $this->prg();
I am getting this POST data - below login return 303 in case of CLI while POST data in terms of Browser POST
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
return $prg;
} elseif ($prg === false) {
return $this->redirect()->toRoute(‘widget/login’);
}
Getthing $_POST data her in Browser case but CLI is not returning POST if $prg
CLI should work as like browser - I want know the reson why CLI and browser is not same
If the request method is POST
then the plugin stores all values in a session container and will redirect. The data of the POST
request will not retrieved on this (first) request.
You must start another request (GET
) to get the POST
data that is included the session.
oh that mean the PRG behaviout is like
FIRST POST data, that POST store in session and redirect again on the same page
Second redirect will be as GET and all the POST data fetch from the session as post
Please confirm
Mukesh_Chaudhari:
FIRST POST data, that POST store in session and redirect again on the same page
Only same page (route) if the first parameter for the plugin is not set.
Mukesh_Chaudhari:
Second redirect will be as GET and all the POST data fetch from the session as post
Not “second redirect” – on calling with a GET
request.
See:
public function __invoke($redirect = null, $redirectToUrl = false)
{
$controller = $this->getController();
$request = $controller->getRequest();
$container = $this->getSessionContainer();
if ($request->isPost()) {
$container->setExpirationHops(1, 'post');
$container->post = $request->getPost()->toArray();
return $this->redirect($redirect, $redirectToUrl);
}
if (null !== $container->post) {
$post = $container->post;
unset($container->post);
return $post;
}
return false;
}
The title of the plugin already contains the behaviour: “PRG” – “Post-Redirect-Get”
See also at Wikipedia: Post/Redirect/Get - Wikipedia
Check if you can write cookies (Cookiewall for instance). Otherwise no session can be stored.