Skip to content

Commit

Permalink
Fix broken codeception tests
Browse files Browse the repository at this point in the history
The previous commit introduced some changes that caused codeception
unit tests to break. This commit fixes this by introducing a new
service class Transifex_Live_Integration_WP_Services to wrap WP API
functionality. This refactoring enables mocking the WP API in the
tests.
Also a new test class, BaseTestCase, is introduced to cater for
different PHPUnit versions between production and development
environment.
  • Loading branch information
deathbird committed Apr 2, 2019
1 parent ae7814f commit 5113479
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 15 deletions.
6 changes: 4 additions & 2 deletions includes/lib/transifex-live-integration-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function __construct( $settings, $rewrite_options ) {
Plugin_Debug::logTrace();
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/common/transifex-live-integration-validators.php';
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/override/transifex-live-integration-generate-rewrite-rules.php';
include_once __DIR__ .'/transifex-live-integration-wp-services.php';
$this->rewrite_options = [ ];
$this->languages_regex = $settings['languages_regex'];
$this->source_language = $settings['source_language'];
Expand Down Expand Up @@ -97,6 +98,7 @@ public function __construct( $settings, $rewrite_options ) {
$this->rewrite_pattern = $pattern;
}
}
$this->wp_services = new Transifex_Live_Integration_WP_Services();
}

