Skip to content

Commit 08bf030

Browse files
authored
Add unit testing factory for Co Authors Plus and Byline Manager (#638)
* Failing test * Switch to class created with eval * Remove unused classes * CHANGELOG typo * Ignore unset.possiblyHookedProperty * Install CAP and byline manager * WIP * Leaving for today * Wrapping up Co authors plus factory * Test fix * CHANGELOG * CHANGELOG and indenting * Fix changelog indenting * Fix changelog indenting * Remove todo * Add method for creating a post with cap authors * Stubbing out byline manager factory * Add byline manager factory * CHANGELOG * Forward directly and allow deprecations * Allow notice * Adding convertNoticesToExceptions * Fix notice with CAP * Split out unit testing for phpunit 9 to have a proper config * Remove unset.possiblyHookedProperty * Simply * Fix commands * Ignore error * Ensure user URL is not over 100 * Fix step name
1 parent e90a1bf commit 08bf030

22 files changed

+844
-20
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/phpstan-baseline.neon export-ignore
1010
/phpstan.neon export-ignore
1111
/phpunit.xml export-ignore
12+
/phpunit-9.xml export-ignore
1213
/tests export-ignore
1314

1415
#

.github/workflows/tests.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
if: github.event.pull_request.draft == false
1818
timeout-minutes: 10
1919
runs-on: ubuntu-latest
20-
name: "Lint Tests"
20+
name: "Composer and Monorepo Validate"
2121
steps:
2222
- uses: actions/checkout@v4
2323

@@ -30,7 +30,7 @@ jobs:
3030
php-version: '8.4'
3131
skip-services: 'true'
3232
skip-wordpress-install: 'true'
33-
test-command: 'composer monorepo-validate'
33+
test-command: 'composer validate --strict && composer monorepo-validate'
3434
lint-tests:
3535
if: github.event.pull_request.draft == false
3636
timeout-minutes: 10
@@ -77,7 +77,8 @@ jobs:
7777
install-command: composer update --${{ matrix.dependencies }} --prefer-dist --no-interaction --no-progress
7878
php-version: '${{ matrix.php }}'
7979
skip-wordpress-install: 'true'
80-
test-command: 'composer phpunit'
80+
# Only run PHPUnit for the lowest dependencies.
81+
test-command: ${{ matrix.dependencies == 'prefer-lowest' && 'composer phpunit-9' || 'composer phpunit' }}
8182
wordpress-host: 'false'
8283
wordpress-multisite: '${{ matrix.multisite }}'
8384
wordpress-version: '${{ matrix.wordpress }}'

CHANGELOG.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added a unit testing factory for Byline Manager profiles and Co-Authors-Plus guest authors.
13+
14+
### Changed
15+
16+
- Passing a `\Stringable` as a model attribute will now be cast to a string
17+
before being set on the model.
1218
- Added `with_debug()` and `with_multisite()` methods to the installation manager.
1319

1420
### Fixed
@@ -96,9 +102,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
96102
by setting the `MANTLE_EXPERIMENTAL_TESTING_USE_HOME_URL_HOST` environment
97103
variable.
98104

99-
Once enabled, the home URL will be used as the base URL for testing rather
100-
the hard-coded `WP_TESTS_DOMAIN`. It will also infer the HTTPS status from
101-
the home URL.
105+
Once enabled, the home URL will be used as the base URL for testing rather
106+
the hard-coded `WP_TESTS_DOMAIN`. It will also infer the HTTPS status from
107+
the home URL.
102108
- Added `with_option()`/`with_home_url()`/`with_site_url()` methods to the installation manager.
103109
- Add a `without_local_object_cache()` method to prevent the `object-cache.php` drop-in from being loaded locally.
104110
- Added a better `dump()` method to the response object when testing HTTP
@@ -111,7 +117,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
111117
- Upgraded to Symfony 7.0 packages.
112118
- Disable `spatie/once`'s cache if found during unit testing.
113119
- Ensure that the `QUERY_STRING` server variable is set when testing HTTP
114-
requests.
120+
requests.
115121

116122
### Fixed
117123

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,14 @@
148148
"phpcs": "phpcs --standard=./phpcs.xml .",
149149
"phpstan": "phpstan --memory-limit=1536M",
150150
"phpunit:multisite": "WP_MULTISITE=1 phpunit",
151+
"phpunit-9": "phpunit --configuration=phpunit-9.xml",
151152
"phpunit": "phpunit",
152153
"rector:fix": "rector process",
153154
"rector": "rector process --dry-run",
154155
"release:patch": "monorepo-builder release patch --ansi",
155156
"release": "monorepo-builder release --ansi",
156157
"test": [
157-
"@phpcs",
158-
"@phpstan",
159-
"@rector",
158+
"@lint",
160159
"@phpunit"
161160
]
162161
},

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ parameters:
1818
identifier: require.fileNotFound
1919
-
2020
identifier: requireOnce.fileNotFound
21+
# Will be fixed in PHPStan soon.
2122
-
2223
identifier: function.alreadyNarrowedType
2324
paths:

