Skip to content
Permalink
Browse files

Re-factor to helpers

  • Loading branch information
Lewiscowles1986 committed Feb 1, 2020
1 parent 30835c1 commit 05c2eeedc3b77b9deb7b8965a816340044613302
Showing with 332 additions and 49 deletions.
  1. +49 −0 app/Helpers/HerokuS3.php
  2. +28 −0 app/Helpers/Uri.php
  3. +2 −49 config/filesystems.php
  4. +161 −0 tests/Unit/HerokuS3HelperTest.php
  5. +92 −0 tests/Unit/UriHelperTest.php
@@ -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;
}
}
@@ -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); }
);
}
}
@@ -1,5 +1,7 @@
<?php

use App\Helpers\HerokuS3;

return [

/*
@@ -67,52 +69,3 @@
],

];

function get_url_path($url) {
$parsed = parse_url($url);
return array_key_exists('path', $parsed) ? $parsed['path'] : '';
}

function get_url_without_path($url, $path=null) {
$path = $path ?? '';
return substr($url, 0, strlen($url) - strlen($path));
}

function get_url_host($url) {
$parsed = parse_url($url);
return array_key_exists('host', $parsed) ? $parsed['host'] : '';
}

function get_url_domain_parts($url) {
return array_filter(
explode('.', get_url_host($url)),
function($item) { return !empty($item); }
);
}

function get_aws_bucket_from_env() {
$value = basename(get_url_path(env('AWS_URL')));
return empty($value) ? null : $value;
}
function get_aws_url_from_env() {
$path = get_url_path(env('AWS_URL'));
$value = get_url_without_path(env('AWS_URL'), $path);
return empty($value) ? null : $value;
}

function get_default_aws_region() {
$domainParts = get_url_domain_parts(env('AWS_URL'));
if(count($domainParts) == '4') {
return get_default_cloudcube_region($domainParts[0]);
}
return 'us-east-1';
}
function get_default_cloudcube_region($region) {
switch($region) {
case 'cloud-cube-eu':
return 'eu-west-1';
case 'cloud-cube-jp':
return 'ap-northeast-1';
}
return 'us-east-1';
}
@@ -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',
]
]
];
}
}
@@ -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"]
]
]
];
}
}

0 comments on commit 05c2eee

Please sign in to comment.
You can’t perform that action at this time.