forked from Solo-Web-Works/Projects-Portfolio
67 lines
2.6 KiB
PHP
67 lines
2.6 KiB
PHP
<?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() );
|
|
}
|
|
}
|