{"id":48131,"date":"2024-12-02T12:04:05","date_gmt":"2024-12-02T04:04:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48131\/"},"modified":"2024-12-02T12:04:05","modified_gmt":"2024-12-02T04:04:05","slug":"%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e5%8c%96php%e5%87%bd%e6%95%b0%e6%b5%8b%e8%af%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48131\/","title":{"rendered":"\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f<\/h1>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>\u4f7f\u7528 PHPUnit \u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5\uff1a\u4f7f\u7528 Composer \u5b89\u88c5 PHPUnit\u3002\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea PHPUnit\\Framework\\TestCase \u7c7b\u7684\u6d4b\u8bd5\u7c7b\u3002\u4e3a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u7f16\u5199 test \u5f00\u5934\u7684\u6d4b\u8bd5\u65b9\u6cd5\u3002\u4f7f\u7528 assert*() \u65ad\u8a00\u65b9\u6cd5\u9a8c\u8bc1\u6d4b\u8bd5\u7ed3\u679c\u3002\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528 phpunit \u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729991380671d92d42be7f.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u81ea\u52a8\u5316\u6d4b\u8bd5\u5bf9\u4e8e\u63d0\u9ad8\u8f6f\u4ef6\u8d28\u91cf\u81f3\u5173\u91cd\u8981\uff0c\u800c\u51fd\u6570\u6d4b\u8bd5\u662f\u786e\u4fdd PHP \u51fd\u6570\u884c\u4e3a\u7b26\u5408\u9884\u671f\u7684\u4e00\u4e2a\u5173\u952e\u65b9\u9762\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u4f7f\u7528 PHPUnit \u6765\u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5\u3002<\/p>\n<p><strong>\u5b89\u88c5 PHPUnit<\/strong><\/p>\n<p>\u4f7f\u7528 Composer \u5b89\u88c5 PHPUnit\uff1a<\/p>\n<pre>composer global require phpunit\/phpunit<\/pre>\n<p><strong>\u521b\u5efa\u6d4b\u8bd5\u7c7b<\/strong><\/p>\n<p>\u4e3a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u7c7b\u3002\u6d4b\u8bd5\u7c7b\u5e94\u7ee7\u627f\u81ea <code>\\PHPUnit\\Framework\\TestCase<\/code> \u7c7b\uff1a<\/p>\n<pre>class MathFunctionsTest extends \\PHPUnit\\Framework\\TestCase\n{\n    \/\/ ...\n}<\/pre>\n<p><strong>\u7f16\u5199\u6d4b\u8bd5\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u6bcf\u4e2a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u7f16\u5199\u4e00\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u3002\u6d4b\u8bd5\u65b9\u6cd5\u540d\u79f0\u5e94\u4ee5 <code>test<\/code> \u5f00\u5934\uff0c\u540e\u8ddf\u51fd\u6570\u540d\uff1a<\/p>\n<pre>public function testAdd()\n{\n    $result = add(2, 3);\n    $this-&gt;assertEquals(5, $result);\n}<\/pre>\n<p><strong>\u65ad\u8a00<\/strong><\/p>\n<p>\u4f7f\u7528 <code>assert*()<\/code> \u65b9\u6cd5\u5bf9\u6d4b\u8bd5\u7ed3\u679c\u8fdb\u884c\u65ad\u8a00\u3002 PHPUnit \u63d0\u4f9b\u4e86\u8bb8\u591a\u65ad\u8a00\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>assertEquals(expected, actual)<\/code>\uff1a\u68c0\u67e5\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49<\/li>\n<li><code>assertTrue(condition)<\/code>\uff1a\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u4e3a\u771f<\/li>\n<li><code>assertFalse(condition)<\/code>\uff1a\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u4e3a\u5047<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b <code>add()<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>function add(int $a, int $b): int\n{\n    return $a + $b;\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u8be5\u51fd\u6570\uff1a<\/p>\n<pre>class MathFunctionsTest extends \\PHPUnit\\Framework\\TestCase\n{\n    public function testAdd()\n    {\n        $result = add(2, 3);\n        $this-&gt;assertEquals(5, $result);\n    }\n}<\/pre>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/p>\n<p>\u8981\u8fd0\u884c\u6d4b\u8bd5\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>phpunit<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u672c\u6587\u4e2d\u7684\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5\u3002 PHPUnit \u7684\u5f3a\u5927\u65ad\u8a00\u673a\u5236\u53ef\u5e2e\u52a9\u60a8\u9a8c\u8bc1\u51fd\u6570\u884c\u4e3a\uff0c\u4ece\u800c\u63d0\u9ad8\u8f6f\u4ef6\u8d28\u91cf\u5e76\u589e\u5f3a\u4fe1\u5fc3\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5982\u4f55\u81ea\u52a8\u5316PHP\u51fd\u6570\u6d4b\u8bd5\uff1f\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u4f7f\u7528 PHPUnit \u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5\uff1a\u4f7f\u7528 Composer \u5b89\u88c5 PHPUnit\u3002\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea PHPUnit\\Framework\\TestCase \u7c7b\u7684\u6d4b\u8bd5\u7c7b\u3002\u4e3a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u7f16\u5199 test \u5f00\u5934\u7684\u6d4b\u8bd5\u65b9\u6cd5\u3002\u4f7f\u7528 assert*() \u65ad\u8a00\u65b9\u6cd5\u9a8c\u8bc1\u6d4b\u8bd5\u7ed3\u679c\u3002\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528 phpunit \u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\u3002 \u5982\u4f55\u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5 \u7b80\u4ecb \u81ea\u52a8\u5316\u6d4b\u8bd5\u5bf9\u4e8e\u63d0\u9ad8\u8f6f\u4ef6\u8d28\u91cf\u81f3\u5173\u91cd\u8981\uff0c\u800c\u51fd\u6570\u6d4b\u8bd5\u662f\u786e\u4fdd PHP \u51fd\u6570\u884c\u4e3a\u7b26\u5408\u9884\u671f\u7684\u4e00\u4e2a\u5173\u952e\u65b9\u9762\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u4f7f\u7528 PHPUnit \u6765\u81ea\u52a8\u5316 PHP \u51fd\u6570\u6d4b\u8bd5\u3002 \u5b89\u88c5 PHPUnit \u4f7f\u7528 Composer \u5b89\u88c5 PHPUnit\uff1a composer global require phpunit\/phpunit \u521b\u5efa\u6d4b\u8bd5\u7c7b \u4e3a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6d4b\u8bd5\u7c7b\u3002\u6d4b\u8bd5\u7c7b\u5e94\u7ee7\u627f\u81ea \\PHPUnit\\Framework\\TestCase \u7c7b\uff1a class MathFunctionsTest extends \\PHPUnit\\Framework\\TestCase { \/\/ &#8230; } \u7f16\u5199\u6d4b\u8bd5\u65b9\u6cd5 \u4e3a\u6bcf\u4e2a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u7f16\u5199\u4e00\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u3002\u6d4b\u8bd5\u65b9\u6cd5\u540d\u79f0\u5e94\u4ee5 test \u5f00\u5934\uff0c\u540e\u8ddf\u51fd\u6570\u540d\uff1a [&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-48131","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48131","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=48131"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48131\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}