PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT' );
PHPUnit_TextUI_Command::main();
* This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- <? php
- abstract class Ext_Db_Table_Test_Abstract extends PHPUnit_Framework_TestCase
- {
- / **
- * @var Zend_Db_Adapter_Abstract
- * /
- protected static $ _db;
- public static function setDbAdapter (Zend_Db_Adapter_Abstract $ db = null )
- {
- if (empty ($ db)) {
- $ db = Zend_Db_Table_Abstract :: getDefaultAdapter ();
- }
- self :: $ _ db = $ db;
- }
- / **
- * Zend_Db_Profiler
- *
- * @return Zend_Db_Profiler
- * /
- public function getProfiler ()
- {
- return self :: $ _ db-> getProfiler ();
- }
- final public function setUp ()
- {
- if (empty (self :: $ _ db)) {
- self :: setDbAdapter ();
- }
- self :: $ _ db-> beginTransaction (); // Each test in its transaction!
- $ this -> _ start ();
- }
- final public function tearDown ()
- {
- $ this -> _ finish ();
- self :: $ _ db-> rollBack ();
- }
- protected function _start ()
- {
- }
- protected function _finish ()
- {
- }
- }
* This source code was highlighted with Source Code Highlighter .
- public function addArticle (array $ data = array ())
- {
- $ base = array (
- 'key' => md5 (mt_rand ()),
- 'content' => md5 (mt_rand ()),
- 'name' => md5 (mt_rand ()),
- 'published' => 1,
- 'file_id' => $ this -> addFile () -> file_id, // Create a dependent object
- 'created' => now ()
- );
- $ article_table = Article :: getInstance ();
- if (empty ($ data [ 'article_category_id' ])) { // If the category id is not passed, the category is automatically created
- $ base [ 'article_category_id' ] = $ this -> addArticleCategory ($ data) -> article_category_id; // The $ data array is passed to the method that creates the category.
- }
- return $ this -> _ createRow ($ article_table, $ base , $ data);
- }
- protected function _createRow (Ext_Db_Table_Abstract $ table, array $ base , array $ data = array ())
- {
- $ data = array_merge ($ base , $ data);
- $ row = $ table-> createRow ($ data);
- $ row-> save ();
- return $ row;
- }
* This source code was highlighted with Source Code Highlighter .
- <? php
- class ArticleTest extends Ext_Db_Table_Test_Abstract
- {
- public function testFindByKey ()
- {
- $ article = Test_Object :: getInstance () -> addArticle ();
- $ row = Article :: getInstance () -> findByKey ($ article-> key);
- $ this -> assertEquals ($ article, $ row);
- }
- }
php run.php model/
PHPUnit 3.3.12 by Sebastian Bergmann.
E........................................................... 60 / 356
............................................................ 120 / 356
............................................................ 180 / 356
F..........................................................I 240 / 356
............................................................ 300 / 356
........................................................
Time: 42 seconds
FAILURES!
Tests: 356, Assertions: 800, Failures: 1, Errors: 1, Incomplete: 1.
* This source code was highlighted with Source Code Highlighter .
- <? php
- class Frontend_Tender_EditControllerTest extends ControllerTestCase // Class in which the acl environment, routing, etc. is raised
- {
- public function testAddAction () // Verify that the page is opened.
- {
- $ this -> dispatch ( '/ tender / add' );
- $ this -> assertNoErrors (); // Verifies that the plugin error did not register any errors.
- $ this -> assertModuleFromParams ( 'tender' );
- $ this -> assertControllerFromParams ( 'edit' );
- $ this -> assertActionFromParams ( 'add' );
- }
- public function testAddActionWithPost () // Check the form
- {
- $ data = array (
- 'name' => 'name' ,
- 'content' => md5 (mt_rand ()),
- 'phone' => '12345' ,
- 'country_id' => 3159,
- 'region_id' => 4312,
- 'city_id' => 4400,
- 'email' => md5 (mt_rand ()). '@ testemail.ru' ,
- );
- $ this -> getRequest () -> setMethod ( 'post' )
- -> setParams ($ data);
- $ this -> dispatch ( '/ tender / add' );
- $ this -> assertNoErrors ();
- $ table = Tender :: getInstance ();
- $ row = $ table-> selectByEmail ($ data [ 'email' ]) -> fetchRow (); // I will tell about it in the next topic)
- foreach ($ data as $ key => $ value) {
- $ this -> assertEquals ($ value, $ row -> $ key); // Check that our data is in the database
- }
- }
- }
Source: https://habr.com/ru/post/72716/
All Articles