From b94271d2e45cf5b3309c31bd034f124c854f33cb Mon Sep 17 00:00:00 2001 From: peter279k Date: Tue, 29 Oct 2019 13:12:38 +0800 Subject: [PATCH] Test enhancement --- .travis.yml | 1 + composer.json | 5 +++++ tests/Unit/EngineClientTest.php | 3 ++- tests/Unit/EventClientTest.php | 5 +++-- tests/Unit/ExporterTest.php | 35 +++++++++++++++++---------------- tests/Unit/FileExporterTest.php | 5 +++-- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index b10fe09..6614e2e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ # limitations under the License. language: php php: + - 7.3 - 7.2 - 7.1 - 7.0 diff --git a/composer.json b/composer.json index 917e5b6..b965af8 100644 --- a/composer.json +++ b/composer.json @@ -32,5 +32,10 @@ "psr-4": { "predictionio\\": "src/predictionio/" } + }, + "autoload-dev": { + "psr-4": { + "predictionio\\tests\\Unit\\": "tests/Unit/" + } } } diff --git a/tests/Unit/EngineClientTest.php b/tests/Unit/EngineClientTest.php index 2f5fbbf..7c0b3fd 100644 --- a/tests/Unit/EngineClientTest.php +++ b/tests/Unit/EngineClientTest.php @@ -24,8 +24,9 @@ use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\TestCase; -class EngineClientTest extends \PHPUnit_Framework_TestCase +class EngineClientTest extends TestCase { /** @var EngineClient $engineClient */ protected $engineClient; diff --git a/tests/Unit/EventClientTest.php b/tests/Unit/EventClientTest.php index 6c442ea..84905ee 100644 --- a/tests/Unit/EventClientTest.php +++ b/tests/Unit/EventClientTest.php @@ -24,8 +24,9 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use predictionio\EventClient; +use PHPUnit\Framework\TestCase; -class EventClientTest extends \PHPUnit_Framework_TestCase +class EventClientTest extends TestCase { /** @var EventClient $eventClient */ protected $eventClient; @@ -319,7 +320,7 @@ public function testGetEvent() $result=array_shift($this->container); /** @var Request $request */ $request=$result['request']; - $body=json_decode($request->getBody(), true); + json_decode($request->getBody(), true); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals( diff --git a/tests/Unit/ExporterTest.php b/tests/Unit/ExporterTest.php index 7f3690a..56d68eb 100644 --- a/tests/Unit/ExporterTest.php +++ b/tests/Unit/ExporterTest.php @@ -18,6 +18,7 @@ namespace predictionio\tests\Unit; use predictionio\Exporter; +use PHPUnit\Framework\TestCase; class TestExporter { @@ -46,7 +47,7 @@ public function export($json) } } -class ExporterTest extends \PHPUnit_Framework_TestCase +class ExporterTest extends TestCase { /** @var TestExporter $exporter */ @@ -63,18 +64,18 @@ public function testTimeIsNow() $this->exporter->createEvent('event', 'entity-type', 'entity-id'); - $this->assertEquals(1, count($this->exporter->data)); + $this->assertCount(1, $this->exporter->data); $data = $this->exporter->data[0]; - $this->assertEquals(4, count($data)); + $this->assertCount(4, $data); $this->assertEquals('event', $data['event']); $this->assertEquals('entity-type', $data['entityType']); $this->assertEquals('entity-id', $data['entityId']); $this->assertEquals($time->format(\DateTime::ISO8601), $data['eventTime'], 'time is now', 1); - $this->assertEquals(1, count($this->exporter->json)); + $this->assertCount(1, $this->exporter->json); $json = $this->exporter->json[0]; $pattern = '/^{"event":"event","entityType":"entity-type","entityId":"entity-id","eventTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}"}$/'; - $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); + $this->assertRegExp($pattern, $json, 'json'); } public function testTimeIsString() @@ -83,18 +84,18 @@ public function testTimeIsString() $this->exporter->createEvent('event', 'entity-type', 'entity-id', null, null, null, '2015-04-01'); - $this->assertEquals(1, count($this->exporter->data)); + $this->assertCount(1, $this->exporter->data); $data = $this->exporter->data[0]; - $this->assertEquals(4, count($data)); + $this->assertCount(4, $data); $this->assertEquals('event', $data['event']); $this->assertEquals('entity-type', $data['entityType']); $this->assertEquals('entity-id', $data['entityId']); $this->assertEquals($time->format(\DateTime::ISO8601), $data['eventTime'], 'time is string', 1); - $this->assertEquals(1, count($this->exporter->json)); + $this->assertCount(1, $this->exporter->json); $json = $this->exporter->json[0]; $pattern = '/^{"event":"event","entityType":"entity-type","entityId":"entity-id","eventTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}"}$/'; - $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); + $this->assertRegExp($pattern, $json, 'json'); } public function testTimeIsDateTime() @@ -103,18 +104,18 @@ public function testTimeIsDateTime() $this->exporter->createEvent('event', 'entity-type', 'entity-id', null, null, null, $time); - $this->assertEquals(1, count($this->exporter->data)); + $this->assertCount(1, $this->exporter->data); $data = $this->exporter->data[0]; - $this->assertEquals(4, count($data)); + $this->assertCount(4, $data); $this->assertEquals('event', $data['event']); $this->assertEquals('entity-type', $data['entityType']); $this->assertEquals('entity-id', $data['entityId']); $this->assertEquals($time->format(\DateTime::ISO8601), $data['eventTime'], 'time is DateTime', 1); - $this->assertEquals(1, count($this->exporter->json)); + $this->assertCount(1, $this->exporter->json); $json = $this->exporter->json[0]; $pattern = '/^{"event":"event","entityType":"entity-type","entityId":"entity-id","eventTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}"}$/'; - $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); + $this->assertRegExp($pattern, $json, 'json'); } public function testOptionalFields() @@ -131,9 +132,9 @@ public function testOptionalFields() $time ); - $this->assertEquals(1, count($this->exporter->data)); + $this->assertCount(1, $this->exporter->data); $data = $this->exporter->data[0]; - $this->assertEquals(7, count($data)); + $this->assertCount(7, $data); $this->assertEquals('event', $data['event']); $this->assertEquals('entity-type', $data['entityType']); $this->assertEquals('entity-id', $data['entityId']); @@ -142,9 +143,9 @@ public function testOptionalFields() $this->assertEquals('target-entity-id', $data['targetEntityId']); $this->assertEquals(['property'=>true], $data['properties']); - $this->assertEquals(1, count($this->exporter->json)); + $this->assertCount(1, $this->exporter->json); $json = $this->exporter->json[0]; $pattern = '/^{"event":"event","entityType":"entity-type","entityId":"entity-id","eventTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}","targetEntityType":"target-entity-type","targetEntityId":"target-entity-id","properties":{"property":true}}$/'; - $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); + $this->assertRegExp($pattern, $json, 'json'); } } diff --git a/tests/Unit/FileExporterTest.php b/tests/Unit/FileExporterTest.php index f7f43cd..702731c 100644 --- a/tests/Unit/FileExporterTest.php +++ b/tests/Unit/FileExporterTest.php @@ -18,10 +18,11 @@ namespace predictionio\tests\Unit; use predictionio\FileExporter; +use PHPUnit\Framework\TestCase; -class FileExporterTest extends \PHPUnit_Framework_TestCase +class FileExporterTest extends TestCase { - public function setUp() + protected function setUp() { register_shutdown_function(function () { if (file_exists('temp.file')) {