ZF3 unittest PRG issue

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…

  1. What is the error you are getting or what is (is not) happening?
  2. What is hiding behind prg()? Is this a local method or is it something injected through the constructor?

This is an official zend-mvc controller plugin: GitHub - zendframework/zend-mvc-plugin-prg: Post/Redirect/Get controller plugin for zend-mvc

Oops… just learned something new :blush:

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:

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

Wait wait!

  1. Use ask your question on Stackoverflow, here in the forum and now also in an old issue on GitHub. Please stay on one channel.
  2. The problem on GitHub was related to “Zend Server” with “Z-Ray” and has nothing to do with your problem. Maybe the same result, but not the same cause.
  3. I gave you the hint that the status code 303 is not an error code. Please recheck this.
  1. 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.

  2. I am facing same and for the reference i shared the link where i am using zend framework 3 and Apache

  3. 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

Only same page (route) if the first parameter for the plugin is not set.

Not “second redirect” – on calling with a GET request.

See:

The title of the plugin already contains the behaviour: “PRG” – “Post-Redirect-Get”

See also at Wikipedia: Post/Redirect/Get - Wikipedia

Thank you :slight_smile:

Check if you can write cookies (Cookiewall for instance). Otherwise no session can be stored.