ThinkPHP6ÖÐÔõÑù¾ÙÐнӿڲâÊÔ£¿
Ëæ×Å»¥ÁªÍøÊÖÒյĸßËÙÉú³¤£¬½Ó¿Ú²âÊÔÔ½À´Ô½³ÉΪÁËÈí¼þ¿ª·¢Àú³ÌÖбز»¿ÉÉÙµÄÒ»»·¡£thinkphp6ÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ÔÚ¾ÙÐнӿڲâÊÔʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃphpunit²âÊÔ¿ò¼Ü¾ÙÐвâÊÔ¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùÔÚthinkphp6ÖоÙÐнӿڲâÊÔ£¬ÈÃÄúÄܹ»Ô½·¢Àû±ãµØ¾ÙÐвâÊÔÊÂÇé¡£
Ò»¡¢×°ÖÃPHPUnit
ÓÉÓÚPHPUnitÊÇÒ»¿îµÚÈý·½²âÊÔ¿ò¼Ü£¬Òò´ËÎÒÃÇÐèÒªÏÈ×°ÖÃPHPUnit¡£ÎÒÃÇ¿ÉÒÔͨ¹ý×°ÖÃComposerÀ´×°ÖÃPHPUnit£º
·¿ªÖն˻òÏÂÁîÐнçÃ棬ÊäÈëÒÔÏÂÏÂÁî×°ÖÃComposer£º
$ curl -sS https://getcomposer.org/installer | php
µÇ¼ºó¸´ÖÆ
×°ÖÃPHPUnit£º
$ php composer.phar require phpunit/phpunit
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃǾͿÉÒÔ×îÏȾÙÐнӿڲâÊÔ¡£
¶þ¡¢±àд²âÊÔÓÃÀý
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔÔÚtestsÎļþ¼ÐÖбàд²âÊÔÓÃÀý¡£ÏÂÃæÎÒÃÇÏȽ¨ÉèÒ»¸ö²âÊÔÓÃÀýÎļþ¼Ð£º
$ mkdir tests/TestCase
µÇ¼ºó¸´ÖÆ
½Ó×Å£¬ÔÚTestCaseÎļþ¼ÐϽ¨ÉèÒ»¸ö²âÊÔÓÃÀýÎļþApiTest.php£º
$ touch tests/TestCase/ApiTest.php
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔÔÚApiTest.phpÎļþÖбàдһ¸ö¼òÆӵĽӿڲâÊÔÓÃÀý¡£¼ÙÉèÎÒÃÇÒª²âÊԵĽӿÚÊÇ /api/user/info£¬·µ»ØÓû§µÄһЩÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔ±àдÒÔϲâÊÔÓÃÀý£º
<?php namespace testsTestCase; use PHPUnitFrameworkTestCase; class ApiTest extends TestCase { public function testGetUserInfo() { $url = 'http://localhost/api/user/info'; $response = file_get_contents($url); $this->assertStringContainsString('user_name', $response); $this->assertStringContainsString('user_email', $response); } }
µÇ¼ºó¸´ÖÆ
Õâ¸ö²âÊÔÓÃÀýÖУ¬ÎÒÃÇʹÓÃÁËPHPUnit×Ô´øµÄassertStringContainsStringÒªÁìÀ´¼ì²é·µ»ØµÄÓû§ÐÅÏ¢ÖÐÊÇ·ñ°üÀ¨ user_name ºÍ user_email ÕâÁ½¸ö×ֶΡ£ÈôÊÇÕâÁ½¸ö×ֶζ¼±£´æ£¬²âÊԾͻáͨ¹ý¡£
Èý¡¢ÉèÖòâÊÔÇéÐÎ
ÔÚ¾ÙÐнӿڲâÊÔ֮ǰ£¬ÎÒÃÇÐèÒªÏÈÉèÖòâÊÔÇéÐΡ£½ÓÏÂÀ´£¬ÎÒÃÇÒÔÉèÖòâÊÔÇéÐÎΪÀý£¬ÏêϸÏÈÈÝÔõÑùÔÚThinkPHP6ÖÐÉèÖòâÊÔÇéÐΡ£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ötestÊý¾Ý¿â£¬ÓÃÓÚ´æ´¢²âÊÔÊý¾Ý¡£ÎÒÃÇ¿ÉÒÔÔÚMySQLÖÐÖ´ÐÐÒÔÏÂÏÂÁîÀ´½¨ÉèÊý¾Ý¿â£º
$ mysql -u root -p mysql> CREATE DATABASE test;
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇÐèÒªÐÞ¸ÄÊý¾Ý¿âÉèÖÃÎļþ£¬½«Êý¾Ý¿âÅþÁ¬ÐÅÏ¢ÉèÖóÉÎÒÃǸոս¨ÉèµÄÊý¾Ý¿âÅþÁ¬ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔÔÚconfig/database.phpÎļþÖÐÐÞ¸ÄÊý¾Ý¿âÐÅÏ¢£º
return [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => '127.0.0.1', // Êý¾Ý¿âÃû 'database' => 'test', // Óû§Ãû 'username' => 'root', // ÃÜÂë 'password' => '', // ¶Ë¿Ú 'hostport' => '', // Êý¾Ý¿âÅþÁ¬²ÎÊý 'params' => [], // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8 'charset' => 'utf8', // Êý¾Ý¿â±íǰ׺ 'prefix' => '', // Êý¾Ý¿âµ÷ÊÔģʽ 'debug' => true, // Êý¾Ý¿â°²ÅÅ·½·¨:0 ¼¯ÖÐʽ(¼òµ¥Ð§ÀÍÆ÷),1 ÂþÑÜʽ(Ö÷´ÓЧÀÍÆ÷) 'deploy' => 0, // Êý¾Ý¿â¶ÁдÊÇ·ñÊèÉ¢ Ö÷´ÓʽÓÐÓà 'rw_separate' => false, // ¶ÁдÊèÉ¢ºó Ö÷ЧÀÍÆ÷ÊýÄ¿ 'master_num' => 1, // Ö¸¶¨´ÓЧÀÍÆ÷ÐòºÅ 'slave_no' => '', // ÊÇ·ñÑÏ¿á¼ì²é×Ö¶ÎÊÇ·ñ±£´æ 'fields_strict' => true, // ×Ô¶¯Ð´Èëʱ¼ä´Á×ֶΠ'auto_timestamp' => false, // ʱ¼ä×Ö¶ÎÈ¡³öºóµÄĬÈÏʱ¼äÃûÌà 'datetime_format' => 'Y-m-d H:i:s', // ÊÇ·ñÐèÒª¾ÙÐÐSQLÐÔÄÜÆÊÎö 'sql_explain' => false, ];
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÔÚphpunit.xmlÎļþÖÐÌí¼ÓÈçÏÂÄÚÈÝ£º
<!-- Êý¾Ý¿âÉèÖà --> <php> <env name="DB_TYPE" value="mysql" /> <env name="DB_HOST" value="127.0.0.1" /> <env name="DB_NAME" value="test" /> <env name="DB_USER" value="root" /> <env name="DB_PASS" value="" /> </php>
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾͿÉÒÔÔÚ²âÊÔÇéÐÎÖÐʹÓÃtestÊý¾Ý¿âÀ´¾ÙÐвâÊÔÁË¡£
ËÄ¡¢ÔËÐвâÊÔ
ÔÚ²âÊÔÇéÐÎÉèÖúÃÖ®ºó£¬ÎÒÃǾͿÉÒÔÔËÐвâÊÔÁË¡£ÎÒÃÇ¿ÉÒÔÔÚÏÂÁîÐнçÃæÖнøÈëÏîÄ¿¸ùĿ¼£¬ÊäÈëÒÔÏÂÏÂÁîÀ´ÔËÐвâÊÔ£º
$ ./vendor/bin/phpunit tests/TestCase/ApiTest.php
µÇ¼ºó¸´ÖÆ
ÈôÊDzâÊÔÓÃÀýÔËÐÐÀֳɣ¬»áÊä³öÒÔÏÂÐÅÏ¢£º
PHPUnit 9.5.2 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 00:00.012, Memory: 6.00 MB OK (1 test, 2 assertions)
µÇ¼ºó¸´ÖÆ
Õâ¾Í˵Ã÷ÎÒÃǵĽӿڲâÊÔÓÃÀýÒѾͨ¹ýÁ˲âÊÔ¡£ÈôÊDzâÊÔÇ·ºà¹ý£¬PHPUnit»áÊä³öÏà¹Ø¹ýʧÐÅÏ¢£¬ÎÒÃÇ¿ÉÒÔƾ֤¹ýʧÐÅÏ¢¾ÙÐÐÐÞ¸´¡£
Îå¡¢×ܽá
±¾ÎÄÏêϸÏÈÈÝÁËÔõÑùÔÚThinkPHP6ÖоÙÐнӿڲâÊÔ£¬´Ó×°ÖÃPHPUnitµ½±àд²âÊÔÓÃÀý£¬ÔÙµ½ÉèÖòâÊÔÇéÐκÍÔËÐвâÊÔ£¬¶¼¾ÙÐÐÁËÖðÒ»½â˵¡£Ï£Íû±¾ÎÄÄܹ»×ÊÖúµ½ÓÐÐèÒªµÄ¶ÁÕߣ¬Èø÷ÈËÔÚ¾ÙÐнӿڲâÊÔʱԽ·¢ÇáËÉÓä¿ì¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐнӿڲâÊÔ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