Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [CakePHP] Testowanie komponentu
gcdreak
post 12.11.2015, 16:37:46
Post #1





Grupa: Zarejestrowani
Postów: 365
Pomógł: 8
Dołączył: 16.12.2008

Ostrzeżenie: (0%)
-----


Hej,
Do testów wykorzystuje bazę Sqlite tworzoną w pamięci.

W komponencie mam taki kod
  1. class TicketComponent extends Component
  2. {
  3. protected $ticketValidityPeriodInHours = 24;
  4.  
  5. public function initialize(array $config)
  6. {
  7. parent::initialize($config);
  8. $this->TicketsTable = TableRegistry::get('Tickets');
  9. }
  10. public function generateTicket($data = null)
  11. {
  12. $ticket = $this->TicketsTable->newEntity();
  13. $ticket->expires = date(
  14. DB_DATE_TIME,
  15. strtotime(sprintf('+%d hours', $this->ticketValidityPeriodInHours))
  16. );
  17. $ticket->data = $data;
  18. $ticket->hash = $this->generateRandomHash();
  19.  
  20. return $this->TicketsTable->save($ticket);
  21. }
  22. // Inne metody
  23. }


Teraz chciałbym napisać do tego komponentu test. Wstępnie wygląda to następująco
  1. class TicketComponentTest extends TestCase
  2. {
  3. public $component = null;
  4. public $controller = null;
  5.  
  6. public function setUp()
  7. {
  8. parent::setUp();
  9.  
  10. $request = new Request();
  11. $response = new Response();
  12. $this->controller = $this->getMock(
  13. 'Cake\Controller\Controller',
  14. null,
  15. [$request, $response]
  16. );
  17. $registry = new ComponentRegistry($this->controller);
  18. $this->component = new TicketComponent($registry);
  19. }
  20.  
  21. public function testGenerateTicket()
  22. {
  23. $ticket = $this->component->generateTicket();
  24.  
  25. $this->assertInstanceOf(Ticket::class, $ticket, 'Returns Ticket object');
  26. $this->assertTrue(strtotime($ticket->expires) > time(), 'Expiry time is from future');
  27. $this->assertNotEmpty($ticket->hash, 'Hash is not empty');
  28. }
  29. }

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
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.06.2025 - 21:03