{"id":46762,"date":"2024-12-02T15:48:40","date_gmt":"2024-12-02T07:48:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46762\/"},"modified":"2024-12-02T15:48:40","modified_gmt":"2024-12-02T07:48:40","slug":"php%e5%87%bd%e6%95%b0mock%e6%b5%8b%e8%af%95%e7%9a%84%e6%9c%89%e6%95%88%e5%88%a9%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46762\/","title":{"rendered":"PHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p>PHPUnit Mockery \u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa mock \u5bf9\u8c61\u7684 PHP \u5e93\uff0c\u53ef\u901a\u8fc7\u9884\u5148\u914d\u7f6e\u548c\u9a8c\u8bc1\u4f9d\u8d56\u9879\u7684\u884c\u4e3a\u6765\u6709\u6548\u6d4b\u8bd5\u4ee3\u7801\u3002\u5b83\u4f7f\u60a8\u53ef\u4ee5\u521b\u5efa mock \u5bf9\u8c61\uff08\u6b65\u9aa4 1\uff09\uff0c\u914d\u7f6e\u5b83\u4eec\u7684\u9884\u671f\u884c\u4e3a\uff08\u6b65\u9aa4 2\uff09\uff0c\u5e76\u5728\u6d4b\u8bd5\u5b8c\u6210\u540e\u9a8c\u8bc1\u5b83\u4eec\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\uff08\u6b65\u9aa4 3\uff09\u3002\u901a\u8fc7 mock \u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u65b9\u6cd5\u8c03\u7528\u3001\u53c2\u6570\u548c\u6b21\u6570\uff0c\u4ece\u800c\u786e\u4fdd\u4ee3\u7801\u5bf9\u5916\u90e8\u4f9d\u8d56\u7684\u5904\u7406\u7b26\u5408\u9884\u671f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729844868671b56841a0fe.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528\u63d2\u56fe\" alt=\"PHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570 Mock \u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHPUnit Mockery\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u6613\u7528\u7684PHP\u5e93\uff0c\u53ef\u7528\u4e8e\u521b\u5efa\u6d4b\u8bd5\u66ff\u8eab(stub)\u548c\u6869(dummy)\u5bf9\u8c61\uff0c\u4ee5\u6709\u6548\u5730\u6d4b\u8bd5\u4f9d\u8d56\u5916\u90e8\u51fd\u6570\u548c\u7c7b\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u521b\u5efa Mock \u5bf9\u8c61<\/strong><\/p>\n<p>\u5728 PHPUnit Mockery \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Mockery::mock()<\/code> \u51fd\u6570\u8f7b\u677e\u521b\u5efa mock \u5bf9\u8c61\uff1a<\/p>\n<pre>$mock = Mockery::mock('stdClass');<\/pre>\n<p>\u8fd9\u4e2a mock \u5bf9\u8c61\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u666e\u901a\u7684 PHP \u5bf9\u8c61\uff0c\u4f46\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5bf9\u5176\u65b9\u6cd5\u8fdb\u884c\u9884\u5148\u914d\u7f6e\u548c\u9a8c\u8bc1<\/li>\n<li>\u53ef\u4ee5\u5bf9\u5176\u4f20\u5165\u7684\u53c2\u6570\u8fdb\u884c\u68c0\u67e5<\/li>\n<li>\u53ef\u4ee5\u68c0\u67e5\u5176\u8c03\u7528\u7684\u6b21\u6570<\/li>\n<\/ul>\n<p><strong>\u914d\u7f6e Mock \u5bf9\u8c61<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5bf9\u8c61\u7684 <code>shouldReceive()<\/code> \u65b9\u6cd5\u6765\u914d\u7f6e mock \u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u51fd\u6570\u7684\u9884\u671f\u884c\u4e3a\u548c\u53c2\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$mock-&gt;shouldReceive('getName')-&gt;andReturn('John Doe');<\/pre>\n<p>\u8fd9\u8868\u660e <code>getName()<\/code> \u65b9\u6cd5\u5e94\u8be5\u88ab\u8c03\u7528\uff0c\u5e76\u8fd4\u56de &#8220;John Doe&#8221;\u3002<\/p>\n<p><strong>\u9a8c\u8bc1 Mock \u5bf9\u8c61<\/strong><\/p>\n<p>\u6d4b\u8bd5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Mockery::close()<\/code> \u51fd\u6570\u6765\u9a8c\u8bc1 mock \u5bf9\u8c61\u3002\u8fd9\u5c06\u786e\u4fdd\u9884\u671f\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u4e86\u6307\u5b9a\u6b21\u6570\uff0c\u5e76\u5177\u6709\u9884\u671f\u7684\u53c2\u6570\uff1a<\/p>\n<pre>Mockery::close();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff0c\u5b83\u8c03\u7528 <code>getUser()<\/code> \u51fd\u6570\u4ece\u5916\u90e8\u6570\u636e\u6e90\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff1a<\/p>\n<pre>function displayUserInfo() {\n    $user = getUser();\n    echo \"Name: \" . $user-&gt;getName();\n}<\/pre>\n<p>\u4f7f\u7528 PHPUnit Mockery\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa <code>getUser()<\/code> \u51fd\u6570\u7684 mock \u5bf9\u8c61\uff0c\u4ee5\u6d4b\u8bd5 <code>displayUserInfo()<\/code> \u51fd\u6570\u7684\u671f\u671b\u884c\u4e3a\uff1a<\/p>\n<pre>$mock = Mockery::mock('getUser');\n$mock-&gt;shouldReceive('getUser')-&gt;andReturn(new User('John Doe'));\n\ndisplayUserInfo();\n\nMockery::close();<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5047\u5b9a <code>getUser()<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u540d\u4e3a &#8220;John Doe&#8221; \u7684\u7528\u6237\u5bf9\u8c61\u3002Mockery \u5141\u8bb8\u6211\u4eec\u9a8c\u8bc1 <code>displayUserInfo()<\/code> \u51fd\u6570\u5c06\u8f93\u51fa &#8220;Name: John Doe&#8221;\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>PHPUnit Mockery \u63d0\u4f9b\u4e86\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\u6765\u6d4b\u8bd5\u4f9d\u8d56\u5916\u90e8\u51fd\u6570\u548c\u7c7b\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u521b\u5efa mock \u5bf9\u8c61\uff0c\u53ef\u4ee5\u9884\u5148\u914d\u7f6e\u548c\u9a8c\u8bc1\u4f9d\u8d56\u9879\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u786e\u4fdd\u4ee3\u7801\u6309\u7167\u9884\u671f\u5de5\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u5355\u5143\u6d4b\u8bd5\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528 \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300aPHP\u51fd\u6570mock\u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 PHPUnit Mockery \u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa mock \u5bf9\u8c61\u7684 PHP \u5e93\uff0c\u53ef\u901a\u8fc7\u9884\u5148\u914d\u7f6e\u548c\u9a8c\u8bc1\u4f9d\u8d56\u9879\u7684\u884c\u4e3a\u6765\u6709\u6548\u6d4b\u8bd5\u4ee3\u7801\u3002\u5b83\u4f7f\u60a8\u53ef\u4ee5\u521b\u5efa mock \u5bf9\u8c61\uff08\u6b65\u9aa4 1\uff09\uff0c\u914d\u7f6e\u5b83\u4eec\u7684\u9884\u671f\u884c\u4e3a\uff08\u6b65\u9aa4 2\uff09\uff0c\u5e76\u5728\u6d4b\u8bd5\u5b8c\u6210\u540e\u9a8c\u8bc1\u5b83\u4eec\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\uff08\u6b65\u9aa4 3\uff09\u3002\u901a\u8fc7 mock \u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u65b9\u6cd5\u8c03\u7528\u3001\u53c2\u6570\u548c\u6b21\u6570\uff0c\u4ece\u800c\u786e\u4fdd\u4ee3\u7801\u5bf9\u5916\u90e8\u4f9d\u8d56\u7684\u5904\u7406\u7b26\u5408\u9884\u671f\u3002 PHP \u51fd\u6570 Mock \u6d4b\u8bd5\u7684\u6709\u6548\u5229\u7528 \u7b80\u4ecb PHPUnit Mockery\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u6613\u7528\u7684PHP\u5e93\uff0c\u53ef\u7528\u4e8e\u521b\u5efa\u6d4b\u8bd5\u66ff\u8eab(stub)\u548c\u6869(dummy)\u5bf9\u8c61\uff0c\u4ee5\u6709\u6548\u5730\u6d4b\u8bd5\u4f9d\u8d56\u5916\u90e8\u51fd\u6570\u548c\u7c7b\u7684\u4ee3\u7801\u3002 \u521b\u5efa Mock \u5bf9\u8c61 \u5728 PHPUnit Mockery \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Mockery::mock() \u51fd\u6570\u8f7b\u677e\u521b\u5efa mock \u5bf9\u8c61\uff1a $mock = Mockery::mock(&#8216;stdClass&#8217;); \u8fd9\u4e2a mock \u5bf9\u8c61\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u666e\u901a\u7684 PHP \u5bf9\u8c61\uff0c\u4f46\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a \u53ef\u4ee5\u5bf9\u5176\u65b9\u6cd5\u8fdb\u884c\u9884\u5148\u914d\u7f6e\u548c\u9a8c\u8bc1 \u53ef\u4ee5\u5bf9\u5176\u4f20\u5165\u7684\u53c2\u6570\u8fdb\u884c\u68c0\u67e5 \u53ef\u4ee5\u68c0\u67e5\u5176\u8c03\u7528\u7684\u6b21\u6570 \u914d\u7f6e Mock \u5bf9\u8c61 \u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5bf9\u8c61\u7684 shouldReceive() \u65b9\u6cd5\u6765\u914d\u7f6e mock \u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u51fd\u6570\u7684\u9884\u671f\u884c\u4e3a\u548c\u53c2\u6570\uff0c\u4f8b\u5982\uff1a $mock-&gt;shouldReceive(&#8216;getName&#8217;)-&gt;andReturn(&#8216;John Doe&#8217;); [&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-46762","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46762","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=46762"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46762\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}