{"id":202487,"date":"2025-05-13T09:29:41","date_gmt":"2025-05-13T01:29:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202487\/"},"modified":"2025-05-13T09:29:41","modified_gmt":"2025-05-13T01:29:41","slug":"php%e5%87%bd%e6%95%b0%e6%b5%8b%e8%af%95%e4%b8%8e%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%a6%82%e4%bd%95%e8%b0%83%e8%af%95web%e6%9c%8d%e5%8a%a1%e9%97%ae%e9%a2%98%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202487\/","title":{"rendered":"php\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6280\u5de7\uff1a\u5982\u4f55\u8c03\u8bd5Web\u670d\u52a1\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6280\u5de7\uff1a\u5982\u4f55\u8c03\u8bd5Web\u670d\u52a1\u95ee\u9898\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6280\u5de7\uff1a\u5982\u4f55\u8c03\u8bd5Web\u670d\u52a1\u95ee\u9898\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p>PHP \u51fd\u6570\u6d4b\u8bd5\u548c\u8c03\u8bd5\u6280\u5de7\u5305\u62ec\uff1a\u5355\u5143\u6d4b\u8bd5\uff08\u4f8b\u5982 PHPUnit\uff09\uff1a\u5355\u72ec\u6d4b\u8bd5\u51fd\u6570\u7684\u884c\u4e3a\u3002\u96c6\u6210\u6d4b\u8bd5\uff08\u4f8b\u5982 Codeception\uff09\uff1a\u6d4b\u8bd5\u591a\u51fd\u6570\u534f\u540c\u5de5\u4f5c\u3002Xdebug\uff1a\u9010\u6b65\u6267\u884c\u4ee3\u7801\u5e76\u68c0\u67e5\u53d8\u91cf\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729838965671b3f75ec006.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6280\u5de7\uff1a\u5982\u4f55\u8c03\u8bd5 Web \u670d\u52a1\u95ee\u9898\uff1f<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728 Web \u5f00\u53d1\u4e2d\uff0cPHP \u51fd\u6570\u7684\u6b63\u786e\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u672a\u80fd\u53ca\u65f6\u68c0\u6d4b\u548c\u89e3\u51b3\u51fd\u6570\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e25\u91cd\u7684\u9519\u8bef\u548c\u5b89\u5168\u6f0f\u6d1e\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u6709\u6548\u7684 PHP \u51fd\u6570\u6d4b\u8bd5\u548c\u8c03\u8bd5\u6280\u5de7\uff0c\u4ee5\u5e2e\u52a9\u4f60\u627e\u51fa\u5e76\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u79cd\u6709\u6548\u7684\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u53ef\u5355\u72ec\u6d4b\u8bd5\u5355\u4e2a\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u9884\u671f\u884c\u4e3a\u3002PHPUnit \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u5b83\u5141\u8bb8\u4f60\u7f16\u5199\u65ad\u8a00\u6765\u9a8c\u8bc1\u51fd\u6570\u7684\u8f93\u51fa\u662f\u5426\u7b26\u5408\u9884\u671f\u3002\u4ee5\u4e0b\u662f PHPUnit \u6d4b\u8bd5\u793a\u4f8b\uff1a<\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\n\nclass MyFunctionTest extends TestCase\n{\n    public function testFunction()\n    {\n        $result = myFunction(10);\n        $expected = 20;\n        $this-&gt;assertEquals($expected, $result);\n    }\n}<\/pre>\n<p><strong>\u96c6\u6210\u6d4b\u8bd5<\/strong><\/p>\n<p>\u96c6\u6210\u6d4b\u8bd5\u6d89\u53ca\u6d4b\u8bd5\u591a\u4e2a\u51fd\u6570\u6216\u6a21\u5757\u534f\u540c\u5de5\u4f5c\u7684\u771f\u5b9e\u573a\u666f\u3002\u5b83\u6709\u52a9\u4e8e\u53d1\u73b0\u8de8\u7ec4\u4ef6\u7684\u4ea4\u4e92\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 Codeception \u6846\u67b6\u7f16\u5199\u96c6\u6210\u6d4b\u8bd5\uff1a<\/p>\n<pre>&lt;?php\nuse Codeception\\Test\\Unit;\n\nclass IntegrationTest extends Unit\n{\n    public function testIntegration()\n    {\n        $this-&gt;sendGET('\/api\/users');\n        $this-&gt;seeResponseCodeIs(200);\n        $this-&gt;seeResponseContainsJson(['name' =&gt; 'John Doe']);\n    }\n}<\/pre>\n<p><strong>Xdebug<\/strong><\/p>\n<p>Xdebug \u662f\u4e00\u4e2a\u6269\u5c55\uff0c\u53ef\u8ba9\u4f60\u9010\u6b65\u6267\u884c PHP \u4ee3\u7801\u5e76\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u4f8b\u5982\u65ad\u70b9\u3001\u5806\u6808\u8ddf\u8e2a\u548c\u53d8\u91cf\u68c0\u67e5\u3002\u8981\u5728 Xdebug \u4e2d\u8c03\u8bd5\u51fd\u6570\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c <code>xdebug_start_trace()<\/code>\u3002<\/li>\n<li>\u8fd0\u884c\u4f60\u7684\u4ee3\u7801\u3002<\/li>\n<li>\u6d4f\u89c8\u751f\u6210\u7684 trace \u6587\u4ef6\u4ee5\u67e5\u770b\u51fd\u6570\u7684\u6267\u884c\u6b65\u9aa4\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e0b\u8c03\u8bd5\u4e00\u4e2a\u8ba1\u7b97\u7528\u6237\u8ba2\u5355\u603b\u4ef7\u7684 <code>calculateOrderTotal<\/code> \u51fd\u6570\u7684\u793a\u4f8b\u3002\u51fd\u6570\u9884\u671f\u5c06\u5546\u54c1\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u8fd4\u56de\u603b\u4ef7\u3002<\/p>\n<pre>function calculateOrderTotal($items)\n{\n    $total = 0;\n    foreach ($items as $item) {\n        $total += $item['price'] * $item['quantity'];\n    }\n    return $total;\n}<\/pre>\n<p><strong>\u4f7f\u7528 PHPUnit \u6d4b\u8bd5<\/strong><\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\n\nclass CalculateOrderTotalTest extends TestCase\n{\n    public function testEmptyItems()\n    {\n        $total = calculateOrderTotal([]);\n        $this-&gt;assertEquals(0, $total);\n    }\n\n    public function testValidItems()\n    {\n        $items = [\n            ['price' =&gt; 10, 'quantity' =&gt; 2],\n            ['price' =&gt; 20, 'quantity' =&gt; 1],\n        ];\n        $total = calculateOrderTotal($items);\n        $this-&gt;assertEquals(40, $total);\n    }\n}<\/pre>\n<p><strong>\u4f7f\u7528 Xdebug \u8c03\u8bd5<\/strong><\/p>\n<pre>xdebug_start_trace();\ncalculateOrderTotal(...);<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6280\u5de7\uff1a\u5982\u4f55\u8c03&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202487","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202487"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202487\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}