From 2fd244528d7c5e83d19144b57b5229771870d29a Mon Sep 17 00:00:00 2001 From: wxa Date: Thu, 25 Apr 2019 11:11:41 +0300 Subject: [PATCH] Move assorted tests from integration to unit [MAILPOET-2009] --- lib/API/JSON/ErrorResponse.php | 2 -- lib/API/JSON/Response.php | 2 -- lib/Form/Block/Date.php | 2 -- lib/Newsletter/Editor/PostContentManager.php | 2 -- lib/Newsletter/Editor/PostTransformer.php | 2 -- .../Editor/StructureTransformer.php | 2 -- lib/Util/Security.php | 2 -- .../API/JSON/ErrorResponseTest.php | 13 +++++++- .../Form/Block/DateTest.php | 2 +- .../Editor/StructureTransformerTest.php | 2 +- .../ImportExport/Import/MailChimpTest.php | 16 +++++++++- .../Util/SecurityTest.php | 10 +----- .../{integration => unit}/WP/DateTimeTest.php | 32 +++++++++++++++---- tests/unit/WP/PostsTest.php | 3 ++ 14 files changed, 59 insertions(+), 33 deletions(-) rename tests/{integration => unit}/API/JSON/ErrorResponseTest.php (70%) rename tests/{integration => unit}/Form/Block/DateTest.php (97%) rename tests/{integration => unit}/Newsletter/Editor/StructureTransformerTest.php (93%) rename tests/{integration => unit}/Subscribers/ImportExport/Import/MailChimpTest.php (92%) rename tests/{integration => unit}/Util/SecurityTest.php (76%) rename tests/{integration => unit}/WP/DateTimeTest.php (77%) diff --git a/lib/API/JSON/ErrorResponse.php b/lib/API/JSON/ErrorResponse.php index 9136c5d68b..97b2318434 100644 --- a/lib/API/JSON/ErrorResponse.php +++ b/lib/API/JSON/ErrorResponse.php @@ -3,8 +3,6 @@ namespace MailPoet\API\JSON; use MailPoet\WP\Functions as WPFunctions; -if (!defined('ABSPATH')) exit; - class ErrorResponse extends Response { public $errors; diff --git a/lib/API/JSON/Response.php b/lib/API/JSON/Response.php index 95f263b33f..10595c5eb1 100644 --- a/lib/API/JSON/Response.php +++ b/lib/API/JSON/Response.php @@ -2,8 +2,6 @@ namespace MailPoet\API\JSON; use MailPoet\WP\Functions as WPFunctions; -if (!defined('ABSPATH')) exit; - abstract class Response { const STATUS_OK = 200; const STATUS_BAD_REQUEST = 400; diff --git a/lib/Form/Block/Date.php b/lib/Form/Block/Date.php index 6874928cc2..fdb86c3772 100644 --- a/lib/Form/Block/Date.php +++ b/lib/Form/Block/Date.php @@ -1,8 +1,6 @@ function ($value) { + return $value; + } + ])); $errors = array( 'valid error', 'SQLSTATE[22001]: Some SQL error', @@ -31,4 +38,8 @@ class ErrorResponseTest extends \MailPoetTest { ) ); } + + function _after() { + WPFunctions::set(new WPFunctions); + } } \ No newline at end of file diff --git a/tests/integration/Form/Block/DateTest.php b/tests/unit/Form/Block/DateTest.php similarity index 97% rename from tests/integration/Form/Block/DateTest.php rename to tests/unit/Form/Block/DateTest.php index 03e2841a63..d930071e2c 100644 --- a/tests/integration/Form/Block/DateTest.php +++ b/tests/unit/Form/Block/DateTest.php @@ -3,7 +3,7 @@ namespace MailPoet\Test\Form\Block; use MailPoet\Form\Block\Date; -class DateTest extends \MailPoetTest { +class DateTest extends \MailPoetUnitTest { function testItCanConvertDateMonthYearFormatToDatetime() { $date = array( 'MM/DD/YYYY' => '05/10/2016', diff --git a/tests/integration/Newsletter/Editor/StructureTransformerTest.php b/tests/unit/Newsletter/Editor/StructureTransformerTest.php similarity index 93% rename from tests/integration/Newsletter/Editor/StructureTransformerTest.php rename to tests/unit/Newsletter/Editor/StructureTransformerTest.php index 6c85cb8584..043127df65 100644 --- a/tests/integration/Newsletter/Editor/StructureTransformerTest.php +++ b/tests/unit/Newsletter/Editor/StructureTransformerTest.php @@ -3,7 +3,7 @@ namespace MailPoet\Test\Newsletter\Editor; use MailPoet\Newsletter\Editor\StructureTransformer; -class StructureTransformerTest extends \MailPoetTest { +class StructureTransformerTest extends \MailPoetUnitTest { function _before() { parent::_before(); diff --git a/tests/integration/Subscribers/ImportExport/Import/MailChimpTest.php b/tests/unit/Subscribers/ImportExport/Import/MailChimpTest.php similarity index 92% rename from tests/integration/Subscribers/ImportExport/Import/MailChimpTest.php rename to tests/unit/Subscribers/ImportExport/Import/MailChimpTest.php index c385e01b1e..6695ee6245 100644 --- a/tests/integration/Subscribers/ImportExport/Import/MailChimpTest.php +++ b/tests/unit/Subscribers/ImportExport/Import/MailChimpTest.php @@ -1,9 +1,11 @@ api_key = getenv('WP_TEST_IMPORT_MAILCHIMP_API'); @@ -11,6 +13,14 @@ class MailChimpTest extends \MailPoetTest { $this->lists = explode(",", getenv('WP_TEST_IMPORT_MAILCHIMP_LISTS')); } + function _before() { + WPFunctions::set(Stub::make(new WPFunctions, [ + '__' => function ($value) { + return $value; + } + ])); + } + function testItCanGetAPIKey() { $valid_api_key_format = '12345678901234567890123456789012-ab1'; // key must consist of two parts separated by hyphen @@ -108,4 +118,8 @@ class MailChimpTest extends \MailPoetTest { ->contains('The information received from MailChimp is too large for processing'); } } + + function _after() { + WPFunctions::set(new WPFunctions); + } } diff --git a/tests/integration/Util/SecurityTest.php b/tests/unit/Util/SecurityTest.php similarity index 76% rename from tests/integration/Util/SecurityTest.php rename to tests/unit/Util/SecurityTest.php index 9fc4c2b41f..2fa22162dc 100644 --- a/tests/integration/Util/SecurityTest.php +++ b/tests/unit/Util/SecurityTest.php @@ -3,15 +3,7 @@ namespace MailPoet\Test\Util; use MailPoet\Util\Security; -class SecurityTest extends \MailPoetTest { - - function testItCanGenerateWPNonce() { - $wp_nonce = Security::generateToken(); - // expect length of nonce to be exactly 10 - expect(strlen($wp_nonce))->equals(10); - // expect only alphanumerical characters - expect(ctype_alnum($wp_nonce))->true(); - } +class SecurityTest extends \MailPoetUnitTest { function testItCanGenerateARandomString() { // it has a default length of 5 diff --git a/tests/integration/WP/DateTimeTest.php b/tests/unit/WP/DateTimeTest.php similarity index 77% rename from tests/integration/WP/DateTimeTest.php rename to tests/unit/WP/DateTimeTest.php index 29c852d61b..4d0db37787 100644 --- a/tests/integration/WP/DateTimeTest.php +++ b/tests/unit/WP/DateTimeTest.php @@ -5,7 +5,7 @@ use Codeception\Stub; use MailPoet\WP\DateTime as WPDateTime; use MailPoet\WP\Functions as WPFunctions; -class DateTimeTest extends \MailPoetTest { +class DateTimeTest extends \MailPoetUnitTest { function testGetTimeFormat() { $date_time = new WPDateTime(Stub::make(new WPFunctions(), [ @@ -40,17 +40,29 @@ class DateTimeTest extends \MailPoetTest { } function testGetCurrentDate() { - $date_time = new WPDateTime(); + $date_time = new WPDateTime(Stub::make(new WPFunctions(), [ + 'currentTime' => function($format) { + return date($format); + } + ])); expect($date_time->getCurrentDate("Y-m"))->equals(date("Y-m")); } function testGetCurrentTime() { - $date_time = new WPDateTime(); + $date_time = new WPDateTime(Stub::make(new WPFunctions(), [ + 'currentTime' => function($format) { + return date($format); + } + ])); expect($date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/'); } function testFormatTime() { - $date_time = new WPDateTime(); + $date_time = new WPDateTime(Stub::make(new WPFunctions(), [ + 'getOption' => function($key) { + return 'H:i'; + } + ])); $timestamp = 1234567; $format = "H:i:s"; expect($date_time->formatTime($timestamp))->equals(date($date_time->getTimeFormat(), $timestamp)); @@ -58,7 +70,11 @@ class DateTimeTest extends \MailPoetTest { } function testFormatDate() { - $date_time = new WPDateTime(); + $date_time = new WPDateTime(Stub::make(new WPFunctions(), [ + 'getOption' => function($key) { + return 'm-d'; + } + ])); $timestamp = 1234567; $format = "Y-m-d"; expect($date_time->formatDate($timestamp))->equals(date($date_time->getDateFormat(), $timestamp)); @@ -66,7 +82,11 @@ class DateTimeTest extends \MailPoetTest { } function testTimeInterval() { - $date_time = new WPDateTime(); + $date_time = new WPDateTime(Stub::make(new WPFunctions(), [ + 'getOption' => function($key) { + return 'H:i'; + } + ])); $one_hour_interval = array_keys($date_time->getTimeInterval( '00:00:00', '+1 hour', diff --git a/tests/unit/WP/PostsTest.php b/tests/unit/WP/PostsTest.php index 37dadd515b..b1385c8932 100644 --- a/tests/unit/WP/PostsTest.php +++ b/tests/unit/WP/PostsTest.php @@ -54,4 +54,7 @@ class PostsTest extends \MailPoetUnitTest { expect($result['arguments'][1])->equals(array_diff_key($args, array('taxonomy' => ''))); } + function _after() { + WPFunctions::set(new WPFunctions); + } }