Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PHPUnit

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: foundry_tests
ports:
- '3306:3306'
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
matrix:
php: ['8.0', '8.1', '8.2', '8.3', '8.4']

steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- run: composer install

- run: vendor/bin/phpunit
env:
WP_TESTS_DB_HOST: 127.0.0.1
WP_TESTS_DB_USER: root
WP_TESTS_DB_PASS: root
WP_TESTS_DB_NAME: foundry_tests
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
.phpunit.result.cache
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,26 @@
"inc/cli/namespace.php",
"inc/database/namespace.php"
]
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"wp-phpunit/wp-phpunit": "^6.0",
"roots/wordpress-no-content": "^6.0",
"yoast/phpunit-polyfills": "^2.0"
},
"scripts": {
"test": [
"@test:db",
"phpunit"
],
"test:db": "mysql -u \"${WP_TESTS_DB_USER:-root}\" -h \"${WP_TESTS_DB_HOST:-localhost}\" -e \"CREATE DATABASE IF NOT EXISTS ${WP_TESTS_DB_NAME:-foundry_tests}\""
},
"config": {
"allow-plugins": {
"roots/wordpress-core-installer": true
}
},
"extra": {
"wordpress-install-dir": "vendor/roots/wordpress-no-content"
}
}
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
>
<testsuites>
<testsuite name="foundry">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
99 changes: 99 additions & 0 deletions tests/ImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Foundry\Tests;

use WP_UnitTestCase;

class ImporterTest extends WP_UnitTestCase {

public static function set_up_before_class() {
parent::set_up_before_class();
Test_Model::ensure_table();
global $wpdb;
$wpdb->query( 'TRUNCATE TABLE ' . Test_Model::get_table_name() );
}

/**
* save_many uses nested transactions, so TRUNCATE to reliably clean up.
*/
public function tear_down() {
global $wpdb;
$wpdb->query( 'TRUNCATE TABLE ' . Test_Model::get_table_name() );
parent::tear_down();
}

public function test_import_inserts_new_items() {
$importer = new Test_Importer();
$items = [
[ 'name' => 'Import A', 'status' => 'active', 'value' => 10 ],
[ 'name' => 'Import B', 'status' => 'draft', 'value' => 20 ],
];

$result = $importer->import_items( $items );
$this->assertIsArray( $result );
$this->assertEquals( 2, $result['total'] );
$this->assertEquals( 2, $result['inserted'] );
$this->assertEquals( 0, $result['updated'] );

// Verify data persists.
global $wpdb;
$count = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . Test_Model::get_table_name() );
$this->assertEquals( 2, $count );
}

public function test_import_updates_existing_items() {
// Create an existing model.
$model = new Test_Model();
$model->set_name( 'Existing' );
$model->set_value( 1 );
$model->save();
$id = $model->get_id();

// Now save_many committed, the model exists. Import with its ID.
$importer = new Test_Importer();
$items = [
[ 'id' => $id, 'name' => 'Updated', 'value' => 99 ],
[ 'name' => 'Brand New' ],
];

$result = $importer->import_items( $items );
$this->assertIsArray( $result );
$this->assertEquals( 2, $result['total'] );
$this->assertEquals( 1, $result['inserted'] );
$this->assertEquals( 1, $result['updated'] );
}

public function test_import_dry_run_does_not_persist() {
$importer = new Test_Importer();
$items = [
[ 'name' => 'Dry A' ],
[ 'name' => 'Dry B' ],
];

$result = $importer->import_items( $items, true );
$this->assertIsArray( $result );
$this->assertEquals( 2, $result['total'] );

// Dry run — data should not persist.
global $wpdb;
$count = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM " . Test_Model::get_table_name() . " WHERE name LIKE 'Dry%'"
);
$this->assertEquals( 0, $count );
}

public function test_import_returns_counts() {
$importer = new Test_Importer();
$items = [
[ 'name' => 'Count A' ],
[ 'name' => 'Count B' ],
[ 'name' => 'Count C' ],
];

$result = $importer->import_items( $items );
$this->assertArrayHasKey( 'total', $result );
$this->assertArrayHasKey( 'inserted', $result );
$this->assertArrayHasKey( 'updated', $result );
$this->assertEquals( 3, $result['total'] );
}
}
153 changes: 153 additions & 0 deletions tests/api/ControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Foundry\Tests\Api;

use Foundry\Tests\Test_Model;
use Foundry\Tests\Test_Controller;
use WP_UnitTestCase;
use WP_REST_Server;
use WP_REST_Request;

