A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearHow does one unit test the addition of a row to a database with Zend Framework & PHPUnit? - page 1
User InfoPosts
How does one unit test the addition of a row to a database with Zend Framework & PHPUnit?#1
To test inserting/update of a row, I(ve written a test to simulate a post, to check that it redirects, and to check that the newly inserted/updated text is on the page. The code works, but not the test - can you tell me why it(s wrong?

public function testEditProduct() {
$request = $this->getRequest();
$request->setMethod((POST();
$request->setPost(array(
(id(=>(1(,
(title(=>(Test Product 1a(
));
$this->dispatch((/product/edit/id/1();
$this->assertRedirectTo((/();
$this->assertQueryContentContains((a(, (Test Product 1a();
}
public function testAddProduct() {
$request = $this->getRequest();
$request->setMethod((POST();
$request->setPost(array(
(title(=>(Test Product 3(
));
$this->dispatch((/product/add/();
$this->assertRedirectTo((/();
$this->assertQueryContentContains((a(, (Test Product 3();
}


The following tests both work, asserting that the index page with an ID parameter contains the appropriate text, and that after deleting a product that product(s title is no longer displayed on the page.

public function testIndexPageListsProducts() {
$this->dispatch((/product/index/id/1();
$this->assertQueryContentContains((h1(, (Test Product 1();
}

public function testDeleteProduct() {
$request = $this->getRequest();
$request->setMethod((POST();
$this->dispatch((/product/delete/id/2();
$this->assertRedirectTo((/();
$this->assertNotQueryContentContains((a(, (Test Product 2();
}

posted date: 2009-04-13 10:24:00


Re: How does one unit test the addition of a row to a database with Zend Framework & PHPUnit?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-13 10:24:01


Re: How does one unit test the addition of a row to a database with Zend Framework & PHPUnit?#3
In your first test, change $this->assertRedirectTo((/();$this->assertQueryContentContains((a(, (Test Product 3();to $this->assertRedirectTo((/();$this->resetRequest();$this->resetResponse();$this->disptach((/();$this->assertQueryContentContains((a(, (Test Product 3();If I(m correct, the unit testing framework does not execute redirects. It keeps track of which redirects were requested, but does not actually do them. So in your code, you(re checking for the presence of (Test Product 3( on the edit view/action, and that test fails of course. You should first request the index, and then do the test.

posted date: 2009-04-14 07:07:00


Re: How does one unit test the addition of a row to a database with Zend Framework & PHPUnit?#4
thanks for your help. I found that $this->resetRequest(); and $this->resetResponse(); threw errors, but I checked the method summary for Zend_Test_PHPUnit_ControllerTestCase and found $this->reset(); The linked page does list the methods you mention, but they(re listed differently.$this-reset(); alone also threw an error (No default module defined for this application), but I found that if I called $this->bootstrap(); afterwards the tests passed. Is this a valid solution? For clarify, my test code is now:public function testEditProduct() {$request = $this->getRequest();$request->setMethod((POST();$request->setPost(array((id(=>(1(,(title(=>(Test Product 1a());$this->dispatch((/product/edit/id/1();$this->assertRedirectTo((/();$this->reset();$this->bootstrap();$this->dispatch((/();$this->assertQueryContentContains((a(, (Test Product 1a();}public function testAddProduct() {$request = $this->getRequest();$request->setMethod((POST();$request->setPost(array((title(=>(Test Product 3());$this->dispatch((/product/add/();$this->assertRedirectTo((/();$this->reset();$this->bootstrap();$this->dispatch((/();$this->assertQueryContentContains((a(, (Test Product 3();}

posted date: 2009-04-16 05:09:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.