<p>I would like to know how to write PHPUnit tests with Zend_Test and in general with PHP.</p>

[{'answer_id': 66008, 'author': 'Sam Corder', 'author_id': 2351, 'author_profile': 'https://Stackoverflow.com/users/2351', 'pm_score': 0, 'selected': False, 'text': "<p>I haven't used Zend_Test but I have written tests against apps using Zend_MVC and the like.  The biggest part is getting enough of your bootstrap code in your test setup.</p>\n"}, {'answer_id': 70082, 'author': 'Stefan Gehrig', 'author_id': 11354, 'author_profile': 'https://Stackoverflow.com/users/11354', 'pm_score': 4, 'selected': False, 'text': "<p>I'm using Zend_Test to completely test all controllers. It's quite simple to set up, as you only have to set up your bootstrap file (the bootstrap file itself should NOT dispatch the front controller!). My base test-case class looks like this:</p>\n\n<pre><code>abstract class Controller_TestCase extends Zend_Test_PHPUnit_ControllerTestCase\n{\n    protected function setUp()\n    {\n        $this-&gt;bootstrap=array($this, 'appBootstrap');\n        Zend_Auth::getInstance()-&gt;setStorage(new Zend_Auth_Storage_NonPersistent());\n        parent::setUp();\n    }\n\n    protected function tearDown()\n    {\n        Zend_Auth::getInstance()-&gt;clearIdentity();\n    }\n\n    protected function appBootstrap()\n    {\n        Application::setup();\n    }\n}\n</code></pre>\n\n<p>where <code>Application::setup();</code> does all the setup up tasks which also set up the real application. A simple test then would look like this:</p>\n\n<pre><code>class Controller_IndexControllerTest extends Controller_TestCase\n{\n    public function testShowist()\n    {\n        $this-&gt;dispatch('/');\n        $this-&gt;assertController('index');\n        $this-&gt;assertAction('list');\n        $this-&gt;assertQueryContentContains('ul li a', 'Test String');\n    }\n}\n</code></pre>\n\n<p>That's all...</p>\n"}, {'answer_id': 182913, 'author': 'blee', 'author_id': 9290, 'author_profile': 'https://Stackoverflow.com/users/9290', 'pm_score': 3, 'selected': False, 'text': '<p>They have an "<a href="http://devzone.zend.com/1115/an-introduction-to-the-art-of-unit-testing-in-php/" rel="nofollow noreferrer">Introduction to the Art of Unit Testing</a>" on the Zend Developer Zone, which covers PHPUnit.</p>\n'}, {'answer_id': 572827, 'author': 'Josef Sábl', 'author_id': 53864, 'author_profile': 'https://Stackoverflow.com/users/53864', 'pm_score': 2, 'selected': False, 'text': '<p>I found <a href="http://weierophinney.net/matthew/archives/190-Setting-up-your-Zend_Test-test-suites.html" rel="nofollow noreferrer">this</a> article very useful. Also <a href="http://framework.zend.com/manual/en/zend.test.html" rel="nofollow noreferrer">Zend_Test</a> documentation helped a lot.\nWith the help of these two resources, I managed to succesfully implement unit testing in the <a href="http://framework.zend.com/docs/quickstart" rel="nofollow noreferrer">QuickStart tutorial</a> of the Zend Framework and write few tests for it.</p>\n'}, {'answer_id': 2399785, 'author': 'Alex', 'author_id': 288568, 'author_profile': 'https://Stackoverflow.com/users/288568', 'pm_score': 1, 'selected': False, 'text': '<p>Using ZF 1.10, I put some bootstrap code into tests/bootstrap.php (basically what is in (public/index.php), until $application->bootstrap().</p>\n\n<p>Then I am able to run a test using</p>\n\n<pre><code>phpunit --bootstrap ../bootstrap.php  PersonControllerTest.php \n</code></pre>\n'}, {'answer_id': 5709984, 'author': 'KdPurvesh', 'author_id': 708903, 'author_profile': 'https://Stackoverflow.com/users/708903', 'pm_score': 0, 'selected': False, 'text': '<p>Plus if you using a database transaction then it would be best to delete all the transaction that is gets done via a unit test otherwise your database gets all messed.</p>\n\n<p>so on set up </p>\n\n<pre><code>public function setUp() {\n\n\n\n    YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;setUnitTestMode(true);\n\n\n\n    YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;query("BEGIN");\n\n    YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;getCache()-&gt;clear();\n\n    // Manually Start a Doctrine Transaction so we can roll it back\n    Doctrine_Manager::connection()-&gt;beginTransaction();\n}\n</code></pre>\n\n<p>and on teardown all you need to do is rollback </p>\n\n<pre><code>public function tearDown() {\n\n\n\n    // Rollback Doctrine Transactions\n    while (Doctrine_Manager::connection()-&gt;getTransactionLevel() &gt; 0) {\n        Doctrine_Manager::connection()-&gt;rollback();\n    }\n\n    Doctrine_Manager::connection()-&gt;clear();\n\n\n\n    YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;query("ROLLBACK");\n    while (YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;getTransactionDepth() &gt; 0) {\n        YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;rollback();\n    }\n    YOUR_ZEND_DB_INSTANCE::getInstance()-&gt;setUnitTestMode(false);\n\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/65683', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']