3.0 KiB
3.0 KiB
🧪 Testowanie shopPRO
Szybki start
Uruchom wszystkie testy
./test.bat # Windows CMD (z nazwami testów)
./test-simple.bat # Tylko kropki (szybki)
./test-debug.bat # Pełne szczegóły (debug)
./test.ps1 # PowerShell (autodetekcja PHP)
./test.sh # Git Bash
Konkretny plik
./test.bat tests/Unit/Domain/Product/ProductRepositoryTest.php
./test.ps1 tests/Unit/admin/Controllers/ArticlesControllerTest.php
Tryby wyświetlania
1. TestDox (domyślny) - Czytelna lista ✅
./test.bat
Wynik:
Product Repository
✔ Get quantity returns correct value [2.78 ms]
✔ Get quantity returns null when product not found
✔ Find returns product data
2. Simple - Tylko kropki 📊
./test-simple.bat
Wynik:
..... 5 / 5 (100%)
OK (5 tests, 11 assertions)
3. Debug - Wszystkie szczegóły 🔬
./test-debug.bat
Wynik:
Test 'testGetQuantity' started
Test 'testGetQuantity' ended
...
Interpretacja wyników
✅ Sukces
..... 5 / 5 (100%)
OK (5 tests, 11 assertions)
.= test przeszedł- Wszystko działa!
❌ Błąd
..E.. 5 / 5 (100%)
ERRORS!
Tests: 5, Assertions: 8, Errors: 1.
E= Error - błąd w kodzie- Sprawdź szczegóły powyżej
❌ Niepowodzenie
..F.. 5 / 5 (100%)
FAILURES!
Tests: 5, Assertions: 11, Failures: 1.
F= Failure - asercja się nie powiodła- Oczekiwano innej wartości
Przykładowy test
public function testGetQuantityReturnsCorrectValue()
{
// Arrange - Przygotuj
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('get')->willReturn(42);
$repository = new ProductRepository($mockDb);
// Act - Wykonaj
$quantity = $repository->getQuantity(123);
// Assert - Sprawdź
$this->assertEquals(42, $quantity);
}
Dodawanie nowych testów
- Utwórz plik w
tests/Unit/Domain/{Module}/{Class}Test.php - Rozszerz
TestCase - Metody testowe zaczynaj od
test - Użyj pattern AAA (Arrange, Act, Assert)
Asercje
$this->assertEquals(expected, actual); // Równość
$this->assertIsInt($value); // Typ
$this->assertNull($value); // Null
$this->assertTrue($condition); // Prawda
$this->assertCount(3, $array); // Rozmiar
Mockowanie
// Prosty mock
$mock = $this->createMock(\medoo::class);
$mock->method('get')->willReturn('wartość');
// Z weryfikacją
$mock->expects($this->once())
->method('get')
->with($this->equalTo('tabela'))
->willReturn(42);
📚 Więcej: tests/README.md