Do testów wykorzystuje bazę Sqlite tworzoną w pamięci.
W komponencie mam taki kod
class TicketComponent extends Component { protected $ticketValidityPeriodInHours = 24; { parent::initialize($config); $this->TicketsTable = TableRegistry::get('Tickets'); } public function generateTicket($data = null) { $ticket = $this->TicketsTable->newEntity(); DB_DATE_TIME, ); $ticket->data = $data; $ticket->hash = $this->generateRandomHash(); return $this->TicketsTable->save($ticket); } // Inne metody }
Teraz chciałbym napisać do tego komponentu test. Wstępnie wygląda to następująco
class TicketComponentTest extends TestCase { public $component = null; public $controller = null; public function setUp() { parent::setUp(); $request = new Request(); $response = new Response(); $this->controller = $this->getMock( 'Cake\Controller\Controller', null, [$request, $response] ); $registry = new ComponentRegistry($this->controller); $this->component = new TicketComponent($registry); } public function testGenerateTicket() { $ticket = $this->component->generateTicket(); $this->assertInstanceOf(Ticket::class, $ticket, 'Returns Ticket object'); $this->assertNotEmpty($ticket->hash, 'Hash is not empty'); } }
Kiedy odpalam phpunit uczywiście dostaję błąd
Cannot describe tickets. It has 0 columns.
Jest to zrozumiałe - tabela nie istnieje w testowej bazie
Czy ktoś mógłby mi pokazać jak zrobić mock-a by zastąpić użycie TicketsTable bym mógł swobodnie testować TicketComponent lub jak iżyć Fixture w tym wypadku.
Dziękuję za pomoc