Skip to content

Instantly share code, notes, and snippets.

@alloyking
Last active May 14, 2022 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alloyking/3ef60a6b5d3a27a1ca8476c5a1018224 to your computer and use it in GitHub Desktop.
Save alloyking/3ef60a6b5d3a27a1ca8476c5a1018224 to your computer and use it in GitHub Desktop.
Test
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class testCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'A test command';
const LANDING = "index.html";
protected $myURL = '';
protected $requestedURL = "";
protected $passedTests;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tests = [
["myURL" => "/section/index.html", "requestedURL" => "/section/page.html", "expected" => true],
["myURL" => "/section/index.html", "requestedURL" => "/section/subsection/index.html", "expected" => true],
["myURL" => "/section/index.html", "requestedURL" => "/section/subsection/page.html", "expected" => true],
["myURL" => "/section/page.html", "requestedURL" => "/section/other-page.html", "expected" => false],
["myURL" => "/section/subsection/index.html", "requestedURL" => "/section/other/index.html", "expected" => false],
//extra
["myURL" => "", "requestedURL" => "/section/page.html", "expected" => false],
["myURL" => "section/index.html", "requestedURL" => "", "expected" => false],
["myURL" => "", "requestedURL" => "", "expected" => false],
["myURL" => "/section/subsection/index.html", "requestedURL" => "/section/subsection/index.html", "expected" => true],
["myURL" => "/section/subsection/index.html", "requestedURL" => "/section/yo.png", "expected" => false],
["myURL" => "/section/subsection/index.html", "requestedURL" => "/section", "expected" => false],
["myURL" => "/section/index.html", "requestedURL" => "/section/subsection", "expected" => true],
];
foreach($tests as $test){
$this->myURL = $test['myURL'];
$this->requestedURL = $test['requestedURL'];
$this->runTest($test);
}
$this->info("passed " . $this->passedTests . " out of ". count($tests));
}
private function runTest($test){
if(!$this->myURL || !$this->requestedURL){
$this->buildTestResults($test, false);
return false;
}
//return true if the requested page is exactly the same as myURL regardless if it's a landing it would seem
if($this->myURL === $this->requestedURL){
$this->info(json_encode($test));
$this->buildTestResults($test, true);
return;
}
//is our myURL a landing page today? Returns true if we have index.html
$isLanding = $this->isLanding();
//strip off any file names and compare the url segments to see if we are in the same folder structure as myURL.
//This should return true if we are in the same root, section, or child section
$sameSection = strpos($this->getURLSections($this->requestedURL), $this->getURLSections($this->myURL));
if($sameSection !== false){
//since we are in the same section we also need to make sure our myURL is indeed a landing page.
//If we are in the same section/child AND we are a landing then return true
if($isLanding){
$this->info(json_encode($test));
$this->buildTestResults($test, true);
return;
}
}
$this->buildTestResults($test, false);
}
private function buildTestResults($test, $result){
if($test['expected'] == $result){
$this->passedTests++;
}
}
/**
* Return the folder structure of a path.
*
* @return mixed
*/
private function getURLSections($url){
//1. given a url of /section/butter/index.html we would return /section/butter
//2. given a url of /section/butter/stick we would return /section/butter/stick
// The goal here is to find the parent url sections of a path to a file w/o the filename
//parse the URL and remove the domain if it happened to have one
$path = parse_url($url, PHP_URL_PATH);
//Does this path end in .html or something else that would indicate we are loading a file
$hasExtension = pathinfo($path, PATHINFO_EXTENSION);
if($hasExtension){
//grab the portion of the URL that does not contain an extension
$path = dirname($path);
}
return $path;
}
private function isLanding(){
//all pages that end with index.html are considered a landing page
//if we are given a myURL that ends with index.html this will return true
$identifyer = self::LANDING;
//given no url we are just false automatically
if(!$this->myURL){
return false;
}
//find the end of the url (which might be index.html)
$last = basename($this->myURL);
if($last !== $identifyer){
return false;
}
return true;
}
}
/*
➜ php artisan command:test
{"myURL":"\/section\/index.html","requestedURL":"\/section\/page.html","expected":true}
{"myURL":"\/section\/index.html","requestedURL":"\/section\/subsection\/index.html","expected":true}
{"myURL":"\/section\/index.html","requestedURL":"\/section\/subsection\/page.html","expected":true}
{"myURL":"\/section\/subsection\/index.html","requestedURL":"\/section\/subsection\/index.html","expected":true}
{"myURL":"\/section\/index.html","requestedURL":"\/section\/subsection","expected":true}
passed 12 out of 12
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment