{"id":46950,"date":"2024-12-02T12:48:50","date_gmt":"2024-12-02T04:48:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46950\/"},"modified":"2024-12-02T12:48:50","modified_gmt":"2024-12-02T04:48:50","slug":"php-%e5%87%bd%e6%95%b0%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e7%9a%84%e8%87%aa%e5%8a%a8%e5%8c%96%e5%b7%a5%e5%85%b7%e9%80%89%e6%8b%a9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46950\/","title":{"rendered":"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9<\/h1>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>PHP \u5355\u5143\u6d4b\u8bd5\u81ea\u52a8\u5316\u5de5\u5177\u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u6613\u7528\u6027\u3001\u8986\u76d6\u8303\u56f4\u3001\u62a5\u544a\u548c\u793e\u533a\u652f\u6301\u7b49\u56e0\u7d20\u3002\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u5de5\u5177\u5305\u62ec\uff1aPHPUnit\uff1a\u5177\u6709\u5e7f\u6cdb\u529f\u80fd\uff0c\u5305\u62ec\u6a21\u62df\u3001\u5b58\u6839\u548c\u65ad\u8a00\u3002Codeception\uff1a\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u7f16\u5199\u6d4b\u8bd5\uff0c\u5e76\u652f\u6301 multiple testing tools\u3002Behat\uff1a\u4e13\u6ce8\u4e8e\u9a8c\u6536\u6d4b\u8bd5\uff0c\u53ef\u7528\u4e8e\u6d4b\u8bd5 API \u548c Web \u5e94\u7528\u7a0b\u5e8f\u3002PHPSpec\uff1a\u57fa\u4e8e\u89c4\u8303\uff0c\u4f7f\u7528\u63cf\u8ff0\u6027\u8bed\u8a00\u6307\u5b9a\u6d4b\u8bd5\u884c\u4e3a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729856676671b84a448388.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u5bf9\u4e8e\u4efb\u4f55\u8f6f\u4ef6\u5f00\u53d1\u751f\u547d\u5468\u671f\u4e2d\u90fd\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u90e8\u5206\u3002\u5b83\u7684\u76ee\u7684\u662f\u786e\u4fdd\u8f6f\u4ef6\u7684\u6bcf\u4e2a\u8f83\u5c0f\u7684\u7ec4\u6210\u90e8\u5206\uff08\u4f8b\u5982\u51fd\u6570\uff09\u6309\u9884\u671f\u5de5\u4f5c\u3002PHP \u6709\u8bb8\u591a\u53ef\u7528\u4f5c\u5355\u5143\u6d4b\u8bd5\u81ea\u52a8\u5316\u7684\u5de5\u5177\u3002<\/p>\n<p><strong>\u9009\u62e9\u5de5\u5177\u7684\u56e0\u7d20<\/strong><\/p>\n<p>\u9009\u62e9 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u65f6\uff0c\u5e94\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<ul>\n<li><strong>\u6613\u7528\u6027\uff1a<\/strong>\u5de5\u5177\u5e94\u6613\u4e8e\u8bbe\u7f6e\u548c\u4f7f\u7528\u3002<\/li>\n<li><strong>\u8986\u76d6\u8303\u56f4\uff1a<\/strong>\u5b83\u5e94\u8be5\u80fd\u591f\u6d4b\u8bd5\u5c3d\u53ef\u80fd\u591a\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>\u62a5\u544a\uff1a<\/strong>\u5b83\u5e94\u8be5\u80fd\u591f\u751f\u6210\u6613\u4e8e\u7406\u89e3\u548c\u89e3\u91ca\u7684\u6d4b\u8bd5\u62a5\u544a\u3002<\/li>\n<li><strong>\u793e\u533a\u652f\u6301\uff1a<\/strong>\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301\u5bf9\u4e8e\u83b7\u5f97\u5e2e\u52a9\u548c\u5206\u4eab\u6700\u4f73\u5b9e\u8df5\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<\/ul>\n<p><strong>\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u5de5\u5177<\/strong><\/p>\n<p><strong>1. PHPUnit<\/strong><\/p>\n<p>\u5b83\u662f PHP \u4e2d\u6700\u53d7\u6b22\u8fce\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u4e4b\u4e00\u3002\u5b83\u5177\u6709\u5e7f\u6cdb\u7684\u529f\u80fd\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u6a21\u62df\u548c\u5b58\u6839<\/li>\n<li>\u65ad\u8a00<\/li>\n<li>\u6570\u636e\u63d0\u4f9b\u8005<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>&lt;?php\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\nclass MyFunctionTest extends \\PHPUnit\\Framework\\TestCase {\n\n    \/\/ \u6d4b\u8bd5\u51fd\u6570\u7684\u529f\u80fd\n    public function testFunction() {\n        $this-&gt;assertEquals(10, my_function(5)); \/\/ \u65ad\u8a00\u671f\u671b\u548c\u5b9e\u9645\u7ed3\u679c\u76f8\u7b49\n    }\n}<\/pre>\n<p><strong>2. Codeception<\/strong><\/p>\n<p>Codeception \u662f\u4e00\u4e2aBDD\uff08\u884c\u4e3a\u9a71\u52a8\u5f00\u53d1\uff09\u6846\u67b6\uff0c\u5b83\u5141\u8bb8\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u6765\u7f16\u5199\u6d4b\u8bd5\u3002\u5b83\u8fd8\u652f\u6301\u591a\u79cd\u5de5\u5177\uff08\u5305\u62ec PHPUnit\uff09\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>&lt;?php\n\n\/\/ \u63cf\u8ff0\u8981\u6d4b\u8bd5\u7684\u529f\u80fd\nFeature: MyFunction\n\n\/\/ \u5b9a\u4e49\u6d4b\u8bd5\u7528\u4f8b\nScenario: Test the function with a number\n    Given a number 5\n    When I call the function\n    Then I should get 10<\/pre>\n<p><strong>3. Behat<\/strong><\/p>\n<p>Behat \u662f\u4e00\u4e2a\u4e13\u6ce8\u4e8e\u63a5\u53d7\u6d4b\u8bd5\u7684 BDD \u6846\u67b6\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u6d4b\u8bd5 API \u548c Web \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>&lt;?php\n\n\/\/ \u5b9a\u4e49\u6d4b\u8bd5\u6b65\u9aa4\nGiven('I have a number 5')\n-&gt;When('I call the function')\n-&gt;Then('I should get 10');<\/pre>\n<p><strong>4. PHPSpec<\/strong><\/p>\n<p>PHPSpec \u662f\u4e00\u4e2a\u57fa\u4e8e\u89c4\u8303\u7684\u6d4b\u8bd5\u6846\u67b6\u3002\u5b83\u5141\u8bb8\u60a8\u4f7f\u7528\u63cf\u8ff0\u6027\u8bed\u8a00\u6765\u6307\u5b9a\u6d4b\u8bd5\u7684\u9884\u671f\u884c\u4e3a\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>&lt;?php\n\n\/\/ \u63cf\u8ff0\u8981\u6d4b\u8bd5\u7684\u529f\u80fd\ndescribe('MyFunction', function() {\n\n    \/\/ \u5b9a\u4e49\u6d4b\u8bd5\u7528\u4f8b\n    it('should return 10 when called with 5', function() {\n        my_function(5)-&gt;should-&gt;be(10);\n    });\n});<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9009\u62e9\u6700\u9002\u5408\u60a8\u9879\u76ee\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u5de5\u5177\u53d6\u51b3\u4e8e\u5177\u4f53\u8981\u6c42\u548c\u504f\u597d\u3002\u901a\u8fc7\u4ed4\u7ec6\u8003\u8651\u4e0a\u9762\u6982\u8ff0\u7684\u56e0\u7d20\uff0c\u60a8\u53ef\u4ee5\u4e3a\u60a8\u7684\u6d4b\u8bd5\u81ea\u52a8\u5316\u627e\u5230\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9 \u5b66\u4e60\u6587\u7ae0\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9\u300b\u5c06\u4f1a\u4ecb\u7ecd\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60\u6587\u7ae0\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01 PHP \u5355\u5143\u6d4b\u8bd5\u81ea\u52a8\u5316\u5de5\u5177\u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u6613\u7528\u6027\u3001\u8986\u76d6\u8303\u56f4\u3001\u62a5\u544a\u548c\u793e\u533a\u652f\u6301\u7b49\u56e0\u7d20\u3002\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u5de5\u5177\u5305\u62ec\uff1aPHPUnit\uff1a\u5177\u6709\u5e7f\u6cdb\u529f\u80fd\uff0c\u5305\u62ec\u6a21\u62df\u3001\u5b58\u6839\u548c\u65ad\u8a00\u3002Codeception\uff1a\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u7f16\u5199\u6d4b\u8bd5\uff0c\u5e76\u652f\u6301 multiple testing tools\u3002Behat\uff1a\u4e13\u6ce8\u4e8e\u9a8c\u6536\u6d4b\u8bd5\uff0c\u53ef\u7528\u4e8e\u6d4b\u8bd5 API \u548c Web \u5e94\u7528\u7a0b\u5e8f\u3002PHPSpec\uff1a\u57fa\u4e8e\u89c4\u8303\uff0c\u4f7f\u7528\u63cf\u8ff0\u6027\u8bed\u8a00\u6307\u5b9a\u6d4b\u8bd5\u884c\u4e3a\u3002 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u9009\u62e9 \u5f15\u8a00 \u5355\u5143\u6d4b\u8bd5\u5bf9\u4e8e\u4efb\u4f55\u8f6f\u4ef6\u5f00\u53d1\u751f\u547d\u5468\u671f\u4e2d\u90fd\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u90e8\u5206\u3002\u5b83\u7684\u76ee\u7684\u662f\u786e\u4fdd\u8f6f\u4ef6\u7684\u6bcf\u4e2a\u8f83\u5c0f\u7684\u7ec4\u6210\u90e8\u5206\uff08\u4f8b\u5982\u51fd\u6570\uff09\u6309\u9884\u671f\u5de5\u4f5c\u3002PHP \u6709\u8bb8\u591a\u53ef\u7528\u4f5c\u5355\u5143\u6d4b\u8bd5\u81ea\u52a8\u5316\u7684\u5de5\u5177\u3002 \u9009\u62e9\u5de5\u5177\u7684\u56e0\u7d20 \u9009\u62e9 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\u65f6\uff0c\u5e94\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a \u6613\u7528\u6027\uff1a\u5de5\u5177\u5e94\u6613\u4e8e\u8bbe\u7f6e\u548c\u4f7f\u7528\u3002 \u8986\u76d6\u8303\u56f4\uff1a\u5b83\u5e94\u8be5\u80fd\u591f\u6d4b\u8bd5\u5c3d\u53ef\u80fd\u591a\u7684\u4ee3\u7801\u3002 \u62a5\u544a\uff1a\u5b83\u5e94\u8be5\u80fd\u591f\u751f\u6210\u6613\u4e8e\u7406\u89e3\u548c\u89e3\u91ca\u7684\u6d4b\u8bd5\u62a5\u544a\u3002 \u793e\u533a\u652f\u6301\uff1a\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301\u5bf9\u4e8e\u83b7\u5f97\u5e2e\u52a9\u548c\u5206\u4eab\u6700\u4f73\u5b9e\u8df5\u975e\u5e38\u91cd\u8981\u3002 \u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u5de5\u5177 1. PHPUnit \u5b83\u662f PHP \u4e2d\u6700\u53d7\u6b22\u8fce\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u4e4b\u4e00\u3002\u5b83\u5177\u6709\u5e7f\u6cdb\u7684\u529f\u80fd\uff0c\u5305\u62ec\uff1a \u6a21\u62df\u548c\u5b58\u6839 \u65ad\u8a00 \u6570\u636e\u63d0\u4f9b\u8005 \u793a\u4f8b\u4ee3\u7801\uff1a &lt;?php \/\/ \u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b class MyFunctionTest extends \\PHPUnit\\Framework\\TestCase { \/\/ \u6d4b\u8bd5\u51fd\u6570\u7684\u529f\u80fd public function testFunction() { $this-&gt;assertEquals(10, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-46950","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=46950"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46950\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}