Skip to content

Commit

Permalink
Merge pull request #437 from seamuslee001/test_voids
Browse files Browse the repository at this point in the history
Add in Void to Tests and setUp and tearDown functions for phpunit8 co…
  • Loading branch information
eileenmcnaughton authored Mar 18, 2021
2 parents 5a729ca + 4d820b7 commit d3baee3
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 188 deletions.
6 changes: 3 additions & 3 deletions tests/phpunit/CRM/Mosaico/AbDemuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUpHeadless() {
->apply();
}

protected function setUp() {
protected function setUp(): void {
parent::setUp();

Civi::settings()->set('disable_mandatory_tokens_check', TRUE);
Expand Down Expand Up @@ -61,7 +61,7 @@ public function getVariantExamples() {
*
* @dataProvider getVariantExamples
*/
public function testMailingSubmit($inputVariantA, $inputVariantB, $expectVariantA, $expectVariantB) {
public function testMailingSubmit($inputVariantA, $inputVariantB, $expectVariantA, $expectVariantB): void {
$this->assertTrue(Civi::container()->has('mosaico_ab_demux'), 'Mosaico services should be active in test environment');
$createParams = [
'template_type' => 'mosaico',
Expand Down Expand Up @@ -138,7 +138,7 @@ public function groupPctProvider() {
* @param $expectedCountC
* @dataProvider groupPctProvider
*/
public function testDistribution($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC) {
public function testDistribution($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC): void {
$this->assertTrue(Civi::container()->has('mosaico_ab_demux'), 'Mosaico services should be active in test environment');

$groupId = $this->groupCreate();
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Mosaico/ImageFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function filterExamples() {
* @param string $expectHtml
* @dataProvider filterExamples
*/
public function testFilter($config, $inputHtml, $expectHtml) {
public function testFilter($config, $inputHtml, $expectHtml): void {
$filter = new CRM_Mosaico_ImageFilter($config);
$e = \Civi\Core\Event\GenericHookEvent::create([
'content' => [
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/CRM/Mosaico/MosaicoBaseTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public static function setUpBeforeClass() {
// \Civi\Test::e2e()->uninstall('*')->install('org.civicrm.*')->apply();
}

public function setUp() {
public function setUp(): void {
parent::setUp();
}

public function tearDown() {
public function tearDown(): void {
parent::tearDown();
}

public function testGet() {
public function testGet(): void {
$result = $this->callAPISuccess('MosaicoBaseTemplate', 'get', []);
$this->assertTrue(is_array($result['values']));
$this->assertEquals('versafix-1', $result['values']['versafix-1']['name']);
$this->assertRegExp(';\.html$;', $result['values']['versafix-1']['path']);
$this->assertRegExp(';https?://.*mosaico.*versafix-1.*png;', $result['values']['versafix-1']['thumbnail']);
}

public function testGetSingle() {
public function testGetSingle(): void {
$result = $this->callAPISuccess('MosaicoBaseTemplate', 'getsingle', ['name' => 'tedc15']);
$this->assertEquals('tedc15', $result['name']);
$this->assertRegExp(';\.html$;', $result['path']);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Mosaico/MosaicoComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getComposerExamples() {
*
* @dataProvider getComposerExamples
*/
public function testMailingPreview($field, $inputValue, $expectRegex) {
public function testMailingPreview($field, $inputValue, $expectRegex): void {
$this->assertEquals('installed', CRM_Extension_System::singleton()->getManager()->getStatus('org.civicrm.flexmailer'));

$contactId = $this->getContactId($GLOBALS['_CV']['ADMIN_USER']);
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/CRM/Mosaico/MosaicoTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public static function setUpBeforeClass() {
// \Civi\Test::e2e()->uninstall('*')->install('org.civicrm.*')->apply();
}

public function setUp() {
public function setUp(): void {
parent::setUp();
}

public function tearDown() {
public function tearDown(): void {
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mosaico_template WHERE title LIKE "MosaicoTemplateTest%"');
parent::tearDown();
}

public function testCreateGetDelete() {
public function testCreateGetDelete(): void {
$r = rand();
$createResult = $this->callAPISuccess('MosaicoTemplate', 'create', [
'title' => 'MosaicoTemplateTest ' . $r,
Expand All @@ -61,7 +61,7 @@ public function testCreateGetDelete() {
]);
}

public function testClone() {
public function testClone(): void {
$createResult = $this->callAPISuccess('MosaicoTemplate', 'create', [
'title' => 'MosaicoTemplateTest foo',
'base' => 'versafix-1',
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Mosaico/ResizeScaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CRM_Mosaico_ResizeScaleTest extends CRM_Mosaico_TestCase implements EndToE
/**
* Test adjustResizeDimensions method for various inputs.
*/
public function testResizeScaleFactor() {
public function testResizeScaleFactor(): void {
// 3x - upto 190 pixels
Civi::settings()->set('mosaico_scale_width_limit1', 190);
Civi::settings()->set('mosaico_scale_factor1', 3);
Expand Down
169 changes: 1 addition & 168 deletions tests/phpunit/CRM/Mosaico/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,173 +2,6 @@

class CRM_Mosaico_TestCase extends PHPUnit\Framework\TestCase {

private $_apiversion = 3;

/**
* wrap api functions.
* so we can ensure they succeed & throw exceptions without litterering the test with checks
*
* @param string $entity
* @param string $action
* @param array $params
* @param mixed $checkAgainst
* Optional value to check result against, implemented for getvalue,.
* getcount, getsingle. Note that for getvalue the type is checked rather than the value
* for getsingle the array is compared against an array passed in - the id is not compared (for
* better or worse )
*
* @return array|int
*/
public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL) {
$params = array_merge([
'version' => $this->_apiversion,
'debug' => 1,
],
$params
);
switch (strtolower($action)) {
case 'getvalue':
return $this->callAPISuccessGetValue($entity, $params, $checkAgainst);

case 'getsingle':
return $this->callAPISuccessGetSingle($entity, $params, $checkAgainst);

case 'getcount':
return $this->callAPISuccessGetCount($entity, $params, $checkAgainst);
}
$result = $this->civicrm_api($entity, $action, $params);
$this->assertAPISuccess($result, "Failure in api call for $entity $action");
return $result;
}

/**
* This function exists to wrap api getValue function & check the result
* so we can ensure they succeed & throw exceptions without litterering the test with checks
* There is a type check in this
*
* @param string $entity
* @param array $params
* @param string $type
* Per http://php.net/manual/en/function.gettype.php possible types.
* - boolean
* - integer
* - double
* - string
* - array
* - object
*
* @return array|int
*/
public function callAPISuccessGetValue($entity, $params, $type = NULL) {
$params += [
'version' => $this->_apiversion,
'debug' => 1,
];
$result = $this->civicrm_api($entity, 'getvalue', $params);
if ($type) {
if ($type == 'integer') {
// api seems to return integers as strings
$this->assertTrue(is_numeric($result), "expected a numeric value but got " . print_r($result, 1));
}
else {
$this->assertType($type, $result, "returned result should have been of type $type but was ");
}
}
return $result;
}

/**
* This function exists to wrap api getValue function & check the result
* so we can ensure they succeed & throw exceptions without litterering the test with checks
* There is a type check in this
* @param string $entity
* @param array $params
* @param null $count
* @throws Exception
* @return array|int
*/
public function callAPISuccessGetCount($entity, $params, $count = NULL) {
$params += [
'version' => $this->_apiversion,
'debug' => 1,
];
$result = $this->civicrm_api($entity, 'getcount', $params);
if (!is_int($result) || !empty($result['is_error']) || isset($result['values'])) {
throw new Exception('Invalid getcount result : ' . print_r($result, TRUE) . " type :" . gettype($result));
}
if (is_int($count)) {
$this->assertEquals($count, $result, "incorrect count returned from $entity getcount");
}
return $result;
}

/**
* This function exists to wrap api getsingle function & check the result
* so we can ensure they succeed & throw exceptions without litterering the test with checks
*
* @param string $entity
* @param array $params
* @param array $checkAgainst
* Array to compare result against.
* - boolean
* - integer
* - double
* - string
* - array
* - object
*
* @throws Exception
* @return array|int
*/
public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) {
$params += [
'version' => $this->_apiversion,
'debug' => 1,
];
$result = $this->civicrm_api($entity, 'getsingle', $params);
if (!is_array($result) || !empty($result['is_error']) || isset($result['values'])) {
throw new Exception('Invalid getsingle result' . print_r($result, TRUE));
}
if ($checkAgainst) {
// @todo - have gone with the fn that unsets id? should we check id?
$this->checkArrayEquals($result, $checkAgainst);
}
return $result;
}

/**
* Check that api returned 'is_error' => 0.
*
* @param array $apiResult
* Api result.
* @param string $prefix
* Extra test to add to message.
*/
public function assertAPISuccess($apiResult, $prefix = '') {
if (!empty($prefix)) {
$prefix .= ': ';
}
$errorMessage = empty($apiResult['error_message']) ? '' : " " . $apiResult['error_message'];

if (!empty($apiResult['debug_information'])) {
$errorMessage .= "\n " . print_r($apiResult['debug_information'], TRUE);
}
if (!empty($apiResult['trace'])) {
$errorMessage .= "\n" . print_r($apiResult['trace'], TRUE);
}
$this->assertEquals(0, $apiResult['is_error'], $prefix . $errorMessage);
}

/**
* A stub for the API interface. This can be overriden by subclasses to change how the API is called.
*
* @param $entity
* @param $action
* @param array $params
* @return array|int
*/
public function civicrm_api($entity, $action, $params) {
return civicrm_api($entity, $action, $params);
}
use \Civi\Test\Api3TestTrait;

}
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Mosaico/UrlFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function filterExamples() {
* @param string $expectHtml
* @dataProvider filterExamples
*/
public function testFilterArray($baseUrl, $inputHtml, $expectHtml) {
public function testFilterArray($baseUrl, $inputHtml, $expectHtml): void {
$filter = new CRM_Mosaico_UrlFilter();
$filter->setBaseUrl($baseUrl);
list($actual) = $filter->filterHtml([$inputHtml]);
Expand All @@ -96,7 +96,7 @@ public function testFilterArray($baseUrl, $inputHtml, $expectHtml) {
* @param string $expectHtml
* @dataProvider filterExamples
*/
public function testFilterString($baseUrl, $inputHtml, $expectHtml) {
public function testFilterString($baseUrl, $inputHtml, $expectHtml): void {
$filter = new CRM_Mosaico_UrlFilter();
$filter->setBaseUrl($baseUrl);
$actual = $filter->filterHtml($inputHtml);
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/api/v3/Job/MosaicoMigrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function setUpHeadless() {
/**
* The setup() method is executed before the test is executed (optional).
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
}

/**
* The tearDown() method is executed after the test was executed (optional)
* This can be used for cleanup.
*/
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
}

Expand All @@ -41,7 +41,7 @@ public function tearDown() {
*
* Note how the function name begins with the word "test".
*/
public function testMigrate() {
public function testMigrate(): void {
$this->createExampleLegacyTemplate();
$this->assertEquals(1, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_mosaico_msg_template'));
$this->assertEquals(0, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_mosaico_template'));
Expand All @@ -64,7 +64,7 @@ public function testMigrate() {
$this->assertEquals(1, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_mosaico_template'));
}

protected function createExampleLegacyTemplate() {
protected function createExampleLegacyTemplate(): void {
$msgTpl = civicrm_api3('MessageTemplate', 'create', [
'msg_title' => 'The Title',
'msg_subject' => 'The Subject',
Expand Down

0 comments on commit d3baee3

Please sign in to comment.