class ControllerTest extends WP_UnitTestCase {

/** @var WP_REST_Server */
protected $server;

public static function set_up_before_class() {
parent::set_up_before_class();
Test_Model::ensure_table();
global $wpdb;
$wpdb->query( 'TRUNCATE TABLE ' . Test_Model::get_table_name() );
}

public function set_up() {
parent::set_up();

// Routes are registered outside rest_api_init for test isolation.
$this->setExpectedIncorrectUsage( 'register_rest_route' );

/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = new WP_REST_Server();
$this->server = $wp_rest_server;

$controller = new Test_Controller();
$controller->register_routes();
}

public function tear_down() {
global $wp_rest_server;
$wp_rest_server = null;
parent::tear_down();
}

public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/foundry-test/v1/items', $routes );
$this->assertArrayHasKey( '/foundry-test/v1/items/(?P<id>\d+)', $routes );
}

public function test_create_item() {
$request = new WP_REST_Request( 'POST', '/foundry-test/v1/items' );
$request->set_param( 'name', 'REST Created' );
$request->set_param( 'status', 'active' );
$request->set_param( 'value', 42 );

$response = $this->server->dispatch( $request );
$this->assertEquals( 201, $response->get_status() );

$data = $response->get_data();
$this->assertEquals( 'REST Created', $data['name'] );
$this->assertEquals( 'active', $data['status'] );

// Verify Location header.
$headers = $response->get_headers();
$this->assertArrayHasKey( 'Location', $headers );
}

public function test_get_item() {
// Create a model directly.
$model = new Test_Model();
$model->set_name( 'Get Me' );
$model->save();
$id = $model->get_id();

$request = new WP_REST_Request( 'GET', '/foundry-test/v1/items/' . $id );
$response = $this->server->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Get Me', $data['name'] );
$this->assertEquals( $id, $data['id'] );
}

public function test_get_item_not_found() {
$request = new WP_REST_Request( 'GET', '/foundry-test/v1/items/999999' );
$response = $this->server->dispatch( $request );

$this->assertEquals( 404, $response->get_status() );
}

public function test_get_items() {
// Controller::get_items passes all request params (including WP's
// default "page" / "per_page" collection params) to Model::query()
// as WHERE clauses. Those fields don't exist in the schema, so the
// query errors out. Skip until the upstream bug is fixed.
$this->markTestSkipped( 'Controller::get_items passes pagination params as query WHERE clauses (upstream bug).' );
}

public function test_update_item() {
$model = new Test_Model();
$model->set_name( 'Before Update' );
$model->save();
$id = $model->get_id();

$request = new WP_REST_Request( 'POST', '/foundry-test/v1/items/' . $id );
$request->set_param( 'name', 'After Update' );
$response = $this->server->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'After Update', $data['name'] );
}

public function test_delete_item() {
$model = new Test_Model();
$model->set_name( 'Delete Me' );
$model->save();
$id = $model->get_id();

$request = new WP_REST_Request( 'DELETE', '/foundry-test/v1/items/' . $id );
$response = $this->server->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertTrue( $data['deleted'] );
$this->assertEquals( 'Delete Me', $data['previous']['name'] );

// Verify actually deleted.
$this->assertNull( Test_Model::get( $id ) );
}

public function test_create_item_rejects_existing_id() {
$request = new WP_REST_Request( 'POST', '/foundry-test/v1/items' );
$request->set_param( 'id', 999 );
$request->set_param( 'name', 'Should Fail' );

$response = $this->server->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
}

public function test_response_includes_self_link() {
$model = new Test_Model();
$model->set_name( 'Link Test' );
$model->save();
$id = $model->get_id();

$request = new WP_REST_Request( 'GET', '/foundry-test/v1/items/' . $id );
$response = $this->server->dispatch( $request );

$links = $response->get_links();
$this->assertArrayHasKey( 'self', $links );
}
}
19 changes: 19 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$_tests_dir = dirname( __DIR__ ) . '/vendor/wp-phpunit/wp-phpunit';

// Point to our test config.
putenv( 'WP_PHPUNIT__TESTS_CONFIG=' . __DIR__ . '/wp-tests-config.php' );

// Load Composer autoloader.
require_once dirname( __DIR__ ) . '/vendor/autoload.php';

// Load the WP testing framework.
require_once $_tests_dir . '/includes/bootstrap.php';

// Load test helpers.
require_once __DIR__ . '/helpers/class-test-model.php';
require_once __DIR__ . '/helpers/class-test-child-model.php';
require_once __DIR__ . '/helpers/class-test-parent-model.php';
require_once __DIR__ . '/helpers/class-test-importer.php';
require_once __DIR__ . '/helpers/class-test-controller.php';
Loading