{"id":47042,"date":"2024-12-02T12:01:33","date_gmt":"2024-12-02T04:01:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47042\/"},"modified":"2024-12-02T12:01:33","modified_gmt":"2024-12-02T04:01:33","slug":"php-%e5%87%bd%e6%95%b0%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e4%b8%8e%e8%b0%83%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47042\/","title":{"rendered":"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729861762671b9882a370c.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u6d4b\u8bd5\u8f6f\u4ef6\u7684\u6700\u5c0f\u5355\u5143\uff08\u51fd\u6570\u6216\u65b9\u6cd5\uff09\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u5065\u58ee\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u8fdb\u884c\u51fd\u6570\u5355\u5143\u6d4b\u8bd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u4f7f\u7528 PHPUnit \u8fdb\u884c\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>PHPUnit \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u8981\u5b89\u88c5\u5b83\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer require --dev phpunit\/phpunit<\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>test.php<\/code> \u7684\u6587\u4ef6\u5e76\u7f16\u5199\u4ee5\u4e0b\u6d4b\u8bd5\u7528\u4f8b\uff1a<\/p>\n<pre>&lt;?php\n\nuse PHPUnit\\Framework\\TestCase;\n\nclass StringFunctionsTest extends TestCase\n{\n    public function testStringToUpper()\n    {\n        $this-&gt;assertEquals(\"HELLO WORLD\", strtoupper(\"Hello world\"));\n    }\n}<\/pre>\n<p>\u5728\u6d4b\u8bd5\u7528\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>assertEquals()<\/code> \u65b9\u6cd5\u6765\u9a8c\u8bc1\u5b57\u7b26\u4e32\u8f6c\u6362\u7ed3\u679c\u662f\u5426\u6b63\u786e\u3002<\/p>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/p>\n<p>\u8981\u8fd0\u884c\u6d4b\u8bd5\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>phpunit<\/pre>\n<p>\u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u4f60\u5c06\u770b\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n<pre>PHPUnit 9.5.15 by Sebastian Bergmann and contributors.\n\nTesting:\nStringFunctionsTest\n\nTime: 12 ms, Memory: 6.75 MB\n\nOK (1 test, 1 assertion)<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684\u5e73\u5747\u503c\uff1a<\/p>\n<pre>function average($a, $b)\n{\n    return ($a + $b) \/ 2;\n}<\/pre>\n<p>\u8981\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff1a<\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\n\nclass AverageFunctionTest extends TestCase\n{\n    \/**\n     * @dataProvider averageProvider\n     *\/\n    public function testAverage(float $a, float $b, float $expected)\n    {\n        $this-&gt;assertEquals($expected, average($a, $b));\n    }\n\n    public function averageProvider()\n    {\n        return [\n            [5, 10, 7.5],\n            [0, 100, 50],\n            [-5, 5, 0],\n        ];\n    }\n}<\/pre>\n<p>\u5728\u6d4b\u8bd5\u7528\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 @dataProvider \u6ce8\u91ca\uff0c\u5e76\u901a\u8fc7 averageProvider() \u65b9\u6cd5\u63d0\u4f9b\u6d4b\u8bd5\u6570\u636e\u3002<\/p>\n<p><strong>\u8c03\u8bd5<\/strong><\/p>\n<p>\u5982\u679c\u6d4b\u8bd5\u5931\u8d25\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Xdebug \u7b49\u8c03\u8bd5\u5668\u6765\u67e5\u660e\u9519\u8bef\u3002\u8981\u5728 Xdebug \u4e2d\u542f\u7528\u8c03\u8bd5\uff0c\u8bf7\u5c06\u4ee5\u4e0b\u884c\u6dfb\u52a0\u5230 <code>php.ini<\/code> \u6587\u4ef6\uff1a<\/p>\n<pre>[xdebug]\nzend_extension=\/path\/to\/xdebug.so\nxdebug.remote_enable=1<\/pre>\n<p>\u7136\u540e\uff0c\u91cd\u65b0\u542f\u52a8 PHP \u5e76\u6309\u7167 xdebug \u6587\u6863\u8bbe\u7f6e\u8c03\u8bd5\u5668\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f PHP \u5f00\u53d1\u4e2d\u7684\u4e00\u4e2a\u5b9d\u8d35\u5de5\u5177\u3002\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u5e76\u7b80\u5316\u8c03\u8bd5\u8fc7\u7a0b\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u4f7f\u7528 PHPUnit \u8fdb\u884c\u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u8fdb\u884c\u4e86\u8bf4\u660e\u3002\u901a\u8fc7\u5b9e\u65bd\u5355\u5143\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u5e76\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5 \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b PHP \u51fd\u6570\u5355\u5143\u6d4b\u8bd5\u4e0e\u8c03\u8bd5 \u5355\u5143\u6d4b\u8bd5\u662f\u6d4b\u8bd5\u8f6f\u4ef6\u7684\u6700\u5c0f\u5355\u5143\uff08\u51fd\u6570\u6216\u65b9\u6cd5\uff09\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u5065\u58ee\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u8fdb\u884c\u51fd\u6570\u5355\u5143\u6d4b\u8bd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002 \u4f7f\u7528 PHPUnit \u8fdb\u884c\u5355\u5143\u6d4b\u8bd5 PHPUnit \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u8981\u5b89\u88c5\u5b83\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require &#8211;dev phpunit\/phpunit \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a test.php \u7684\u6587\u4ef6\u5e76\u7f16\u5199\u4ee5\u4e0b\u6d4b\u8bd5\u7528\u4f8b\uff1a &lt;?php use PHPUnit\\Framework\\TestCase; class StringFunctionsTest extends TestCase { public function testStringToUpper() { $this-&gt;assertEquals(&#8220;HELLO WORLD&#8221;, strtoupper(&#8220;Hello world&#8221;)); } } \u5728\u6d4b\u8bd5\u7528\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 assertEquals() \u65b9\u6cd5\u6765\u9a8c\u8bc1\u5b57\u7b26\u4e32\u8f6c\u6362\u7ed3\u679c\u662f\u5426\u6b63\u786e\u3002 \u8fd0\u884c\u6d4b\u8bd5 \u8981\u8fd0\u884c\u6d4b\u8bd5\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a phpunit \u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u4f60\u5c06\u770b\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u8f93\u51fa\uff1a PHPUnit 9.5.15 by Sebastian Bergmann and contributors. [&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-47042","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47042","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=47042"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47042\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}