Add provider factory with meta + option wiring
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider factory: selects the right adapter for a project post.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @param int $post_id
|
||||
* @return Repository_Provider
|
||||
*/
|
||||
function projects_portfolio_get_provider( int $post_id ): Repository_Provider {
|
||||
$provider_meta = (string) get_post_meta( $post_id, '_projects_portfolio_provider', true );
|
||||
$provider_id = '' !== $provider_meta ? $provider_meta : 'github';
|
||||
|
||||
$repo_url = (string) get_post_meta( $post_id, '_projects_portfolio_repo_url', true );
|
||||
if ( '' === $repo_url ) {
|
||||
// Lazy fallback to legacy key.
|
||||
$repo_url = (string) get_post_meta( $post_id, '_projects_portfolio_github_url', true );
|
||||
}
|
||||
|
||||
if ( 'gitea' === $provider_id ) {
|
||||
$base_url = (string) get_post_meta( $post_id, '_projects_portfolio_gitea_base_url', true );
|
||||
if ( '' === $base_url ) {
|
||||
$base_url = (string) get_option( 'projects_portfolio_default_gitea_base_url', 'https://codeberg.org' );
|
||||
}
|
||||
$token = (string) get_option( 'projects_portfolio_gitea_api_token', '' );
|
||||
return new Gitea_Provider( $repo_url, $base_url, $token );
|
||||
}
|
||||
|
||||
$token = (string) get_option( 'projects_portfolio_github_api_token', '' );
|
||||
return new GitHub_Provider( $repo_url, $token );
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../includes/providers/interface-repository-provider.php';
|
||||
require_once __DIR__ . '/../includes/providers/class-github-provider.php';
|
||||
require_once __DIR__ . '/../includes/providers/class-gitea-provider.php';
|
||||
require_once __DIR__ . '/../includes/providers/class-provider-factory.php';
|
||||
|
||||
/**
|
||||
* @covers \projects_portfolio_get_provider
|
||||
*/
|
||||
class Provider_Factory_Test extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
\Brain\Monkey\setUp();
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
\Brain\Monkey\tearDown();
|
||||
}
|
||||
|
||||
private function stub_meta( array $provider_meta, array $options = [] ): void {
|
||||
\Brain\Monkey\Functions\stubs( [
|
||||
'get_post_meta' => function ( $post_id, $key, $single = false ) use ( $provider_meta ) {
|
||||
return $provider_meta[ $key ] ?? '';
|
||||
},
|
||||
'get_option' => function ( $key, $default = '' ) use ( $options ) {
|
||||
return $options[ $key ] ?? $default;
|
||||
},
|
||||
] );
|
||||
}
|
||||
|
||||
public function test_defaults_to_github_when_provider_meta_missing(): void {
|
||||
$this->stub_meta( [
|
||||
'_projects_portfolio_github_url' => 'https://github.com/owner/repo',
|
||||
] );
|
||||
|
||||
$provider = projects_portfolio_get_provider( 42 );
|
||||
$this->assertSame( 'github', $provider->get_id() );
|
||||
$this->assertSame( 'https://github.com/owner/repo', $provider->get_repo_browse_url() );
|
||||
}
|
||||
|
||||
public function test_uses_repo_url_when_provider_explicit(): void {
|
||||
$this->stub_meta( [
|
||||
'_projects_portfolio_provider' => 'gitea',
|
||||
'_projects_portfolio_repo_url' => 'https://codeberg.org/owner/repo',
|
||||
], [
|
||||
'projects_portfolio_default_gitea_base_url' => 'https://codeberg.org',
|
||||
'projects_portfolio_gitea_api_token' => 'gitea-token',
|
||||
] );
|
||||
|
||||
$provider = projects_portfolio_get_provider( 7 );
|
||||
$this->assertSame( 'gitea', $provider->get_id() );
|
||||
}
|
||||
|
||||
public function test_per_project_gitea_base_overrides_global(): void {
|
||||
$this->stub_meta( [
|
||||
'_projects_portfolio_provider' => 'gitea',
|
||||
'_projects_portfolio_repo_url' => 'https://git.example.org/owner/repo',
|
||||
'_projects_portfolio_gitea_base_url' => 'https://git.example.org',
|
||||
], [
|
||||
'projects_portfolio_default_gitea_base_url' => 'https://codeberg.org',
|
||||
] );
|
||||
|
||||
$provider = projects_portfolio_get_provider( 7 );
|
||||
$this->assertSame( 'https://git.example.org/owner/repo', $provider->get_repo_browse_url() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user