-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a fake provider that allows for faking the cdn providers default country
- Loading branch information
Showing
11 changed files
with
177 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
// config for Worksome/cdn-headers | ||
return [ | ||
'default-provider' => env('CDN_HEADERS_PROVIDER', 'cloudflare'), | ||
'default-country' => env('CDN_COUNTRY_CODE', null), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
src/CloudFlare/CloudFlareProvider.php → ...oviders/CloudFlare/CloudFlareProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Worksome\CdnHeaders\Providers; | ||
|
||
use Worksome\CdnHeaders\Contracts\CdnHeadersProvider; | ||
|
||
class FakeProvider implements CdnHeadersProvider | ||
{ | ||
public function __construct(private array $config) | ||
{ | ||
} | ||
|
||
public function getCountryCode(): string|null | ||
{ | ||
return $this->config['default-country'] ?? null; | ||
} | ||
|
||
public function hasCountryCode(): bool | ||
{ | ||
return (bool) $this->getCountryCode(); | ||
} | ||
|
||
public function getConnectingIp(): string|null | ||
{ | ||
return null; | ||
} | ||
|
||
public function getTraceId(): string|null | ||
{ | ||
return null; | ||
} | ||
|
||
public function boot(): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Contracts\Container\Container; | ||
use Worksome\CdnHeaders\CdnHeadersManager; | ||
use Worksome\CdnHeaders\ServerHeadersRepository; | ||
|
||
beforeEach(function () { | ||
$this->createMocks = function ($config) { | ||
$this->mockContainer = Mockery::mock(Container::class); | ||
$this->mockConfig = Mockery::mock(Repository::class); | ||
$this->mockContainer->shouldReceive('make')->andReturn($this->mockConfig); | ||
$this->mockContainer->shouldReceive('get')->andReturn(new ServerHeadersRepository()); | ||
$this->mockConfig->shouldReceive('get') | ||
->with('cdn-headers.default-provider') | ||
->andReturn($config['default-provider']); | ||
$this->mockConfig->shouldReceive('get') | ||
->with('cdn-headers') | ||
->andReturn($config); | ||
}; | ||
}); | ||
|
||
it('should return cloudflare as the default provider if no config param is present', function () { | ||
$this->createMocks->__invoke([ | ||
'default-provider' => null, | ||
]); | ||
$manager = new CdnHeadersManager($this->mockContainer); | ||
expect($manager->getDefaultDriver())->toBe('cloudflare'); | ||
}); | ||
|
||
it('should return fake as the provider if specified in config', function () { | ||
$this->createMocks->__invoke([ | ||
'default-provider' => 'fake', | ||
]); | ||
$manager = new CdnHeadersManager($this->mockContainer); | ||
expect($manager->getDefaultDriver())->toBe('fake'); | ||
}); | ||
|
||
it('should return cloudflare as the provider if specified in config', function () { | ||
$this->createMocks->__invoke([ | ||
'default-provider' => 'cloudflare', | ||
]); | ||
$manager = new CdnHeadersManager($this->mockContainer); | ||
expect($manager->getDefaultDriver())->toBe('cloudflare'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,60 @@ | ||
<?php | ||
|
||
use Worksome\CdnHeaders\CloudFlare\CloudFlareProvider; | ||
use Worksome\CdnHeaders\Providers\CloudFlare\CloudFlareProvider; | ||
use Worksome\CdnHeaders\ServerHeadersRepository; | ||
|
||
beforeEach(fn () => $this->mock = Mockery::mock(ServerHeadersRepository::class)); | ||
beforeEach(function () { | ||
$this->country = 'GB'; | ||
$this->mock = Mockery::mock(ServerHeadersRepository::class); | ||
$this->config = [ | ||
'default-country' => $this->country, | ||
]; | ||
}); | ||
|
||
it('should get null as a default country code', function () { | ||
it('should get null as a default country code if no config is present', function () { | ||
$this->mock->shouldReceive('get')->andReturn(null); | ||
$provider = new CloudFlareProvider($this->mock); | ||
$provider = new CloudFlareProvider([], $this->mock); | ||
|
||
expect($provider->getCountryCode())->toBe(null); | ||
expect($provider->hasCountryCode())->toBeFalse(); | ||
}); | ||
|
||
it('should get config default country code if present', function () { | ||
$this->mock->shouldReceive('get')->andReturn(null); | ||
$provider = new CloudFlareProvider($this->config, $this->mock); | ||
|
||
expect($provider->getCountryCode())->toBe($this->country); | ||
expect($provider->hasCountryCode())->toBeTrue(); | ||
}); | ||
|
||
it('should return a correct country code if server header present', function () { | ||
$this->mock->shouldReceive('get')->andReturn('US'); | ||
$provider = new CloudFlareProvider($this->mock); | ||
$provider = new CloudFlareProvider($this->config, $this->mock); | ||
|
||
expect($provider->getCountryCode())->toBe('US'); | ||
expect($provider->hasCountryCode())->toBeTrue(); | ||
}); | ||
|
||
it('should get null as a default connecting ip', function () { | ||
$this->mock->shouldReceive('get')->andReturn(null); | ||
$provider = new CloudFlareProvider($this->mock); | ||
$provider = new CloudFlareProvider($this->config, $this->mock); | ||
expect($provider->getConnectingIp())->toBe(null); | ||
}); | ||
|
||
it('should return an ip', function () { | ||
$this->mock->shouldReceive('get')->andReturn('127.0.0.1'); | ||
$provider = new CloudFlareProvider($this->mock); | ||
$provider = new CloudFlareProvider($this->config, $this->mock); | ||
expect($provider->getConnectingIp())->toBe('127.0.0.1'); | ||
}); | ||
|
||
it('should get null as a default ray', function () { | ||
$this->mock->shouldReceive('get')->andReturn(null); | ||
$provider = new CloudFlareProvider($this->mock); | ||
$provider = new CloudFlareProvider($this->config, $this->mock); | ||
expect($provider->getTraceId())->toBe(null); | ||
}); | ||
|
||
it('should return ray', function () { | ||
$this->mock->shouldReceive('get')->andReturn('CF12345679'); | ||
$provider = new CloudFlareProvider($this->mock); | ||
$provider = new CloudFlareProvider($this->config, $this->mock); | ||
expect($provider->getTraceId())->toBe('CF12345679'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Worksome\CdnHeaders\Providers\FakeProvider; | ||
|
||
beforeEach(function () { | ||
$this->country = 'GB'; | ||
$this->config = [ | ||
'default-country' => $this->country, | ||
]; | ||
}); | ||
|
||
it('should get null as a default country code if no config is present', function () { | ||
$provider = new FakeProvider([]); | ||
|
||
expect($provider->getCountryCode())->toBe(null); | ||
expect($provider->hasCountryCode())->toBeFalse(); | ||
}); | ||
|
||
it('should get config default country code if present', function () { | ||
$provider = new FakeProvider($this->config); | ||
|
||
expect($provider->getCountryCode())->toBe($this->country); | ||
expect($provider->hasCountryCode())->toBeTrue(); | ||
}); | ||
|
||
it('should get null as a default connecting ip', function () { | ||
$provider = new FakeProvider($this->config); | ||
expect($provider->getConnectingIp())->toBe(null); | ||
}); | ||
|
||
it('should get null as a default ray', function () { | ||
$provider = new FakeProvider($this->config); | ||
expect($provider->getTraceId())->toBe(null); | ||
}); |