开发者

How to stub data for PHPUnit with plugin using Zend_Auth that redirects on failed authentication

开发者 https://www.devze.com 2023-01-20 21:39 出处:网络
I\'m still a newb to phpunit so I hope to be explaining this correctly. I\'m trying to run a controller test with PHPUnit, however the plugin that is using Zend_Auth is redirecting back to the login.

I'm still a newb to phpunit so I hope to be explaining this correctly. I'm trying to run a controller test with PHPUnit, however the plugin that is using Zend_Auth is redirecting back to the login. How do I create the stub data? I tried placing it in my setUp but Zend_Auth appears to be reinstantiated after setUp. 开发者_StackOverflowThanks for any help


You just have to authenticate in your testcase manually. Say, if you have a custom class My_Auth_Adapter which implements Zend_Auth_Adapter_Interface, you can do something like this:

// authentication - you can place it in setUp() method if you want
$adapter = new My_Auth_Adapter('someusername', 'somepassword');
Zend_Auth::getInstance()->authenticate($adapter);

// checking if it's successful
$this->assertTrue(Zend_Auth::getInstance()->hasIdentity());

// testing your controller code
$this->dispatch('/some/url');
$this->assertController('some');
$this->assertAction('url');
$this->assertXpath(/*some element on your page*/);
// and so on


I like how Wallgate used a custom Adapter pre-populate the login. Will give that a try later. Right now, i use this.

    // login as a valid user
    $this->request
         ->setMethod('POST')
         ->setPost(array(
             'username' => 'foobar',
             'password' => 'foobar'
         ));
    $this->dispatch('/user/login');

    // to make another request, we need to reset both request and response
    $this->resetRequest();
    $this->resetResponse();
    $this->dispatch('user/contactdetail');

The last line results in a successful logged in page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号