137 lines
3.0 KiB
Markdown
137 lines
3.0 KiB
Markdown
# 🧪 Testowanie shopPRO
|
|
|
|
## Szybki start
|
|
|
|
### Uruchom wszystkie testy
|
|
```bash
|
|
./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
|
|
```bash
|
|
./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 ✅
|
|
```bash
|
|
./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 📊
|
|
```bash
|
|
./test-simple.bat
|
|
```
|
|
Wynik:
|
|
```
|
|
..... 5 / 5 (100%)
|
|
OK (5 tests, 11 assertions)
|
|
```
|
|
|
|
### 3. Debug - Wszystkie szczegóły 🔬
|
|
```bash
|
|
./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
|
|
|
|
```php
|
|
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
|
|
|
|
1. Utwórz plik w `tests/Unit/Domain/{Module}/{Class}Test.php`
|
|
2. Rozszerz `TestCase`
|
|
3. Metody testowe zaczynaj od `test`
|
|
4. Użyj pattern **AAA** (Arrange, Act, Assert)
|
|
|
|
## Asercje
|
|
|
|
```php
|
|
$this->assertEquals(expected, actual); // Równość
|
|
$this->assertIsInt($value); // Typ
|
|
$this->assertNull($value); // Null
|
|
$this->assertTrue($condition); // Prawda
|
|
$this->assertCount(3, $array); // Rozmiar
|
|
```
|
|
|
|
## Mockowanie
|
|
|
|
```php
|
|
// 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](tests/README.md)
|