2016-09-08 23 views
21

Tôi đang sử dụng CakePHP v3.x với phpunit v5.1.3. và thử nghiệm đơn vị của tôi dường như không nhận ra sự tồn tại của đồ đạc của tôi. Có vẻ như các bài kiểm tra đang đọc từ kết nối cơ sở dữ liệu cục bộ (default).Kiểm tra đơn vị CakePHP không công nhận đồ đạc

Đây là lớp TestCase tôi:

namespace App\Test\TestCase\Model\Table; 

use Cake\Datasource\ConnectionManager; 
use Cake\I18n\Time; 
use Cake\ORM\TableRegistry; 
use Cake\TestSuite\TestCase; 

/** 
* App\Model\Table\ScreensTable Test Case 
* 
* @property \App\Model\Table\ScreensTable ScreensTable 
*/ 
class ScreensTableTest extends TestCase 
{ 
    /** 
    * Fixtures 
    * 
    * @var array 
    */ 
    public $fixtures = [ 
     'app.screens' 
    ]; 

    /** 
    * setUp method 
    * 
    * @return void 
    */ 
    public function setUp() 
    { 
     parent::setUp(); 
     $config = TableRegistry::exists('Screens') ? [] : ['className' => 'App\Model\Table\ScreensTable']; 
     $this->Screens = TableRegistry::get('Screens', $config); 
    } 

    public testSomethingHere(){ 
     $newScreen = $this->Screens->newEntity(['who' => 'cares']); 
     $screen = $this->Screens->save($newScreen); 
     //^This record will show up in my app's DB! 
    } 

Dưới đây là file fixture của tôi:

<?php 
namespace App\Test\Fixture; 

use App\TestSuite\Fixture\TestFixture; 

/** 
* ScreensFixture 
* 
*/ 
class ScreensFixture extends TestFixture 
{ 
    /** 
    * Records 
    * 
    * @var array 
    */ 
    public $records = [ 
     [ 
      'id' => 2, 
      'name' => 'Bla bla', 
     ], 
     ... 

Khi tôi debug() giá trị trả về từ $this->Screens->find('all')->order(['id'=>'ASC'])->first() tôi thấy kỷ lục REAL đầu tiên trong DB. Không phải vật cố của tôi.

Tôi đang thiếu gì?

UPDATE1: Dưới đây là các kết nối được xác định trong tập tin cấu hình của tôi:

'Datasources' => [ 
    'default' => [ 
     'className' => 'Cake\Database\Connection', 
     'driver' => 'Cake\Database\Driver\Postgres', 
     'persistent' => false, 
     'host' => 'localhost', 
     'port' => '5432', 
     'username' => 'postgres', 
     'password' => 'postgres', 
     'database' => 'transitscreen', 
     'encoding' => 'utf8', 
     'timezone' => 'UTC', 
     'cacheMetadata' => true, 
     'quoteIdentifiers' => false, 
    ], 
    /** 
    * Used by test suites 
    */ 
    'test' => [ 
     'className' => 'Cake\Database\Connection', 
     'driver' => 'Cake\Database\Driver\Postgres', 
     'persistent' => false, 
     'host' => 'localhost', 
     'port' => '5432', 
     'username' => 'postgres', 
     'password' => 'postgres', 
     'database' => 'ts_test', 
     'encoding' => 'utf8', 
     'timezone' => 'UTC', 
     'cacheMetadata' => true, 
     'quoteIdentifiers' => false, 
    ], 
], 

Cập nhật

Nội dung App\Test\Fixture (Đừng nghĩ rằng có điều gì thú vị xảy ra ở đây ... Tôi nghĩ rằng nó chỉ mở rộng lớp bánh cố định)

<?php 

namespace App\TestSuite\Fixture; 

use Cake\I18n\Time; 
use Cake\TestSuite\Fixture\TestFixture as CoreTestFixture; 

/** 
* Override core TestFixture. 
*/ 
class TestFixture extends CoreTestFixture 
{ 

    /** 
    * Initialize the fixture. 
    * 
    * @return void 
    * @throws \Cake\ORM\Exception\MissingTableClassException When importing from a table that does not exist. 
    */ 
    public function init() 
    { 
     $this->setRecordTimestamps(); 
     $this->encodeJsonColumns(); 

     parent::init(); 
    } 

    /** 
    * Set record timestamps. 
    * 
    * - created 
    * - modified 
    * 
    * @return void 
    */ 
    public function setRecordTimestamps() 
    { 
     foreach (['created', 'modified'] as $timestampField) { 
      if (array_key_exists($timestampField, $this->fields)) { 
       foreach ($this->records as &$record) { 
        $record[$timestampField] = new Time(); 
       } 
      } 
     } 
    } 

    /** 
    * Encode JSON columns. 
    * 
    * Bake will output JSON fields as arrays (because of the `JsonType` object mapped to this field) but since 
    * migrations execute directly against the database we need to encode these fields manually before insertion. 
    * 
    * @return void 
    */ 
    public function encodeJsonColumns() 
    { 
     $fixtureName = namespaceSplit(get_called_class())[1]; 
     if ($fixtureName === 'DatasourcesFixture' && array_key_exists('widget_options', $this->fields)) { 
      foreach ($this->records as &$record) { 
       $record['widget_options'] = json_encode($record['widget_options']); 
      } 
     } 
    } 
} 
+0

@ AD7six Tôi đã thêm cấu hình cơ sở dữ liệu của mình ở trên. Nó có vẻ đúng với tôi, như tôi đã 'mặc định' và' kiểm tra' được định nghĩa và tôi không nhận được bất kỳ lỗi kết nối DB khi tôi chạy thử nghiệm của tôi. – emersonthis

+0

Bạn đã thêm trình lắng nghe lịch thi đấu vào tệp phpunit.xml chưa? –

+0

@ code-kobold oooo Tôi không chắc chắn! Vui lòng gửi và trả lời với những gì tôi nên kiểm tra. – emersonthis

Trả lời

1

Bạn đã cố gắng thu hẹp phạm vi của vấn đề theo:

  1. Loại bỏ các thừa kế với App \ Test \ Lịch thi đấu
  2. Xác định sơ đồ thi đấu bằng cách tuyên bố các $fields tài sản.
  3. Kích hoạt query logging

Cả hai sẽ là biện pháp tạm thời để thử và làm cho vấn đề đơn giản để giải quyết. Bạn cũng có thể muốn sử dụng cờ --debug khi chạy PHPUnit. Điều này sẽ làm cho CakePHP xuất tất cả các SQL được sử dụng để tạo các đồ đạc.

Các vấn đề liên quan