-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30835c1
commit 05c2eee
Showing
5 changed files
with
332 additions
and
49 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
<?php | |||
|
|||
namespace App\Helpers; | |||
|
|||
class HerokuS3 | |||
{ | |||
public static function get_aws_root_from_env() { | |||
$value = basename(Uri::get_uri_path(env('CLOUDCUBE_URL'))); | |||
return empty($value) ? null : $value; | |||
} | |||
|
|||
public static function get_aws_url_from_env() { | |||
$path = Uri::get_uri_path(env('CLOUDCUBE_URL')); | |||
$value = Uri::get_uri_without_path(env('CLOUDCUBE_URL'), $path); | |||
return empty($value) ? null : $value; | |||
} | |||
|
|||
public static function get_default_aws_region() { | |||
$domainParts = Uri::get_uri_domain_parts( | |||
Uri::get_uri_host(env('CLOUDCUBE_URL')) | |||
); | |||
if( | |||
(count($domainParts) === 4) && | |||
(strpos($domainParts[0], 'cloud-cube') !== false) | |||
) { | |||
return self::get_default_cloudcube_region($domainParts[0]); | |||
} | |||
return null; | |||
} | |||
|
|||
public static function get_aws_bucket_from_env() { | |||
$domainParts = Uri::get_uri_domain_parts( | |||
Uri::get_uri_host(env('CLOUDCUBE_URL')) | |||
); | |||
return count($domainParts) >= 1 ? $domainParts[0] : null; | |||
} | |||
|
|||
public static function get_default_cloudcube_region($region) { | |||
switch($region) { | |||
case 'cloud-cube': | |||
return 'us-east-1'; | |||
case 'cloud-cube-eu': | |||
return 'eu-west-1'; | |||
case 'cloud-cube-jp': | |||
return 'ap-northeast-1'; | |||
} | |||
return 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
<?php | |||
|
|||
namespace App\Helpers; | |||
|
|||
class Uri { | |||
public static function get_uri_path($url) { | |||
$parsed = parse_url($url); | |||
return array_key_exists('path', $parsed) ? $parsed['path'] : ''; | |||
} | |||
|
|||
public static function get_uri_without_path($url, $path=null) { | |||
$url = $url ?? ''; | |||
$path = $path ?? ''; | |||
return substr($url, 0, strlen($url) - strlen($path)); | |||
} | |||
|
|||
public static function get_uri_host($url) { | |||
$parsed = parse_url($url); | |||
return array_key_exists('host', $parsed) ? $parsed['host'] : ''; | |||
} | |||
|
|||
public static function get_uri_domain_parts($hoststr) { | |||
return array_filter( | |||
explode('.', $hoststr), | |||
function($item) { return !empty($item); } | |||
); | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,161 @@ | |||
<?php | |||
|
|||
namespace Tests\Unit; | |||
|
|||
use Tests\TestCase; | |||
use App\Helpers\HerokuS3; | |||
|
|||
class HerokuS3HelperTest extends TestCase | |||
{ | |||
// Preserve Environment | |||
protected $_oldenv; | |||
|
|||
protected function setUp() : void | |||
{ | |||
$this->_oldenv = $_ENV; | |||
} | |||
|
|||
protected function tearDown() : void | |||
{ | |||
$_ENV = $this->_oldenv; | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider systemTestProvider | |||
*/ | |||
public function get_aws_bucket_from_env($system_env, $expectations) { | |||
$this->restoreEnv($system_env); | |||
|
|||
$this->assertEquals( | |||
$expectations['bucket'], | |||
HerokuS3::get_aws_bucket_from_env() | |||
); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider systemTestProvider | |||
*/ | |||
public function get_aws_root_from_env($system_env, $expectations) { | |||
$this->restoreEnv($system_env); | |||
|
|||
$this->assertEquals( | |||
$expectations['root'], | |||
HerokuS3::get_aws_root_from_env() | |||
); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider systemTestProvider | |||
*/ | |||
public function get_aws_url_from_env($system_env, $expectations) { | |||
$this->restoreEnv($system_env); | |||
|
|||
$this->assertEquals( | |||
$expectations['aws_url'], | |||
HerokuS3::get_aws_url_from_env() | |||
); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider systemTestProvider | |||
*/ | |||
public function get_default_aws_region($system_env, $expectations) { | |||
$this->restoreEnv($system_env); | |||
|
|||
$this->assertEquals( | |||
$expectations['aws_region'], | |||
HerokuS3::get_default_aws_region() | |||
); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider cloudcubeTestProvider | |||
*/ | |||
public function get_default_cloudcube_region( | |||
$left_most_subdomain, $expectation | |||
) { | |||
$this->assertEquals( | |||
$expectation, | |||
HerokuS3::get_default_cloudcube_region( | |||
$left_most_subdomain | |||
) | |||
); | |||
} | |||
|
|||
private function restoreEnv($system_env) { | |||
$_ENV = $system_env; | |||
} | |||
|
|||
public static function cloudcubeTestProvider() { | |||
return [ | |||
"Generally (default)" => [ | |||
'Anything You Want', | |||
null | |||
], | |||
"US east 1" => [ | |||
'cloud-cube', | |||
'us-east-1' | |||
], | |||
"EU west 1" => [ | |||
'cloud-cube-eu', | |||
'eu-west-1' | |||
], | |||
"AP NorthEast 1" => [ | |||
'cloud-cube-jp', | |||
'ap-northeast-1' | |||
] | |||
]; | |||
} | |||
|
|||
public static function systemTestProvider() { | |||
return [ | |||
"When CLOUDCUBE_URL is not set" => [ | |||
'ENV' => [], | |||
'expectations'=> [ | |||
'bucket' => null, | |||
'aws_url' => null, | |||
'aws_region' => null, | |||
'root' => null, | |||
] | |||
], | |||
"When CLOUDCUBE_URL is an US one" => [ | |||
'ENV' => [ | |||
'CLOUDCUBE_URL' => 'https://cloud-cube.s3.amazonaws.com/xmfnhr2po8rp' | |||
], | |||
'expectations'=> [ | |||
'bucket' => 'cloud-cube', | |||
'aws_url' => 'https://cloud-cube.s3.amazonaws.com', | |||
'aws_region' => 'us-east-1', | |||
'root' => 'xmfnhr2po8rp' | |||
] | |||
], | |||
"When CLOUDCUBE_URL is an EU one" => [ | |||
'ENV' => [ | |||
'CLOUDCUBE_URL' => 'https://cloud-cube-eu.s3.amazonaws.com/my-apps-root' | |||
], | |||
'expectations'=> [ | |||
'bucket' => 'cloud-cube-eu', | |||
'aws_url' => 'https://cloud-cube-eu.s3.amazonaws.com', | |||
'aws_region' => 'eu-west-1', | |||
'root' => 'my-apps-root', | |||
] | |||
], | |||
"When CLOUDCUBE_URL is an AP one" => [ | |||
'ENV' => [ | |||
'CLOUDCUBE_URL' => 'https://cloud-cube-jp.s3.amazonaws.com/something' | |||
], | |||
'expectations'=> [ | |||
'bucket' => 'cloud-cube-jp', | |||
'aws_url' => 'https://cloud-cube-jp.s3.amazonaws.com', | |||
'aws_region' => 'ap-northeast-1', | |||
'root' => 'something', | |||
] | |||
] | |||
]; | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,92 @@ | |||
<?php | |||
|
|||
namespace Tests\Unit; | |||
|
|||
use Tests\TestCase; | |||
use App\Helpers\Uri; | |||
|
|||
class UriHelperTest extends TestCase | |||
{ | |||
/** | |||
* @test | |||
* @dataProvider uriTestProvider | |||
*/ | |||
public function get_uri_path($uri, $expectations) | |||
{ | |||
$output = Uri::get_uri_path($uri); | |||
$this->assertEquals($expectations['path'], $output); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider uriTestProvider | |||
*/ | |||
public function get_uri_without_path($uri, $expectations) { | |||
$output = Uri::get_uri_without_path( | |||
$uri, Uri::get_uri_path($uri) | |||
); | |||
$this->assertEquals($expectations['uri_sans_path'], $output); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider uriTestProvider | |||
*/ | |||
public function get_uri_host($uri, $expectations) { | |||
$output = Uri::get_uri_host($uri); | |||
$this->assertEquals($expectations['host'], $output); | |||
} | |||
|
|||
/** | |||
* @test | |||
* @dataProvider uriTestProvider | |||
*/ | |||
public function get_uri_domain_parts($uri, $expectations) { | |||
$output = Uri::get_uri_domain_parts( | |||
Uri::get_uri_host($uri) | |||
); | |||
$this->assertEquals($expectations['host_parts'], $output); | |||
$this->assertEquals(count($expectations['host_parts']), count($output)); | |||
} | |||
|
|||
public static function uriTestProvider() { | |||
return [ | |||
"When Uri is Null" => [ | |||
"uri" => null, | |||
"expectations" => [ | |||
"path" => "", | |||
"uri_sans_path" => "", | |||
"host" => "", | |||
"host_parts" => [] | |||
] | |||
], | |||
"When Uri without path" => [ | |||
"uri" => 'https://www.google.com', | |||
"expectations" => [ | |||
"path" => "", | |||
"uri_sans_path" => "https://www.google.com", | |||
"host" => "www.google.com", | |||
"host_parts" => ["www", "google", "com"] | |||
] | |||
], | |||
"When Uri Path is Root" => [ | |||
"uri" => 'https://www.google.com/', | |||
"expectations" => [ | |||
"path" => "/", | |||
"uri_sans_path" => "https://www.google.com", | |||
"host" => "www.google.com", | |||
"host_parts" => ["www", "google", "com"] | |||
] | |||
], | |||
"When given Uri with path" => [ | |||
"uri" => 'https://cloud-cube.s3.amazonaws.com/xmfnhr2po8rp', | |||
"expectations" => [ | |||
"path" => "/xmfnhr2po8rp", | |||
"uri_sans_path" => "https://cloud-cube.s3.amazonaws.com", | |||
"host" => "cloud-cube.s3.amazonaws.com", | |||
"host_parts" => ["cloud-cube", "s3", "amazonaws", "com"] | |||
] | |||
] | |||
]; | |||
} | |||
} |