* 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>
169 lines
4.7 KiB
PHP
169 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* REST API controller for the reconstitution calculator.
|
|
*
|
|
* What: Exposes presets and calculation endpoints for the frontend JS.
|
|
* Who calls it: rest_api_init hook in PRC_Calculator::boot().
|
|
* Dependencies: PRC_Preset_Provider, PRC_Math.
|
|
*
|
|
* @see class-prc-preset-provider.php — Data source for presets.
|
|
* @see class-prc-math.php — Calculation engine.
|
|
* @see class-prc-shortcode.php — Frontend JS consumes these endpoints.
|
|
* @package PeptideReconstitutionCalculator
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* REST endpoints for the reconstitution calculator.
|
|
*
|
|
* Namespace: prc/v1
|
|
* Endpoints:
|
|
* GET /presets — List all available peptide presets.
|
|
* GET /presets/{slug} — Get a single preset by slug.
|
|
* POST /calculate — Run reconstitution math server-side.
|
|
*/
|
|
class PRC_Rest_Controller {
|
|
|
|
/**
|
|
* REST namespace.
|
|
*/
|
|
private const NAMESPACE = 'prc/v1';
|
|
|
|
/**
|
|
* Register all routes.
|
|
*
|
|
* @return void Side effect: registers REST routes.
|
|
*/
|
|
public function register_routes(): void {
|
|
register_rest_route(
|
|
self::NAMESPACE,
|
|
'/presets',
|
|
array(
|
|
'methods' => 'GET',
|
|
'callback' => array( $this, 'get_presets' ),
|
|
'permission_callback' => '__return_true',
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
self::NAMESPACE,
|
|
'/presets/(?P<slug>[a-z0-9-]+)',
|
|
array(
|
|
'methods' => 'GET',
|
|
'callback' => array( $this, 'get_preset' ),
|
|
'permission_callback' => '__return_true',
|
|
'args' => array(
|
|
'slug' => array(
|
|
'type' => 'string',
|
|
'sanitize_callback' => 'sanitize_title',
|
|
'required' => true,
|
|
),
|
|
),
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
self::NAMESPACE,
|
|
'/calculate',
|
|
array(
|
|
'methods' => 'POST',
|
|
'callback' => array( $this, 'calculate' ),
|
|
'permission_callback' => '__return_true',
|
|
'args' => $this->get_calculate_args(),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET /presets — return all available presets.
|
|
*
|
|
* @param \WP_REST_Request $request Request object.
|
|
* @return \WP_REST_Response
|
|
*/
|
|
public function get_presets( \WP_REST_Request $request ): \WP_REST_Response { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
|
$provider = new PRC_Preset_Provider();
|
|
$presets = $provider->get_all_presets();
|
|
|
|
return new \WP_REST_Response( $presets, 200 );
|
|
}
|
|
|
|
/**
|
|
* GET /presets/{slug} — return a single preset.
|
|
*
|
|
* @param \WP_REST_Request $request Request with 'slug' param.
|
|
* @return \WP_REST_Response|\WP_Error
|
|
*/
|
|
public function get_preset( \WP_REST_Request $request ) {
|
|
$slug = $request->get_param( 'slug' );
|
|
$provider = new PRC_Preset_Provider();
|
|
$presets = $provider->get_all_presets();
|
|
|
|
foreach ( $presets as $preset ) {
|
|
if ( $preset['slug'] === $slug ) {
|
|
return new \WP_REST_Response( $preset, 200 );
|
|
}
|
|
}
|
|
|
|
return new \WP_Error(
|
|
'prc_preset_not_found',
|
|
__( 'Preset not found.', 'peptide-reconstitution-calculator' ),
|
|
array( 'status' => 404 )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* POST /calculate — run reconstitution math.
|
|
*
|
|
* Accepts vial_mg, water_ml, desired_dose, dose_unit and returns
|
|
* concentration, injection volume, syringe units, and doses per vial.
|
|
*
|
|
* @param \WP_REST_Request $request Request with calculation params.
|
|
* @return \WP_REST_Response
|
|
*/
|
|
public function calculate( \WP_REST_Request $request ): \WP_REST_Response {
|
|
$result = PRC_Math::calculate(
|
|
(float) $request->get_param( 'vial_mg' ),
|
|
(float) $request->get_param( 'water_ml' ),
|
|
(float) $request->get_param( 'desired_dose' ),
|
|
(string) $request->get_param( 'dose_unit' )
|
|
);
|
|
|
|
return new \WP_REST_Response( $result, 200 );
|
|
}
|
|
|
|
/**
|
|
* Define validation args for the /calculate endpoint.
|
|
*
|
|
* @return array<string, array<string, mixed>>
|
|
*/
|
|
private function get_calculate_args(): array {
|
|
return array(
|
|
'vial_mg' => array(
|
|
'type' => 'number',
|
|
'required' => true,
|
|
'sanitize_callback' => 'floatval',
|
|
'validate_callback' => fn( $v ) => is_numeric( $v ) && (float) $v > 0,
|
|
),
|
|
'water_ml' => array(
|
|
'type' => 'number',
|
|
'required' => true,
|
|
'sanitize_callback' => 'floatval',
|
|
'validate_callback' => fn( $v ) => is_numeric( $v ) && (float) $v > 0,
|
|
),
|
|
'desired_dose' => array(
|
|
'type' => 'number',
|
|
'required' => true,
|
|
'sanitize_callback' => 'floatval',
|
|
'validate_callback' => fn( $v ) => is_numeric( $v ) && (float) $v > 0,
|
|
),
|
|
'dose_unit' => array(
|
|
'type' => 'string',
|
|
'required' => true,
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
'validate_callback' => fn( $v ) => in_array( $v, array( 'mcg', 'mg' ), true ),
|
|
),
|
|
);
|
|
}
|
|
}
|