Testing
Testing your views with WPZylos Views.
Unit Testing
Testing View Rendering
use PHPUnit\Framework\TestCase;
use WPZylos\Framework\Views\View;
class ViewTest extends TestCase
{
public function testRenderReturnsString(): void
{
$html = View::render('simple', ['name' => 'John']);
$this->assertIsString($html);
$this->assertStringContainsString('John', $html);
}
public function testViewExists(): void
{
$this->assertTrue(View::exists('pages/dashboard'));
$this->assertFalse(View::exists('pages/nonexistent'));
}
}
Testing View Content
class ProductViewTest extends TestCase
{
public function testProductListShowsAllProducts(): void
{
$products = [
(object) ['name' => 'Product 1', 'price' => 10],
(object) ['name' => 'Product 2', 'price' => 20],
];
$html = View::render('products/list', compact('products'));
$this->assertStringContainsString('Product 1', $html);
$this->assertStringContainsString('Product 2', $html);
}
public function testEmptyListShowsMessage(): void
{
$html = View::render('products/list', ['products' => []]);
$this->assertStringContainsString('No products found', $html);
}
}
Testing Shared Variables
class SharedVariablesTest extends TestCase
{
public function testSharedVariablesAvailableInViews(): void
{
View::share('siteName', 'Test Site');
$html = View::render('header');
$this->assertStringContainsString('Test Site', $html);
}
}
Running Tests
composer run test