Repository: predictionio-sdk-php Updated Branches: refs/heads/develop 2b99dcc00 -> f1e33f3ad
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/f1e33f3a/tests/Unit/EventClientTest.php ---------------------------------------------------------------------- diff --git a/tests/Unit/EventClientTest.php b/tests/Unit/EventClientTest.php index 7a463ff..6c442ea 100644 --- a/tests/Unit/EventClientTest.php +++ b/tests/Unit/EventClientTest.php @@ -1,4 +1,20 @@ <?php + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + namespace predictionio\tests\Unit; use GuzzleHttp\Client; @@ -9,243 +25,263 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use predictionio\EventClient; -class EventClientTest extends \PHPUnit_Framework_TestCase { - /** @var EventClient $eventClient */ - protected $eventClient; - protected $container = []; - - protected function setUp() { - $history=Middleware::History($this->container); - $mock=new MockHandler([new Response(200)]); - $handler=HandlerStack::create($mock); - $handler->push($history); - $this->eventClient=new EventClient( - "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O"); - $existingOptions = $this->eventClient->client->getConfig(); - $existingOptions['handler'] = $handler; - $mockClient=new Client($existingOptions); - $this->eventClient->client = $mockClient; - } - - public function testSetUser() { - $this->eventClient->setUser(1,array('age'=>20)); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$set',$body['event']); - $this->assertEquals('user',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertEquals(20,$body['properties']['age']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testSetUserWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->setUser(1,array('age'=>20), $eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$set',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - public function testUnsetUser() { - $this->eventClient->unsetUser(1,array('age'=>20)); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$unset',$body['event']); - $this->assertEquals('user',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertEquals(20,$body['properties']['age']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testUnsetUserWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->unsetUser(1,array('age'=>20), $eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$unset',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - /** - * @expectedException \predictionio\PredictionIOAPIError - */ - public function testUnsetUserWithoutProperties() { - $this->eventClient->unsetUser(1, array()); - } - - public function testDeleteUser() { - $this->eventClient->deleteUser(1); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$delete',$body['event']); - $this->assertEquals('user',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testDeleteUserWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->deleteUser(1, $eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$delete',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - public function testSetItem() { - $this->eventClient->setItem(1,array('type'=>'book')); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$set',$body['event']); - $this->assertEquals('item',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertEquals('book',$body['properties']['type']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testSetItemWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->setItem(1,array('type'=>'book'), $eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$set',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - public function testUnsetItem() { - $this->eventClient->unsetItem(1,array('type'=>'book')); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$unset',$body['event']); - $this->assertEquals('item',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertEquals('book',$body['properties']['type']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testUnsetItemWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->unsetItem(1,array('type'=>'book'), $eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$unset',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - /** - * @expectedException \predictionio\PredictionIOAPIError - */ - public function testUnsetItemWithoutProperties() { - $this->eventClient->unsetItem(1, array()); - } - - public function testDeleteItem() { - $this->eventClient->deleteItem(1); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$delete',$body['event']); - $this->assertEquals('item',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testDeleteItemWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->deleteItem(1, $eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('$delete',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - public function testRecordAction() { - $this->eventClient->recordUserActionOnItem('view',1,888, array('count'=>2)); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('view',$body['event']); - $this->assertEquals('user',$body['entityType']); - $this->assertEquals(1,$body['entityId']); - $this->assertEquals('item',$body['targetEntityType']); - $this->assertEquals(888,$body['targetEntityId']); - $this->assertEquals(2,$body['properties']['count']); - $this->assertNotNull($body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testRecordActionWithEventTime() { - $eventTime='1982-09-25T01:23:45+0800'; - - $this->eventClient->recordUserActionOnItem('view',1, 8, array(),$eventTime); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('view',$body['event']); - $this->assertEquals($eventTime, $body['eventTime']); - } - - public function testCreateEvent() { - $this->eventClient->createEvent(array( +class EventClientTest extends \PHPUnit_Framework_TestCase +{ + /** @var EventClient $eventClient */ + protected $eventClient; + protected $container = []; + + protected function setUp() + { + $history=Middleware::History($this->container); + $mock=new MockHandler([new Response(200)]); + $handler=HandlerStack::create($mock); + $handler->push($history); + $this->eventClient=new EventClient( + "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O" + ); + $existingOptions = $this->eventClient->client->getConfig(); + $existingOptions['handler'] = $handler; + $mockClient=new Client($existingOptions); + $this->eventClient->client = $mockClient; + } + + public function testSetUser() + { + $this->eventClient->setUser(1, array('age'=>20)); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$set', $body['event']); + $this->assertEquals('user', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertEquals(20, $body['properties']['age']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testSetUserWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->setUser(1, array('age'=>20), $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$set', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + public function testUnsetUser() + { + $this->eventClient->unsetUser(1, array('age'=>20)); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$unset', $body['event']); + $this->assertEquals('user', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertEquals(20, $body['properties']['age']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testUnsetUserWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->unsetUser(1, array('age'=>20), $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$unset', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + /** + * @expectedException \predictionio\PredictionIOAPIError + */ + public function testUnsetUserWithoutProperties() + { + $this->eventClient->unsetUser(1, array()); + } + + public function testDeleteUser() + { + $this->eventClient->deleteUser(1); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$delete', $body['event']); + $this->assertEquals('user', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testDeleteUserWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->deleteUser(1, $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$delete', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + public function testSetItem() + { + $this->eventClient->setItem(1, array('type'=>'book')); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$set', $body['event']); + $this->assertEquals('item', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertEquals('book', $body['properties']['type']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testSetItemWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->setItem(1, array('type'=>'book'), $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$set', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + public function testUnsetItem() + { + $this->eventClient->unsetItem(1, array('type'=>'book')); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$unset', $body['event']); + $this->assertEquals('item', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertEquals('book', $body['properties']['type']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testUnsetItemWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->unsetItem(1, array('type'=>'book'), $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$unset', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + /** + * @expectedException \predictionio\PredictionIOAPIError + */ + public function testUnsetItemWithoutProperties() + { + $this->eventClient->unsetItem(1, array()); + } + + public function testDeleteItem() + { + $this->eventClient->deleteItem(1); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$delete', $body['event']); + $this->assertEquals('item', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testDeleteItemWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->deleteItem(1, $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('$delete', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + public function testRecordAction() + { + $this->eventClient->recordUserActionOnItem('view', 1, 888, array('count'=>2)); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('view', $body['event']); + $this->assertEquals('user', $body['entityType']); + $this->assertEquals(1, $body['entityId']); + $this->assertEquals('item', $body['targetEntityType']); + $this->assertEquals(888, $body['targetEntityId']); + $this->assertEquals(2, $body['properties']['count']); + $this->assertNotNull($body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testRecordActionWithEventTime() + { + $eventTime='1982-09-25T01:23:45+0800'; + + $this->eventClient->recordUserActionOnItem('view', 1, 8, array(), $eventTime); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('view', $body['event']); + $this->assertEquals($eventTime, $body['eventTime']); + } + + public function testCreateEvent() + { + $this->eventClient->createEvent(array( 'event' => 'my_event', 'entityType' => 'user', 'entityId' => 'uid', @@ -255,43 +291,40 @@ class EventClientTest extends \PHPUnit_Framework_TestCase { 'prop4'=>true, 'prop5'=>array('a','b','c'), 'prop6'=>4.56 - ), + ), 'eventTime' => '2004-12-13T21:39:45.618-07:00' )); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('my_event',$body['event']); - $this->assertEquals('user',$body['entityType']); - $this->assertEquals('uid',$body['entityId']); - $this->assertEquals(1,$body['properties']['prop1']); - $this->assertEquals('value2',$body['properties']['prop2']); - $this->assertEquals(array(1,2,3),$body['properties']['prop3']); - $this->assertEquals(true,$body['properties']['prop4']); - $this->assertEquals(array('a','b','c'),$body['properties']['prop5']); - $this->assertEquals(4.56,$body['properties']['prop6']); - $this->assertEquals('2004-12-13T21:39:45.618-07:00',$body['eventTime']); - $this->assertEquals('POST',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri()); - } - - public function testGetEvent() { - $this->eventClient->getEvent('event_id'); - $result=array_shift($this->container); - /** @var Request $request */ - $request=$result['request']; - $body=json_decode($request->getBody(), true); - - $this->assertEquals('GET',$request->getMethod()); - $this->assertEquals('http://localhost:7070/events/event_id.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', - $request->getUri()); - } - - - - + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('my_event', $body['event']); + $this->assertEquals('user', $body['entityType']); + $this->assertEquals('uid', $body['entityId']); + $this->assertEquals(1, $body['properties']['prop1']); + $this->assertEquals('value2', $body['properties']['prop2']); + $this->assertEquals(array(1,2,3), $body['properties']['prop3']); + $this->assertEquals(true, $body['properties']['prop4']); + $this->assertEquals(array('a','b','c'), $body['properties']['prop5']); + $this->assertEquals(4.56, $body['properties']['prop6']); + $this->assertEquals('2004-12-13T21:39:45.618-07:00', $body['eventTime']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri()); + } + + public function testGetEvent() + { + $this->eventClient->getEvent('event_id'); + $result=array_shift($this->container); + /** @var Request $request */ + $request=$result['request']; + $body=json_decode($request->getBody(), true); + + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals( + 'http://localhost:7070/events/event_id.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', + $request->getUri() + ); + } } -?> - http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/f1e33f3a/tests/Unit/ExporterTest.php ---------------------------------------------------------------------- diff --git a/tests/Unit/ExporterTest.php b/tests/Unit/ExporterTest.php index fb2377b..7f3690a 100644 --- a/tests/Unit/ExporterTest.php +++ b/tests/Unit/ExporterTest.php @@ -1,11 +1,26 @@ <?php -namespace predictionio\tests\Unit; +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +namespace predictionio\tests\Unit; use predictionio\Exporter; -class TestExporter { +class TestExporter +{ use Exporter { jsonEncode as traitJsonEncode; } @@ -13,31 +28,37 @@ class TestExporter { public $json; public $data; - public function __construct() { + public function __construct() + { $this->json = []; $this->data = []; } - public function jsonEncode($data) { + public function jsonEncode($data) + { $this->data[] = $data; return $this->traitJsonEncode($data); } - public function export($json) { + public function export($json) + { $this->json[] = $json; } } -class ExporterTest extends \PHPUnit_Framework_TestCase { +class ExporterTest extends \PHPUnit_Framework_TestCase +{ /** @var TestExporter $exporter */ private $exporter; - protected function setUp() { + protected function setUp() + { $this->exporter = new TestExporter(); } - public function testTimeIsNow() { + public function testTimeIsNow() + { $time = new \DateTime(); $this->exporter->createEvent('event', 'entity-type', 'entity-id'); @@ -56,7 +77,8 @@ class ExporterTest extends \PHPUnit_Framework_TestCase { $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); } - public function testTimeIsString() { + public function testTimeIsString() + { $time = new \DateTime('2015-04-01'); $this->exporter->createEvent('event', 'entity-type', 'entity-id', null, null, null, '2015-04-01'); @@ -75,7 +97,8 @@ class ExporterTest extends \PHPUnit_Framework_TestCase { $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); } - public function testTimeIsDateTime() { + public function testTimeIsDateTime() + { $time = new \DateTime('2015-04-01'); $this->exporter->createEvent('event', 'entity-type', 'entity-id', null, null, null, $time); @@ -94,11 +117,19 @@ class ExporterTest extends \PHPUnit_Framework_TestCase { $this->assertTrue(preg_match($pattern, $json) === 1, 'json'); } - public function testOptionalFields() { + public function testOptionalFields() + { $time = new \DateTime('2015-04-01'); - $this->exporter->createEvent('event', 'entity-type', 'entity-id', - 'target-entity-type', 'target-entity-id', ['property' => true], $time); + $this->exporter->createEvent( + 'event', + 'entity-type', + 'entity-id', + 'target-entity-type', + 'target-entity-id', + ['property' => true], + $time + ); $this->assertEquals(1, count($this->exporter->data)); $data = $this->exporter->data[0]; @@ -116,5 +147,4 @@ class ExporterTest extends \PHPUnit_Framework_TestCase { $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'); } - -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/f1e33f3a/tests/Unit/FileExporterTest.php ---------------------------------------------------------------------- diff --git a/tests/Unit/FileExporterTest.php b/tests/Unit/FileExporterTest.php index f23affa..f7f43cd 100644 --- a/tests/Unit/FileExporterTest.php +++ b/tests/Unit/FileExporterTest.php @@ -1,12 +1,26 @@ <?php -namespace predictionio\tests\Unit; +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +namespace predictionio\tests\Unit; use predictionio\FileExporter; -class FileExporterTest extends \PHPUnit_Framework_TestCase { - +class FileExporterTest extends \PHPUnit_Framework_TestCase +{ public function setUp() { register_shutdown_function(function () { @@ -16,12 +30,27 @@ class FileExporterTest extends \PHPUnit_Framework_TestCase { }); } - public function testExporter() { + public function testExporter() + { $exporter = new FileExporter('temp.file'); - $exporter->createEvent('event-1', 'entity-type-1', 'entity-id-1', - null, null, null, '2015-04-01'); - $exporter->createEvent('event-2', 'entity-type-2', 'entity-id-2', - 'target-entity-type-2', 'target-entity-id-2', ['property' => 'blue'], '2015-04-01'); + $exporter->createEvent( + 'event-1', + 'entity-type-1', + 'entity-id-1', + null, + null, + null, + '2015-04-01' + ); + $exporter->createEvent( + 'event-2', + 'entity-type-2', + 'entity-id-2', + 'target-entity-type-2', + 'target-entity-id-2', + ['property' => 'blue'], + '2015-04-01' + ); $exporter->close(); $exported = file_get_contents('temp.file'); @@ -37,4 +66,4 @@ EOS; $this->assertEquals($expected, $exported); } -} \ No newline at end of file +}