Skip to content

Instantly share code, notes, and snippets.

@ozh
Created March 11, 2022 18:39
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 ozh/787c4e7233a935252a0cd7f978ba34e7 to your computer and use it in GitHub Desktop.
Save ozh/787c4e7233a935252a0cd7f978ba34e7 to your computer and use it in GitHub Desktop.
Future unit tests for `yourls_is_shorturl()`

As of writing and with YOURLS 1.8.3-dev, expectedly works with http(s)://sho.rt/ozh but fail with http(s)://www.sho.rt/ozh (and logically if YOURLS_SITE has www. (eg http://www.sho.rt) the failures and success get inverted)

$ phpunit --filter ShortURL_Is_Shorturl_Tests
YOURLS installed, starting PHPUnit

PHPUnit 8.5.14 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.0.1 with Xdebug 3.0.2
Configuration: D:\home\planetozh\ozh.in\phpunit.xml

..FF                                                                4 / 4 (100%)

Time: 2.26 seconds, Memory: 24.00 MB

There were 2 failures:

1) ShortURL_Is_Shorturl_Tests::test_is_shorturl_https_www
Failed asserting that false is true.

[...]
D:\home\planetozh\ozh.in\tests\tests\shorturl\is_shorturl.php:78
[...]

2) ShortURL_Is_Shorturl_Tests::test_is_shorturl_http_www
Failed asserting that false is true.

[...]
D:\home\planetozh\ozh.in\tests\tests\shorturl\is_shorturl.php:84
[...]

FAILURES!
Tests: 4, Assertions: 4, Failures: 2.
<?php
/**
* Short URLs : yourls_is_shorturl() extensive tests
*
* @group shorturls
*/
class ShortURL_Is_Shorturl_Tests extends PHPUnit\Framework\TestCase
{
/**
* Notes for my future self
*
* The purpose of these tests is to make sure yourls_is_shorturl() works correctly and consistently :
* http://sho.rt/ozh,
* https://shor.rt/ozh,
* http://www.sho.rt/ozh
* https://www.sho.rt/ozh
* should all be considered short URLs (since short URL 'ozh' was created upon install)
*
* First off, we're adding a filter on 'get_yourls_site' to modify YOURLS_SITE value to 'http://sho.rt'. Why?
* Because we'll be adding or removing 'www.' from YOURLS_SITE and we need to make sure its independent from the
* actual value used by a test machine (if YOURLS_SITE is 127.0.0.1, it makes no sense to prefix with 'www.')
*
* Then we're using 4 very similar tests instead of one test with a data provider. Why?
* Because data provider are executed before both setUpBeforeClass and setUp methods, and we need to have the
* filter activated in order to manipulate YOURLS_SITE
* See https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html
*
*/
protected static $base,
$site_no_www,
$site_www;
public static function setUpBeforeClass(): void
{
yourls_add_filter('get_yourls_site', function () {
return 'https://sho.rt';
});
// Now that the filter is set, we can modify YOURLS_SITE
self::set_yourls_site();
// We know that 'ozh' is a valid short URL (created upon install)
// We need refresh its info, otherwise it'll be existent but empty
yourls_get_keyword_infos('ozh', false);
}
public static function set_yourls_site() {
self::$base = yourls_get_domain(yourls_get_yourls_site() );
self::$site_no_www = str_replace('www.', '', self::$base);
self::$site_www = 'www.' . self::$site_no_www;
}
public static function tearDownAfterClass(): void
{
yourls_remove_all_filters('get_yourls_site');
}
// yourls_is_shorturl (also see crud.php/test_is_shorturl)
// Make variations with http/https/www/no-www with filtered YOURLS_SITE and test them.
function test_is_shorturl_https_no_www()
{
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'https', 'host' => self::$site_no_www]) . '/ozh';
$this->assertTrue(yourls_is_shorturl($shorturl));
}
function test_is_shorturl_http_no_www()
{
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'http', 'host' => self::$site_no_www]) . '/ozh';
$this->assertTrue(yourls_is_shorturl($shorturl));
}
function test_is_shorturl_https_www()
{
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'https', 'host' => self::$site_www]) . '/ozh';
$this->assertTrue(yourls_is_shorturl($shorturl));
}
function test_is_shorturl_http_www()
{
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'http', 'host' => self::$site_www]) . '/ozh';
$this->assertTrue(yourls_is_shorturl($shorturl));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment