發表文章

目前顯示的是有「PHPUnit」標籤的文章

Markdown

[PHPUnit] mock

mock === Mock Objects https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects 對異常進行測試 https://jaceju-books.gitbooks.io/phpunit-in-action/content/chapters/03/5.html Testing Exceptions https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html How to unit testing Exceptions with PHPUnit? https://stackoverflow.com/questions/4646822/how-to-unit-testing-exceptions-with-phpunit this->fail() ?? https://stackoverflow.com/questions/28760534/phpunit-to-test-catch-block https://stackoverflow.com/questions/5683592/phpunit-assert-that-an-exception-was-thrown 如何使用 PHPUnit mock Closure? http://oomusou.io/phpunit/phpunit-closure-mock/ https://php-and-symfony.matthiasnoback.nl/2012/06/symfony2-testing-your-controllers/

[PHPUnit] 如何測試 DB 及一些回傳null 的function

https://phpunit.de/manual/current/zh_cn/test-doubles.html http://symfony.com/doc/current/testing/database.html 跑測試之後寫到測試資料庫 不影響現有資料。 使用MOCK 來取得 並模擬返回值。 $message = $this->createMock(Message::class); Mock_Message_a175c170 {#387 -__phpunit_invocationMocker: null -__phpunit_originalObject: null -__phpunit_configurable: array:9 [ 0 => "getupdatedat" 1 => "setupdatedat" 2 => "getpublishedat" 3 => "setpublishedat" 4 => "getid" 5 => "setusername" 6 => "getusername" 7 => "setmsg" 8 => "getmsg" ] -id: null -userName: null -msg: null -publishedAt: null -updatedAt: n entity http://symfony.com/doc/current/testing/doctrine.html確認ㄌㄨㄛ 目前還不常用到 先留作紀錄。 測試中不能有條件式存在 測試覆蓋率 去掉Dead line block 檢查邏輯

[PHPUnit] 解決CentOS7 底下使用 php56 安裝 XDEBUG 衝突問題

安裝XDEBUG的時候遇到PHP的一些環境衝突 sudo yum install php-xdebug ... Resolving Dependencies --> Running transaction check ---> Package php-pecl-xdebug.x86_64 0:2.2.7-1.el7 will be installed --> Processing Dependency: php(zend-abi) = 20100525-64 for package: php-pecl-xdebug-2.2.7-1.el7.x86_64 --> Processing Dependency: php(api) = 20100412-64 for package: php-pecl-xdebug-2.2.7-1.el7.x86_64 --> Processing Dependency: /usr/bin/pecl for package: php-pecl-xdebug-2.2.7-1.el7.x86_64 --> Processing Dependency: /usr/bin/pecl for package: php-pecl-xdebug-2.2.7-1.el7.x86_64 --> Running transaction check ---> Package php-common.x86_64 0:5.4.16-42.el7 will be installed ---> Package php-pear.noarch 1:1.9.4-21.el7 will be installed --> Processing Conflict: php56w-common-5.6.30-1.w7.x86_64 conflicts php-common < 5.6 --> Finished Dependency Resolution Error: php56w-common conflicts with php-common-5.4.16-42.el7.x86_64 You could try using --skip-broken to work around the probl...

[PHPUnit] API 與 TEST 的規劃

之前做的 Create API 測試都是呼叫API完,跑去首頁撈結果比對。 所以之前的Create API設計就只有單向,不return response也沒關西。 但這樣等於是測試一個API 卻用到另一隻顯示首頁的 list API ,也就是跨了兩支API。 這樣顯然不符合針對Create API 做測試的目的, 所以現在要在後端API 收完request 且寫入完成的時候, 發一個 response 將寫入的資料結果用json_encode 丟出來。 然後再Test 這邊再去吃對API 做 post request 之後 ,API 給的 response。 並做 json_decode處理 ,再去比對 test 先前發出的 post request 內容是否一致。 --- 先看TEST的部分 /** * @group aa */ public function testCreateAction () { $client = static ::createClient(); $postData = [ 'userName' => 'testPHPUnit' . mt_rand(), 'msg' => 'test' ]; $crawler = $client->request( 'POST' , '/api/add' , array (), array (), array ( 'CONTENT_TYPE' => 'application/json' ), json_encode($postData) ); $this ->assertEqual...

[PHPUnit] 將測試分開做

add the annotation before function 48 /**~ 49 * @group aa~ 50 */~ public function testCreateAction(){…} $ vendor/bin/phpunit --group aa

[PHPUnit] request a post by crawler

class EntidadeControllerTest extends WebTestCase { public function testCreate() { $dados = array( 'nome' => 'Entidade TESTE 01', 'ativo' => 0, '_token' => $this->csrfDefaultToken ); $crawler = $this->client->request( 'POST', '/admin/entidades', array(), array(), array( 'CONTENT_TYPE' => 'application/json', 'HTTP_X-Requested-With' => 'XMLHttpRequest' ), json_encode(array('cartorio_servico_entidade'=>$dados)) ); $this->assertEquals(201, $this->client->getResponse()->getStatusCode()); } } --- https://stackoverflow.com/questions/20025222/how-to-test-restful-json-create-entity-action