You've already forked wc-composable-product
Add PHPUnit test suite, PSR-4 refactor, lint+test CI jobs (v1.3.1)
- 57 unit tests covering ProductType, StockManager, CartHandler, Plugin, Admin/ProductData, Admin/Settings using Brain Monkey + Mockery - WooCommerce class stubs for testing without WP installation - PHP lint and test jobs in release workflow (test gate blocks release) - PSR-4 namespace change: WC_Composable_Product -> Magdev\WcComposableProduct - PascalCase filenames for all classes under includes/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
75
tests/TestCase.php
Normal file
75
tests/TestCase.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Base Test Case
|
||||
*
|
||||
* @package Magdev\WcComposableProduct\Tests
|
||||
*/
|
||||
|
||||
namespace Magdev\WcComposableProduct\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use Brain\Monkey;
|
||||
|
||||
/**
|
||||
* Base test case with Brain Monkey and Mockery integration.
|
||||
*
|
||||
* All test classes should extend this instead of PHPUnit\Framework\TestCase.
|
||||
*/
|
||||
abstract class TestCase extends PHPUnitTestCase
|
||||
{
|
||||
use MockeryPHPUnitIntegration;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Monkey\setUp();
|
||||
|
||||
// Stub common WordPress translation functions (__(), _e(), esc_html__(), etc.)
|
||||
Monkey\Functions\stubTranslationFunctions();
|
||||
|
||||
// Stub common WordPress escaping functions (esc_html(), esc_attr(), etc.)
|
||||
Monkey\Functions\stubEscapeFunctions();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Monkey\tearDown();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Mockery mock of WC_Product with sensible defaults.
|
||||
*
|
||||
* @param array $overrides Method return value overrides
|
||||
* @return \Mockery\MockInterface
|
||||
*/
|
||||
protected function createProductMock(array $overrides = []): \Mockery\MockInterface
|
||||
{
|
||||
$defaults = [
|
||||
'get_id' => 100,
|
||||
'get_name' => 'Test Product',
|
||||
'get_type' => 'simple',
|
||||
'get_price' => '10.00',
|
||||
'get_regular_price' => '10.00',
|
||||
'get_price_html' => '<span>$10.00</span>',
|
||||
'get_permalink' => 'https://example.com/product/test',
|
||||
'get_image_id' => 1,
|
||||
'get_stock_quantity' => null,
|
||||
'get_stock_status' => 'instock',
|
||||
'is_purchasable' => true,
|
||||
'is_in_stock' => true,
|
||||
'managing_stock' => false,
|
||||
'backorders_allowed' => false,
|
||||
];
|
||||
|
||||
$config = array_merge($defaults, $overrides);
|
||||
$mock = \Mockery::mock('WC_Product');
|
||||
|
||||
foreach ($config as $method => $return) {
|
||||
$mock->shouldReceive($method)->andReturn($return)->byDefault();
|
||||
}
|
||||
|
||||
return $mock;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user