public function get_language_url( $atts ) {
Expand Down Expand Up @@ -146,7 +148,7 @@ function wp_hook() {
* @return string Returns modified link
*/

static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
$pattern ) {
Plugin_Debug::logTrace();
if ( !(isset( $pattern )) ) {
Expand All @@ -170,7 +172,7 @@ static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
if ( count( $m ) > 1 ) {
$link = str_replace( $m[1], $lang, $m[0] );
} else {
$site_host = parse_url(site_url())['host'];
$site_host = parse_url($this->wp_services->get_site_url())['host'];
$parsed_url = parse_url($link);
$link_host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
// change only wordpress links - not links reffering to other domains
Expand Down
20 changes: 20 additions & 0 deletions includes/lib/transifex-live-integration-wp-services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* Language rewrites
* @package TransifexLiveIntegration
*/

/**
* Provides WP services to other classes.
*/
class Transifex_Live_Integration_WP_Services {

/*
* Wraps WP site_url().
* @return string Site URL link with optional path appended.
*/
function get_site_url() {
return site_url();
}
}
19 changes: 19 additions & 0 deletions tests/unit/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/*
* This class caters for the different PHPUnit versions between production
* and development.
* To execute unit tests especially during development set an environment
* variable TEST_ENV=<environment> either as 'dev' or 'prod' :
*
* export TEST_ENV=<environment>
* php codecept.phar run unit --debug
*/
$test_env = null !== getenv('TEST_ENV') ? getenv('TEST_ENV') : 'prod';

if ($test_env !== 'dev') {
// php 5.6, production environment
class BaseTestCase extends \PHPUnit_Framework_TestCase {}
} else {
// php 7+, dev environment
class BaseTestCase extends PHPunit\Framework\TestCase {}
}
3 changes: 2 additions & 1 deletion tests/unit/CalculateDefaultsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class CalculateDefaultsTest extends \PHPUnit_Framework_TestCase
class CalculateDefaultsTest extends BaseTestCase
{
private $data_subdomain;
private $data_subdirectory;
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/GenerateLanguageUrlMapTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class GenerateLanguageUrlMapTest extends \PHPUnit_Framework_TestCase
class GenerateLanguageUrlMapTest extends BaseTestCase
{

private $data;
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/GenerateTokenizedUrlTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class GenerateTokenizedUrlTest extends \PHPUnit_Framework_TestCase
class GenerateTokenizedUrlTest extends BaseTestCase
{

private $data;
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/PrerenderCheckTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class PrerenderCheckTest extends \PHPUnit_Framework_TestCase
class PrerenderCheckTest extends BaseTestCase
{

private $data;
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/ReplaceSubdomainTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once __DIR__ .'/BaseTestCase.php';

class ReplaceSubdomainTest extends \PHPUnit_Framework_TestCase
class ReplaceSubdomainTest extends BaseTestCase
{

private $data;
Expand All @@ -9,6 +10,7 @@ protected function setUp()
{
include_once './includes/common/plugin-debug.php';
include_once './includes/common/transifex-live-integration-common.php';
include_once './includes/transifex-live-integration-util.php';
$this->data = [
[
'page_url' => 'https://www.foo.bar/about/',
Expand Down
49 changes: 41 additions & 8 deletions tests/unit/ReverseHardLinkTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<?php
/**
* Execute this test setting <environment> either as 'dev' or 'prod' :
*
* export TEST_ENV=<environment>
* php codecept.phar run unit --debug ReverseHardLinkTest
*/
include_once __DIR__ .'/BaseTestCase.php';

class ReverseHardLinkTest extends \PHPUnit_Framework_TestCase {
class ReverseHardLinkTest extends BaseTestCase {

private $data;

protected function setUp() {
include_once './includes/common/plugin-debug.php';
include_once './includes/lib/transifex-live-integration-rewrite.php';
include_once './includes/lib/transifex-live-integration-wp-services.php';
include_once '../../../wp-load.php';
$this->data = [[ //1
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/www.mydomain.com\/(zh_CN|de_DE)\//',
'result' => 'http://www.mydomain.com/zh_CN/page-markup-and-formatting'
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/www.mydomain.com\/(zh_CN|de_DE)\//',
'result' => 'http://www.mydomain.com/zh_CN/page-markup-and-formatting'
],
[ //2
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
Expand All @@ -24,6 +35,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //3
'host' => 'http://www.mydomain.com',
'lang' => 'zh_HK',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
Expand All @@ -32,6 +44,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //4
'host' => 'http://www.mydomain.com',
'lang' => 'cn',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ],
Expand All @@ -40,6 +53,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/cn/page-markup-and-formatting'
],
[ //5
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => [ ],
Expand All @@ -48,6 +62,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //6
'host' => 'http://www.mydomain.com',
'lang' => null,
'link' => 'http://www.mydomain.com/',
'languages_map' => null,
Expand All @@ -56,6 +71,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/'
],
[ //7
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ],
Expand All @@ -64,6 +80,7 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/page-markup-and-formatting'
],
[ //8
'host' => 'http://www.mydomain.com',
'lang' => 'zh_CN',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ],
Expand All @@ -72,6 +89,7 @@ protected function setUp() {
'result' => 'http://zh_CN.mydomain.com/page-markup-and-formatting'
],
[ //9
'host' => 'http://www.mydomain.com',
'lang' => 'cn',
'link' => 'http://www.mydomain.com/page-markup-and-formatting',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ],
Expand All @@ -80,6 +98,7 @@ protected function setUp() {
'result' => 'http://cn.mydomain.com/page-markup-and-formatting'
],
[ //10 plex case
'host' => 'http://www.mydomain.com',
'lang' => 'de',
'link' => 'http://www.mydomain.com/page-markup-and-formatting-de',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ],
Expand All @@ -88,25 +107,39 @@ protected function setUp() {
'result' => 'http://www.mydomain.com/de/page-markup-and-formatting-de'
],
[ //11
'host' => 'http://www.mydomain.com',
'lang' => 'de',
'link' => 'http://www.mydomain.com/page-markup-and-formatting-de',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/(cn|de|www).mydomain.com\/.*/',
'result' => 'http://de.mydomain.com/page-markup-and-formatting-de'
],
[ //12 external link, leave intact
'host' => 'http://www.mydomain.com',
'lang' => 'de',
'link' => 'http://www.another.com/page-markup-and-formatting-de',
'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ],
'souce_lang' => 'en',
'pattern' => '/http:\/\/www.mydomain.com\/(cn|de)\//',
'result' => 'http://www.another.com/page-markup-and-formatting-de'
]
];
}

public function testMe() {
$counter = 0;
$rewrite = new Transifex_Live_Integration_Rewrite( [], [] );
foreach ($this->data as $i) {
$counter = $counter + 1;
$result = Transifex_Live_Integration_Rewrite::reverse_hard_link(
// stub
$rewrite->wp_services = \Codeception\Stub::make(Transifex_Live_Integration_WP_Services::class, ['get_site_url' => $i['host']]);
$result = $rewrite->reverse_hard_link(
$i['lang'], $i['link'], $i['languages_map'], $i['souce_lang'], $i['pattern']
);

// eval(\Psy\sh());
// codecept_debug('result:' . $i['result']);
$this->assertEquals( $i['result'], $result, 'Test Number:' . $counter );
}
}
Expand Down

0 comments on commit 5113479

Please sign in to comment.