Files
peptide-reconstitution-calc…/tests/unit/RestControllerTest.php
peptiderepo 31ad0463f7 Adopt reusable CI standard + PHPUnit suite (#8)
* feat(ci): adopt estate CI + add PHPUnit suite from scratch (v1.2.0)

- Add PHPUnit 9.6 test suite (WP stubs, tests: stubs) — 40 tests across 4 files:
  MathTest: 25 tests covering all PRC_Math public methods and calculate()
  orchestrator with real peptide scenarios (BPC-157/TB-500/Semaglutide/MK-677/
  Ipamorelin) plus edge cases (zero water, negative inputs, floor rounding,
  rounding precision, mcg↔mg conversion).
  DefaultPresetsTest: 11 tests — all 8 expected slugs, required keys per preset
  (dataProvider), dose_unit validity, min<max range, source field, spot-checks.
  RestControllerTest: 7 tests — calculate() 200 + correct values (mcg+mg),
  get_presets() 200, get_preset() 200 for known slug / WP_Error for unknown.
  PresetProviderTest: 4 tests — fallback to defaults sans PR Core, slug key
  present, cache invalidation, idempotent invalidation.
- Add phpunit.xml.dist + tests/bootstrap.php (WP function/class stubs).
- Add composer test script: vendor/bin/phpunit --configuration phpunit.xml.dist.
- Add phpunit/phpunit ^9.6 + yoast/phpunit-polyfills ^2.0 to require-dev.
- Replace standalone ci.yml with thin caller to estate reusable workflow:
  uses: peptiderepo/peptide-e2e/.github/workflows/ci.yml@main
  with: tests: stubs, has_js: true
  permissions: contents: write (required for phpcbf commit-back)
  workflow_call (required for deploy.yml gate).
- deploy.yml unchanged — already gates on ./ci.yml + uses shared deploy-app.yml.
- Version bump 1.1.0 → 1.2.0.

Agent-Session: claude/adopt-ci-20260616

* style: auto-fix PHPCS violations via phpcbf

* fix(ci): resolve latent PHPCS + JS lint failures

Latent debt surfaced by the reusable PHPCS job (previously continue-on-error).

PHPCS fixes (all pre-existing, not introduced by this PR):
- Add @package PeptideReconstitutionCalculator to file docblocks in all 12 PHP
  files (previously missing; phpcbf cannot insert missing docblock tags).
- Replace two short-ternary ?: operators in class-prc-preset-provider.php with
  full ternary (Universal.Operators.DisallowShortTernary.Found; lines 136+152).

JS lint fix:
- Add package.json + package-lock.json (no deps; lint script = node --check)
  so the reusable lint-js job's npm ci / cache: npm steps succeed. The
  calculator ships vanilla JS with no build toolchain — package.json is the
  minimal stub the reusable workflow requires for has_js: true.

Agent-Session: claude/adopt-ci-20260616

* style: auto-fix PHPCS violations via phpcbf

* fix(phpcs): suppress intentional warnings with phpcs:ignore annotations

uninstall.php: direct wpdb->query() intentional in uninstall (no caching
available); add phpcs:ignore DirectDatabaseQuery.DirectQuery and NoCaching.
class-prc-rest-controller.php get_presets(): request param required by
WP REST API callback contract; add phpcs:ignore UnusedFunctionParameter.

Agent-Session: claude/adopt-ci-20260616

---------

Co-authored-by: peptiderepo <bot@peptiderepo.com>
Co-authored-by: peptiderepo <peptiderepo@users.noreply.github.com>
2026-06-17 01:25:11 +07:00

151 lines
4.5 KiB
PHP
Executable File

<?php
/**
* Unit tests for PRC_Rest_Controller.
*
* Tests the calculate() method callback using the WP_REST_Request stub.
* The get_presets() / get_preset() methods are thin wrappers over
* PRC_Preset_Provider, which is covered by PresetProviderTest.
*
* @package PRC\Tests\Unit
*/
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for PRC_Rest_Controller REST callback methods.
*/
class RestControllerTest extends TestCase {
private PRC_Rest_Controller $controller;
protected function setUp(): void {
$this->controller = new PRC_Rest_Controller();
}
// ── /calculate callback ───────────────────────────────────────────
/**
* calculate() returns a WP_REST_Response with status 200.
*/
public function test_calculate_returns_200_response(): void {
$request = new WP_REST_Request( 'POST' );
$request->set_param( 'vial_mg', '5' );
$request->set_param( 'water_ml', '2' );
$request->set_param( 'desired_dose', '250' );
$request->set_param( 'dose_unit', 'mcg' );
$response = $this->controller->calculate( $request );
$this->assertInstanceOf( WP_REST_Response::class, $response );
$this->assertSame( 200, $response->get_status() );
}
/**
* calculate() returns the correct BPC-157 math results.
*
* 5 mg / 2 mL / 250 mcg → concentration 2.5, syringe units 10.
*/
public function test_calculate_bpc157_correct_values(): void {
$request = new WP_REST_Request( 'POST' );
$request->set_param( 'vial_mg', '5' );
$request->set_param( 'water_ml', '2' );
$request->set_param( 'desired_dose', '250' );
$request->set_param( 'dose_unit', 'mcg' );
$response = $this->controller->calculate( $request );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertEqualsWithDelta( 2.5, $data['concentration_mg_per_ml'], 0.0001 );
$this->assertEqualsWithDelta( 10.0, $data['syringe_units'], 0.1 );
$this->assertSame( 20.0, $data['doses_per_vial'] );
}
/**
* calculate() accepts mg dose unit correctly.
*
* 5 mg / 2 mL / 0.5 mg → syringe units 20.
*/
public function test_calculate_mg_dose_unit(): void {
$request = new WP_REST_Request( 'POST' );
$request->set_param( 'vial_mg', '5' );
$request->set_param( 'water_ml', '2' );
$request->set_param( 'desired_dose', '0.5' );
$request->set_param( 'dose_unit', 'mg' );
$response = $this->controller->calculate( $request );
$data = $response->get_data();
$this->assertEqualsWithDelta( 20.0, $data['syringe_units'], 0.1 );
$this->assertSame( 10.0, $data['doses_per_vial'] );
}
/**
* calculate() returns all expected keys in the response data.
*/
public function test_calculate_response_has_expected_keys(): void {
$request = new WP_REST_Request( 'POST' );
$request->set_param( 'vial_mg', '5' );
$request->set_param( 'water_ml', '2' );
$request->set_param( 'desired_dose', '250' );
$request->set_param( 'dose_unit', 'mcg' );
$data = $this->controller->calculate( $request )->get_data();
$expected_keys = [
'concentration_mg_per_ml',
'concentration_mcg_per_unit',
'injection_volume_ml',
'syringe_units',
'doses_per_vial',
'dose_unit',
];
foreach ( $expected_keys as $key ) {
$this->assertArrayHasKey( $key, $data, "Missing key: $key" );
}
}
/**
* get_presets() returns a 200 response with an array.
*/
public function test_get_presets_returns_200_with_array(): void {
$request = new WP_REST_Request( 'GET' );
$response = $this->controller->get_presets( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertIsArray( $response->get_data() );
}
/**
* get_preset() returns a WP_Error for an unknown slug.
*/
public function test_get_preset_unknown_slug_returns_wp_error(): void {
$request = new WP_REST_Request( 'GET' );
$request->set_param( 'slug', 'nonexistent-peptide-xyz' );
$result = $this->controller->get_preset( $request );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertSame( 'prc_preset_not_found', $result->get_error_code() );
}
/**
* get_preset() returns a 200 response for a known slug.
*/
public function test_get_preset_known_slug_returns_200(): void {
$request = new WP_REST_Request( 'GET' );
$request->set_param( 'slug', 'bpc-157' );
$result = $this->controller->get_preset( $request );
$this->assertInstanceOf( WP_REST_Response::class, $result );
$this->assertSame( 200, $result->get_status() );
$data = $result->get_data();
$this->assertSame( 'bpc-157', $data['slug'] );
}
}