phpunit-9.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="tests/bootstrap.php"
5+
backupGlobals="false"
6+
colors="true"
7+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
convertDeprecationsToExceptions="true"
12+
>
13+
<testsuites>
14+
<testsuite name="mantle-framework">
15+
<directory suffix="Test.php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

phpunit.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
bootstrap="tests/bootstrap.php"
55
backupGlobals="false"
66
colors="true"
7-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
7+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
88
displayDetailsOnTestsThatTriggerDeprecations="true"
99
displayDetailsOnTestsThatTriggerWarnings="true"
1010
failOnNotice="true"
11+
failOnPhpunitDeprecation="true"
12+
failOnWarning="true"
1113
failOnDeprecation="true"
1214
>
1315
<testsuites>

src/mantle/database/factory/class-factory-container.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ class Factory_Container {
3838
*/
3939
public Blog_Factory $blog;
4040

41+
/**
42+
* Byline Manager Factory
43+
*
44+
* @var Plugins\Byline_Manager_Factory<\Mantle\Database\Model\Post, \Byline_Manager\Models\Profile, \Byline_Manager\Models\Profile>
45+
*/
46+
public Plugins\Byline_Manager_Factory $byline_manager_profile;
47+
48+
/**
49+
* Co Author Guest Author Factory
50+
*
51+
* @var Plugins\Co_Authors_Plus_Factory<\Mantle\Database\Model\Post, \stdClass, \stdClass>
52+
*/
53+
public Plugins\Co_Authors_Plus_Factory $cap_guest_author;
54+
4155
/**
4256
* Category Factory
4357
*
@@ -115,6 +129,10 @@ public function __construct( Container $container ) {
115129
$this->blog = $container->make( Blog_Factory::class );
116130
$this->network = $container->make( Network_Factory::class );
117131
}
132+
133+
// Plugin-specific factories.
134+
$this->byline_manager_profile = $container->make( Plugins\Byline_Manager_Factory::class );
135+
$this->cap_guest_author = $container->make( Plugins\Co_Authors_Plus_Factory::class );
118136
}
119137

120138
/**

src/mantle/database/factory/class-factory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ public function as_objects() {
129129
* Create a new factory instance with middleware.
130130
*
131131
* @param callable(array $args, \Closure $next): mixed $middleware Middleware to run the factory through.
132-
* @return static
133132
*/
134-
public function with_middleware( callable $middleware ) {
133+
public function with_middleware( callable $middleware ): static {
135134
return tap(
136135
clone $this,
137136
fn ( Factory $factory ) => $factory->middleware = $this->middleware->merge( $middleware ),
@@ -189,9 +188,8 @@ public function get_model(): string {
189188
* middleware but supports returning an array of attributes vs a closure.
190189
*
191190
* @param (callable(array<string, mixed>): array<string, mixed>|array<string, mixed>) $state The state transformation.
192-
* @return static
193191
*/
194-
public function state( array|callable $state ) {
192+
public function state( array|callable $state ): static {
195193
return $this->with_middleware(
196194
function ( array $args, Closure $next ) use ( $state ) {
197195
$args = array_merge(

src/mantle/database/factory/class-post-factory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* @extends Factory<TModel, TObject, TReturnValue>
2929
*/
3030
class Post_Factory extends Factory {
31+
use Concerns\With_Byline_Manager_Profiles;
32+
use Concerns\With_Guest_Authors;
3133
use Concerns\With_Meta;
3234

3335
/**
@@ -92,7 +94,11 @@ public function with_terms( ...$terms ): static {
9294
$terms = collect( $terms )->all();
9395

9496
return $this->with_middleware(
95-
fn ( array $args, Closure $next ) => $next( $args )->set_terms( $terms, append: $this->append_terms, create: $this->create_terms ),
97+
fn ( array $args, Closure $next ) => $next( $args )->set_terms(
98+
terms: $terms,
99+
append: $this->append_terms,
100+
create: $this->create_terms,
101+
),
96102
);
97103
}
98104

0 commit comments

Comments
 (0)