{"id":47481,"date":"2024-12-02T13:19:02","date_gmt":"2024-12-02T05:19:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47481\/"},"modified":"2024-12-02T13:19:02","modified_gmt":"2024-12-02T05:19:02","slug":"php-%e5%87%bd%e6%95%b0%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e6%a1%86%e6%9e%b6%e7%9a%84%e4%bc%98%e5%8a%a3%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47481\/","title":{"rendered":"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>PHPUnit \u548c Mockery \u662f PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4e24\u79cd\u9009\u62e9\u3002PHPUnit \u6210\u719f\u4e14\u7075\u6d3b\uff0c\u800c Mockery \u8f7b\u91cf\u4e14\u64c5\u957f\u6a21\u62df\u3002\u4e24\u8005\u4f18\u70b9\u5982\u4e0b\uff1aPHPUnit\uff1a\u5e7f\u6cdb\u4f7f\u7528\u4e14\u6210\u719f\u652f\u6301\u591a\u79cd\u65ad\u8a00\u98ce\u683c\u4e0e\u5176\u4ed6 PHP \u5de5\u5177\u96c6\u6210\u826f\u597dMockery\uff1a\u8f7b\u91cf\u7ea7\u5f3a\u5927\u7684\u6a21\u62df\u529f\u80fd\u652f\u6301\u591a\u79cd\u8bed\u8a00<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729919839671c7b5f2fd74.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u5bf9\u6bd4<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9a8c\u8bc1\u51fd\u6570\u7684\u9884\u671f\u884c\u4e3a\uff0c\u8fdb\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u9760\u6027\u3002PHP \u6709\u8bb8\u591a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u53ef\u4f9b\u9009\u62e9\uff0c\u672c\u6587\u5c06\u6bd4\u8f83\u5176\u4e2d\u4e24\u4e2a\u6700\u6d41\u884c\u7684\u6846\u67b6\uff1aPHPUnit \u548c Mockery\u3002<\/p>\n<p><strong>PHPUnit<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u5e7f\u6cdb\u4f7f\u7528\u4e14\u6210\u719f<\/li>\n<li>\u7075\u6d3b\uff0c\u652f\u6301\u591a\u79cd\u65ad\u8a00\u98ce\u683c<\/li>\n<li>\u4e0e\u5176\u4ed6 PHP \u5de5\u5177\uff08\u4f8b\u5982 Codeception\uff09\u96c6\u6210\u826f\u597d<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u76f8\u5bf9\u8f83\u91cd\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6d4b\u8bd5\u8fd0\u884c\u65f6\u95f4<\/li>\n<li>\u5bf9\u4e8e\u590d\u6742\u6d4b\u8bd5\u573a\u666f\u53ef\u80fd\u8fc7\u4e8e\u5197\u957f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 PHPUnit \u6d4b\u8bd5\u4e00\u4e2a\u6c42\u548c\u51fd\u6570\n&lt;?php\n\nuse PHPUnit\\Framework\\TestCase;\n\nclass SumTest extends TestCase\n{\n    public function testSum()\n    {\n        $sum = new Sum();\n        $this-&gt;assertEquals(6, $sum-&gt;calculate([1, 2, 3]));\n    }\n}<\/pre>\n<p><strong>Mockery<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u8f7b\u91cf\u7ea7\uff0c\u7279\u522b\u9002\u5408\u4e8e\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u7684\u4f9d\u8d56\u9879<\/li>\n<li>\u63d0\u4f9b\u5f3a\u5927\u7684\u6a21\u62df\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u6d4b\u8bd5\u6869\u548c\u6a21\u62df\u5bf9\u8c61<\/li>\n<li>\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff08PHP\u3001JavaScript\u3001Python \u7b49\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5165\u95e8\u6709\u4e00\u5b9a\u5b66\u4e60\u66f2\u7ebf<\/li>\n<li>\u53ef\u80fd\u8fc7\u4e8e\u7075\u6d3b\uff0c\u5bfc\u81f4\u6d4b\u8bd5\u96be\u4ee5\u7406\u89e3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 Mockery \u6a21\u62df\u6570\u636e\u5e93\u8fde\u63a5\n&lt;?php\n\nuse Mockery as m;\n\nclass DatabaseTest extends TestCase\n{\n    public function testConnect()\n    {\n        $mock = m::mock('DatabaseConnection');\n        $mock-&gt;shouldReceive('connect')-&gt;andReturn(true);\n        $database = new Database($mock);\n        $this-&gt;assertTrue($database-&gt;connect());\n    }\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9009\u62e9\u6700\u9002\u5408\u60a8\u7684 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u3002PHPUnit \u662f\u4e00\u4e2a\u6210\u719f\u4e14\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u800c Mockery \u5728\u6a21\u62df\u65b9\u9762\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002\u901a\u8fc7\u6743\u8861\u5404\u81ea\u7684\u4f18\u70b9\u548c\u7f3a\u70b9\uff0c\u60a8\u53ef\u4ee5\u627e\u5230\u6700\u9002\u5408\u60a8\u9879\u76ee\u7684\u6846\u67b6\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4 \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4f18\u52a3\u5bf9\u6bd4\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 PHPUnit \u548c Mockery \u662f PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u7684\u4e24\u79cd\u9009\u62e9\u3002PHPUnit \u6210\u719f\u4e14\u7075\u6d3b\uff0c\u800c Mockery \u8f7b\u91cf\u4e14\u64c5\u957f\u6a21\u62df\u3002\u4e24\u8005\u4f18\u70b9\u5982\u4e0b\uff1aPHPUnit\uff1a\u5e7f\u6cdb\u4f7f\u7528\u4e14\u6210\u719f\u652f\u6301\u591a\u79cd\u65ad\u8a00\u98ce\u683c\u4e0e\u5176\u4ed6 PHP \u5de5\u5177\u96c6\u6210\u826f\u597dMockery\uff1a\u8f7b\u91cf\u7ea7\u5f3a\u5927\u7684\u6a21\u62df\u529f\u80fd\u652f\u6301\u591a\u79cd\u8bed\u8a00 PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u5bf9\u6bd4 \u5f15\u8a00 \u5355\u5143\u6d4b\u8bd5\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9a8c\u8bc1\u51fd\u6570\u7684\u9884\u671f\u884c\u4e3a\uff0c\u8fdb\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u9760\u6027\u3002PHP \u6709\u8bb8\u591a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u53ef\u4f9b\u9009\u62e9\uff0c\u672c\u6587\u5c06\u6bd4\u8f83\u5176\u4e2d\u4e24\u4e2a\u6700\u6d41\u884c\u7684\u6846\u67b6\uff1aPHPUnit \u548c Mockery\u3002 PHPUnit \u4f18\u70b9\uff1a \u5e7f\u6cdb\u4f7f\u7528\u4e14\u6210\u719f \u7075\u6d3b\uff0c\u652f\u6301\u591a\u79cd\u65ad\u8a00\u98ce\u683c \u4e0e\u5176\u4ed6 PHP \u5de5\u5177\uff08\u4f8b\u5982 Codeception\uff09\u96c6\u6210\u826f\u597d \u7f3a\u70b9\uff1a \u76f8\u5bf9\u8f83\u91cd\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6d4b\u8bd5\u8fd0\u884c\u65f6\u95f4 \u5bf9\u4e8e\u590d\u6742\u6d4b\u8bd5\u573a\u666f\u53ef\u80fd\u8fc7\u4e8e\u5197\u957f \u5b9e\u6218\u6848\u4f8b\uff1a \/\/ \u4f7f\u7528 PHPUnit \u6d4b\u8bd5\u4e00\u4e2a\u6c42\u548c\u51fd\u6570 &lt;?php use PHPUnit\\Framework\\TestCase; class SumTest extends TestCase { public function testSum() { $sum = new Sum(); $this-&gt;assertEquals(6, [&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-47481","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47481","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=47481"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47481\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}