Skip to content

Commit 61362ef

Browse files
authored
Merge pull request #26 from WordPress/feature/unit-tests
Adds enum unit tests
2 parents 3402a88 + d8c2267 commit 61362ef

17 files changed

+1110
-0
lines changed

.github/workflows/php-test.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- trunk
7+
- 'feature/**'
8+
- 'release/**'
9+
# Only run if PHP-related files changed.
10+
paths:
11+
- '.github/workflows/php-test.yml'
12+
- '**.php'
13+
- 'phpunit.xml.dist'
14+
- 'composer.json'
15+
- 'composer.lock'
16+
pull_request:
17+
# Only run if PHP-related files changed.
18+
paths:
19+
- '.github/workflows/php-test.yml'
20+
- '**.php'
21+
- 'phpunit.xml.dist'
22+
- 'composer.json'
23+
- 'composer.lock'
24+
types:
25+
- opened
26+
- reopened
27+
- synchronize
28+
29+
jobs:
30+
test:
31+
runs-on: ubuntu-latest
32+
strategy:
33+
matrix:
34+
php-version: ['7.4', '8.0', '8.4']
35+
36+
steps:
37+
- uses: actions/checkout@v4
38+
39+
- name: Setup PHP
40+
uses: shivammathur/setup-php@v2
41+
with:
42+
php-version: ${{ matrix.php-version }}
43+
coverage: xdebug
44+
45+
- name: Validate composer.json and composer.lock
46+
run: composer validate --strict
47+
48+
- name: Cache Composer packages
49+
id: composer-cache
50+
uses: actions/cache@v3
51+
with:
52+
path: vendor
53+
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
54+
restore-keys: |
55+
${{ runner.os }}-php-${{ matrix.php-version }}-
56+
57+
- name: Install dependencies
58+
run: composer install --prefer-dist --no-progress
59+
60+
- name: Run unit tests
61+
run: composer phpunit

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ vendor/
2121
CLAUDE.md
2222
.cursor/
2323
GEMINI.md
24+
25+
############
26+
## PHPUnit
27+
############
28+
29+
.phpunit.cache/

phpunit.xml.dist

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
executionOrder="depends,defects"
7+
forceCoversAnnotation="true"
8+
beStrictAboutCoversAnnotation="true"
9+
beStrictAboutOutputDuringTests="true"
10+
beStrictAboutTodoAnnotatedTests="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
verbose="true"
14+
colors="true">
15+
<testsuites>
16+
<testsuite name="unit">
17+
<directory suffix="Test.php">tests/unit</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
22+
processUncoveredFiles="true">
23+
<include>
24+
<directory suffix=".php">src</directory>
25+
</include>
26+
</coverage>
27+
</phpunit>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\mocks\Enums;
6+
7+
use WordPress\AiClient\Common\AbstractEnum;
8+
9+
/**
10+
* Invalid test enum with lowercase constant name.
11+
*/
12+
class InvalidNameTestEnum extends AbstractEnum
13+
{
14+
public const VALID_NAME = 'valid';
15+
// phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
16+
public const invalid_name = 'invalid'; // This should cause an exception
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\mocks\Enums;
6+
7+
use WordPress\AiClient\Common\AbstractEnum;
8+
9+
/**
10+
* Invalid test enum with float value.
11+
*/
12+
class InvalidTypeTestEnum extends AbstractEnum
13+
{
14+
public const VALID_VALUE = 'valid';
15+
public const INT_VALUE = 42; // This should cause an exception
16+
}

tests/mocks/Enums/ValidTestEnum.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\mocks\Enums;
6+
7+
use WordPress\AiClient\Common\AbstractEnum;
8+
9+
/**
10+
* Valid test enum for testing AbstractEnum functionality.
11+
*
12+
* @method static self firstName() Creates an instance for FIRST_NAME.
13+
* @method static self lastName() Creates an instance for LAST_NAME.
14+
* @method bool isFirstName() Checks if the value is FIRST_NAME.
15+
* @method bool isLastName() Checks if the value is LAST_NAME.
16+
*/
17+
class ValidTestEnum extends AbstractEnum
18+
{
19+
public const FIRST_NAME = 'first';
20+
public const LAST_NAME = 'last';
21+
}

0 commit comments

Comments
 (0)