LoginSignup
0
0

More than 5 years have passed since last update.

CakePHP UnitTest中「ob_end_clean():failed to delete buffer」エラー対応

Last updated at Posted at 2016-05-02

UnitTest中成功したのにも関わらず以下のエラーが出てきて失敗

一部のコードです。最後まで問題なく成功したのにも関わらず以下のエラーが出て失敗しました。
「PHPUnit_Framework_Error_Notice : ob_end_clean(): failed to delete buffer. No buffer to delete」

以下のdownloadをテストしたいと思いました。

controller
 public function download() {

        $this->autoRender = false;

        $id = $this->request->params['pass']['0'];

        $info = $this->XXXX->find('first', ['conditions' => ['id' => $id, 'is_deleted' => 0]]);

        $this->response->header(['Content-type: application/pdf', 'file: ファイル名']);
        $this->response->file(ファイルパス);

    }
unittest
 $this->controller->XXXX= $this->getMockForModel('XXXX', ['find']);
 $this->controller->XXXX->expects($this->once())->method('find')->will($this->returnValue($info));

 $this->testAction('/XXXX/download/123', ['method' => 'get']);

 $file =$this->headers['file'];
 $contentType =$this->headers['Content-type'];
 $fileLength =$this->headers['Content-Length'];

 $this->assertEqual($file, ファイル名);
 $this->assertEqual($contentType, 'application/pdf');
 $this->assertEqual($fileLength, 60079);

同じエラーが発生する場合以下のようにコードを追加すれば解決できます。

unittest
 $this->controller->XXXX= $this->getMockForModel('XXXX', ['download','find']);
 $this->controller->XXXX->expects($this->once())->method('find')->will($this->returnValue($info));
 $this->testAction('/XXXX/download/123', ['method' => 'get']);

 $file =$this->headers['file'];
 $contentType =$this->headers['Content-type'];
 $fileLength =$this->headers['Content-Length'];

 $this->assertEqual($file, ファイル名);
 $this->assertEqual($contentType, 'application/pdf');
 $this->assertEqual($fileLength, 60079);

 //以下のコードを追加すればエラーなく成功になります。
 if (ob_get_length() == 0 ) {
    ob_start();
 }

unitコードが問題なのかPHPUnitのバグかは調べる予定です。一応対策だけ。。。

詳細なApi説明は以下を参考にしてください。
http://php.net/manual/ja/function.ob-end-clean.php
http://php.net/manual/ja/function.ob-get-length.php
http://php.net/manual/ja/function.ob-start.php

以下の内容を参考しました。
https://github.com/sebastianbergmann/phpunit/issues/390

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0