Add new version 0.238 zip file containing updated ProductRepository and Product class files
This commit is contained in:
136
tests/Unit/Domain/Product/ProductRepositoryTest.php
Normal file
136
tests/Unit/Domain/Product/ProductRepositoryTest.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Domain\Product;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Domain\Product\ProductRepository;
|
||||
|
||||
/**
|
||||
* Testy jednostkowe dla ProductRepository
|
||||
*
|
||||
* Testujemy izolowaną logikę repozytorium z mockami bazy danych
|
||||
*/
|
||||
class ProductRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test pobierania ilości produktu - przypadek sukcesu
|
||||
*/
|
||||
public function testGetQuantityReturnsCorrectValue()
|
||||
{
|
||||
// Arrange (Przygotowanie)
|
||||
// Tworzymy mock bazy danych
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
// Konfigurujemy mock - metoda get() zwróci 42
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('pp_shop_products'),
|
||||
$this->equalTo('quantity'),
|
||||
$this->equalTo(['id' => 123])
|
||||
)
|
||||
->willReturn(42);
|
||||
|
||||
$repository = new ProductRepository($mockDb);
|
||||
|
||||
// Act (Działanie)
|
||||
$quantity = $repository->getQuantity(123);
|
||||
|
||||
// Assert (Asercja)
|
||||
$this->assertEquals(42, $quantity);
|
||||
$this->assertIsInt($quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test pobierania ilości - produkt nie istnieje
|
||||
*/
|
||||
public function testGetQuantityReturnsNullWhenProductNotFound()
|
||||
{
|
||||
// Arrange
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
// Medoo zwraca false gdy nie znajdzie rekordu
|
||||
$mockDb->method('get')->willReturn(false);
|
||||
|
||||
$repository = new ProductRepository($mockDb);
|
||||
|
||||
// Act
|
||||
$quantity = $repository->getQuantity(999);
|
||||
|
||||
// Assert
|
||||
$this->assertNull($quantity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test pobierania produktu po ID
|
||||
*/
|
||||
public function testFindReturnsProductData()
|
||||
{
|
||||
// Arrange
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$expectedProduct = [
|
||||
'id' => 123,
|
||||
'name' => 'Test Product',
|
||||
'quantity' => 10,
|
||||
'price_brutto' => '99.99'
|
||||
];
|
||||
|
||||
$mockDb->method('get')->willReturn($expectedProduct);
|
||||
|
||||
$repository = new ProductRepository($mockDb);
|
||||
|
||||
// Act
|
||||
$product = $repository->find(123);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals($expectedProduct, $product);
|
||||
$this->assertIsArray($product);
|
||||
$this->assertEquals(123, $product['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test aktualizacji ilości produktu
|
||||
*/
|
||||
public function testUpdateQuantitySuccess()
|
||||
{
|
||||
// Arrange
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
// Medoo zwraca PDOStatement w przypadku sukcesu
|
||||
$mockDb->expects($this->once())
|
||||
->method('update')
|
||||
->with(
|
||||
$this->equalTo('pp_shop_products'),
|
||||
$this->equalTo(['quantity' => 50]),
|
||||
$this->equalTo(['id' => 123])
|
||||
)
|
||||
->willReturn($this->createMock(\PDOStatement::class));
|
||||
|
||||
$repository = new ProductRepository($mockDb);
|
||||
|
||||
// Act
|
||||
$result = $repository->updateQuantity(123, 50);
|
||||
|
||||
// Assert
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test typu zwracanej wartości
|
||||
*/
|
||||
public function testGetQuantityReturnsInteger()
|
||||
{
|
||||
// Arrange
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')->willReturn('25'); // Baza może zwrócić string
|
||||
|
||||
$repository = new ProductRepository($mockDb);
|
||||
|
||||
// Act
|
||||
$quantity = $repository->getQuantity(123);
|
||||
|
||||
// Assert
|
||||
$this->assertIsInt($quantity); // Sprawdzamy czy konwersja na int zadziałała
|
||||
$this->assertEquals(25, $quantity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user