{"id":47303,"date":"2024-12-02T09:24:13","date_gmt":"2024-12-02T01:24:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47303\/"},"modified":"2024-12-02T09:24:13","modified_gmt":"2024-12-02T01:24:13","slug":"php-%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%bc%96%e5%86%99%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47303\/","title":{"rendered":"PHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>PHP \u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u9a8c\u8bc1\u4ee3\u7801\u6b63\u786e\u6027\u7684\u91cd\u8981\u73af\u8282\u3002\u4f7f\u7528 PHPUnit \u5e93\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5148\u5b89\u88c5\u5b83\u3002\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u53ef\u65ad\u8a00\u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\u4e0e\u9884\u671f\u503c\u76f8\u7b49\uff0c\u4f7f\u7528 phpunit \u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\u3002\u5728\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u5355\u5143\u6d4b\u8bd5\u53ef\u9a8c\u8bc1\u90ae\u7bb1\u9a8c\u8bc1\u51fd\u6570\u7684\u903b\u8f91\uff0c\u589e\u5f3a\u4ee3\u7801\u4fe1\u5fc3\uff0c\u5e76\u786e\u4fdd\u51fd\u6570\u6309\u9884\u671f\u8fd0\u884c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729909942671c54b6db588.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u51fd\u6570\u4e4b\u5355\u5143\u6d4b\u8bd5\u8be6\u89e3\uff0c\u5b9e\u6218\u6848\u4f8b\u52a9\u4f60\u4e0a\u624b<\/h2>\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u73af\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u4eca\u5929\uff0c\u6211\u4eec\u5c31\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 PHP \u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<p><strong>\u51c6\u5907\u5de5\u4f5c<\/strong><\/p>\n<p>\u5728\u5f00\u59cb\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5 PHPUnit \u5e93\uff0c\u8fd9\u662f PHP \u7684\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre>composer require phpunit\/phpunit<\/pre>\n<p><strong>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u4ee5\u4e00\u4e2a\u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\u7684\u51fd\u6570\u4e3a\u4f8b\uff1a<\/p>\n<pre>function sum($a, $b) {\n  return $a + $b;\n}<\/pre>\n<p>\u9488\u5bf9\u6b64\u51fd\u6570\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u5982\u4e0b\uff1a<\/p>\n<pre>class SumTest extends \\PHPUnit\\Framework\\TestCase {\n  public function testSum() {\n    $this-&gt;assertEquals(4, sum(2, 2));\n    $this-&gt;assertEquals(10, sum(5, 5));\n  }\n}<\/pre>\n<p>\u5728\u6d4b\u8bd5\u7c7b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>testSum<\/code> \u65b9\u6cd5\uff0c\u4f7f\u7528 <code>TestCase::assertEquals<\/code> \u65ad\u8a00\u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\u4e0e\u9884\u671f\u503c\u76f8\u7b49\u3002<\/p>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>phpunit<\/code> \u547d\u4ee4\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\uff1a<\/p>\n<pre>phpunit tests<\/pre>\n<p>\u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u5219\u4f1a\u663e\u793a\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre>OK (2 tests, 2 assertions)<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\u6ce8\u518c\u7cfb\u7edf\uff0c\u9700\u8981\u5bf9\u6ce8\u518c\u90ae\u7bb1\u8fdb\u884c\u9a8c\u8bc1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9a8c\u8bc1\u90ae\u7bb1\u662f\u5426\u5408\u6cd5\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function isValidEmail($email) {\n  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {\n    return false;\n  }\n  return true;\n}<\/pre>\n<p>\u9488\u5bf9\u6b64\u51fd\u6570\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff1a<\/p>\n<pre>class IsValidEmailTest extends \\PHPUnit\\Framework\\TestCase {\n  public function testValidEmails() {\n    $this-&gt;assertTrue(isValidEmail('example@gmail.com'));\n    $this-&gt;assertTrue(isValidEmail('username@mail.example.com'));\n  }\n\n  public function testInvalidEmails() {\n    $this-&gt;assertFalse(isValidEmail('invalid-email'));\n    $this-&gt;assertFalse(isValidEmail('@example.com'));\n  }\n}<\/pre>\n<p>\u901a\u8fc7\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c podemos validar la l\u00f3gica de funciones individuales, lo que aumenta la confianza en el c\u00f3digo y garantiza que funcione seg\u00fan lo previsto.<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\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\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5 \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u600e\u4e48\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 PHP \u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u9a8c\u8bc1\u4ee3\u7801\u6b63\u786e\u6027\u7684\u91cd\u8981\u73af\u8282\u3002\u4f7f\u7528 PHPUnit \u5e93\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5148\u5b89\u88c5\u5b83\u3002\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u53ef\u65ad\u8a00\u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\u4e0e\u9884\u671f\u503c\u76f8\u7b49\uff0c\u4f7f\u7528 phpunit \u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\u3002\u5728\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u5355\u5143\u6d4b\u8bd5\u53ef\u9a8c\u8bc1\u90ae\u7bb1\u9a8c\u8bc1\u51fd\u6570\u7684\u903b\u8f91\uff0c\u589e\u5f3a\u4ee3\u7801\u4fe1\u5fc3\uff0c\u5e76\u786e\u4fdd\u51fd\u6570\u6309\u9884\u671f\u8fd0\u884c\u3002 PHP \u51fd\u6570\u4e4b\u5355\u5143\u6d4b\u8bd5\u8be6\u89e3\uff0c\u5b9e\u6218\u6848\u4f8b\u52a9\u4f60\u4e0a\u624b \u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u73af\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u4eca\u5929\uff0c\u6211\u4eec\u5c31\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 PHP \u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002 \u51c6\u5907\u5de5\u4f5c \u5728\u5f00\u59cb\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5 PHPUnit \u5e93\uff0c\u8fd9\u662f PHP \u7684\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a composer require phpunit\/phpunit \u7f16\u5199\u5355\u5143\u6d4b\u8bd5 \u4ee5\u4e00\u4e2a\u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\u7684\u51fd\u6570\u4e3a\u4f8b\uff1a function sum($a, $b) { return $a + $b; } \u9488\u5bf9\u6b64\u51fd\u6570\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u5982\u4e0b\uff1a class SumTest extends \\PHPUnit\\Framework\\TestCase { public function testSum() { $this-&gt;assertEquals(4, sum(2, 2)); $this-&gt;assertEquals(10, sum(5, 5)); } } \u5728\u6d4b\u8bd5\u7c7b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a testSum [&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-47303","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47303","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=47303"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47303\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}