Skip to content

Commit aff949d

Browse files
committed
Fixed export issue, and fixed tests for PHP 7.2
Fixed format processing for exportRepeatingInstrumentsAndEvents, and made changes to the automated tests to get them to work for PHP7.2.
1 parent 7af71a5 commit aff949d

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
bootstrap="vendor/autoload.php">
44
<testsuites>
55
<testsuite name="unit">
6+
<file>tests/unit/ErrorHandlerTest.php</file>
67
<file>tests/unit/FileUtilTest.php</file>
78
<file>tests/unit/PhpCapExceptionTest.php</file>
89
<file>tests/unit/RedCapApiConnectionTest.php</file>

src/ErrorHandler.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ public function throwException(
2222
$httpStatusCode = null,
2323
$previousException = null
2424
) {
25-
throw new PhpCapException(
26-
$message,
27-
$code,
28-
$connectionErrorNumber,
29-
$httpStatusCode,
30-
$previousException
31-
);
25+
throw new PhpCapException($message, $code, $connectionErrorNumber, $httpStatusCode, $previousException);
3226
}
3327
}

src/RedCap.php

+17-20
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,7 @@ public function __construct(
7373
$errorHandler = null,
7474
$connection = null
7575
) {
76-
return new RedCapProject(
77-
$apiUrl,
78-
$apiToken,
79-
$sslVerify,
80-
$caCertificateFile,
81-
$errorHandler,
82-
$connection
83-
);
76+
return new RedCapProject($apiUrl, $apiToken, $sslVerify, $caCertificateFile, $errorHandler, $connection);
8477
};
8578
}
8679

@@ -174,13 +167,17 @@ public function createProject(
174167

175168
$projectConstructorCallback = $this->projectConstructorCallback;
176169

170+
# Note: due to an issue with Xdebug, arguments to multi-line function
171+
# calls, other than the last one, may not be handled correctly
172+
# for code coverage, so the code coverage ignore annotations
173+
# needed to be added below.
177174
$project = call_user_func(
178-
$projectConstructorCallback,
179-
$apiUrl = null,
180-
$apiToken,
181-
$sslVerify = null,
182-
$caCertificateFile = null,
183-
$errorHandler,
175+
$projectConstructorCallback, // @codeCoverageIgnore
176+
$apiUrl = null, // @codeCoverageIgnore
177+
$apiToken, // @codeCoverageIgnore
178+
$sslVerify = null, // @codeCoverageIgnore
179+
$caCertificateFile = null, // @codeCoverageIgnore
180+
$errorHandler, // @codeCoverageIgnore
184181
$connection
185182
);
186183

@@ -205,12 +202,12 @@ public function getProject($apiToken)
205202

206203
# By default, this creates a RedCapProject
207204
$project = call_user_func(
208-
$projectConstructorCallback,
209-
$apiUrl = null,
210-
$apiToken,
211-
$sslVerify = null,
212-
$caCertificateFile = null,
213-
$errorHandler,
205+
$projectConstructorCallback, // @codeCoverageIgnore
206+
$apiUrl = null, // @codeCoverageIgnore
207+
$apiToken, // @codeCoverageIgnore
208+
$sslVerify = null, // @codeCoverageIgnore
209+
$caCertificateFile = null, // @codeCoverageIgnore
210+
$errorHandler, // @codeCoverageIgnore
214211
$connection
215212
);
216213

src/RedCapProject.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ public function exportRepeatingInstrumentsAndEvents($format = 'php')
14221422

14231423
$result = $this->connection->callWithArray($data);
14241424

1425-
$this->processNonExportResult($result);
1425+
$this->processExportResult($result, $format);
14261426

14271427
return $result;
14281428
}

tests/integration/RecordsTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,11 @@ public function testExportRecordsAsCsv()
430430
$recordIds = array ('1001');
431431

432432
$records = self::$basicDemographyProject->exportRecords($format = 'csv', $type = null, $recordIds);
433-
434-
$this->assertEquals(count($records), 1, 'Correct number of records returned test.');
435433

436434
$parser = \KzykHys\CsvParser\CsvParser::fromString($records);
437435
$csv = $parser->parse();
438-
436+
$this->assertEquals(2, count($csv), 'Correct number of records returned test.');
437+
439438
$firstDataRow = $csv[1];
440439

441440
$csvRecordId = $firstDataRow[0];
@@ -453,7 +452,7 @@ public function testExportRecordsAsOdm()
453452

454453
$records = self::$basicDemographyProject->exportRecords($format = 'odm', $type = null, $recordIds);
455454

456-
$this->assertEquals(count($records), 1, 'Correct number of records returned test.');
455+
$this->assertTrue(is_string($records), 'Correct type for records returned test.');
457456

458457
$xml = new \DomDocument();
459458
$xml->loadXML($records);
@@ -477,7 +476,7 @@ public function testExportRecordsAsXml()
477476

478477
$records = self::$basicDemographyProject->exportRecords($format = 'xml', $type = null, $recordIds);
479478

480-
$this->assertEquals(count($records), 1, 'Correct number of records returned test.');
479+
$this->assertTrue(is_string($records), 1, 'Correct type for records returned test.');
481480

482481
$xml = simplexml_load_string($records);
483482

@@ -841,7 +840,7 @@ public function testImportAndDeleteRecordsXmlFormat()
841840
.'</item> </records>';
842841

843842
$result = self::$basicDemographyProject->importRecords($records, $format = 'xml');
844-
$this->assertEquals(1, count($result), 'Record count.');
843+
$this->assertEquals(1, $result, 'Import result value.');
845844

846845
$result = self::$basicDemographyProject->exportRecords();
847846
$this->assertEquals(101, count($result), 'Record count after import.');

tests/unit/ErrorHandlerTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace IU\PHPCap;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* PHPUnit tests for ErrorHandler class.
9+
*/
10+
class ErrorHandlerTest extends TestCase
11+
{
12+
public function test()
13+
{
14+
$errorHandler = new ErrorHandler();
15+
$this->assertNotNull($errorHandler, 'Error handler not null.');
16+
$this->assertTrue($errorHandler instanceof ErrorHandlerInterface);
17+
18+
$exceptionCaught = false;
19+
$expectedMessage = 'Error handler test.';
20+
$expectedCode = ErrorHandlerInterface::REDCAP_API_ERROR;
21+
try {
22+
$errorHandler->throwException($expectedMessage, $expectedCode);
23+
} catch (\Exception $exception) {
24+
$exceptionCaught = true;
25+
$message = $exception->getMessage();
26+
$code = $exception->getCode();
27+
}
28+
29+
$this->assertTrue($exceptionCaught);
30+
$this->assertEquals($expectedMessage, $message);
31+
$this->assertEquals($expectedCode, $code);
32+
}
33+
}

0 commit comments

Comments
 (0)