setNamingStrategy( $namingStrategy ); $dbParams = array( 'driver' => 'pdo_sqlite', 'path' => $this->getDbPath(), ); $this->dateTimeProvider = new TestDateTimeProvider( array( 'objects-service.create' => new DateTime( "12:00" ), 'objects-service.update' => new DateTime( "12:01" ), 'collections-service.add' => new DateTime( "12:03" ), 'collections-service.remove' => new DateTime( "12:04" ), ) ); $this->entityManager = EntityManager::create( $dbParams, $dbConfig ); $this->httpClient = $this->getMock( Client::class ); $this->httpClient->method( 'send' ) ->willReturn( new Response( 404 ) ); $this->objectsService = new ObjectsService( $this->entityManager, $this->dateTimeProvider, $this->httpClient ); $this->collectionsService = new CollectionsService(4, new AuthService(), new ContextProvider(), $this->httpClient, $this->dateTimeProvider, $this->entityManager, $this->objectsService); } private function getTime( $context ) { if ( ! $this->dateTimeProvider ) { $this->dateTimeProvider = new TestDateTimeProvider( array( 'objects-service.create' => new DateTime( "12:00" ), 'objects-service.update' => new DateTime( "12:01" ), 'collections-service.add' => new DateTime( "12:03" ), 'collections-service.remove' => new DateTime( "12:04" ), ) ); } return $this->dateTimeProvider ->getTime( $context ) ->format( "Y-m-d H:i:s" ); } public function provideTestAddItem() { return array( array( array( 'id' => 'basicTest', 'collection' => array( 'id' => 'https://example.com/collections/1', 'type' => 'Collection', 'items' => array(), ), 'item' => array( 'id' => 'https://example.com/notes/1', 'type' => 'Note', ), 'expectedDataSet' => array( 'objects' => array( array( 'id' => 1, 'created' => $this->getTime('objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), array( 'id' => 2, 'created' => $this->getTime('objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), array( 'id' => 3, 'created' => $this->getTime('collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), ), 'fields' => array( array( 'id' => 1, 'object_id' => 1, 'name' => 'id', 'value' => 'https://example.com/collections/1', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 2, 'object_id' => 1, 'name' => 'type', 'value' => 'Collection', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 3, 'object_id' => 1, 'name' => 'items', 'value' => null, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => 2, ), array( 'id' => 4, 'object_id' => 3, 'name' => 'id', 'value' => 'https://example.com/notes/1', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), array( 'id' => 5, 'object_id' => 3, 'name' => 'type', 'value' => 'Note', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), array( 'id' => 6, 'object_id' => 2, 'name' => '0', 'value' => null, 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => 3, ), array( 'id' => 7, 'object_id' => 1, 'name' => 'totalItems', 'value' => '1', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), ), ), ) ), array( array( 'id' => 'createItemsField', 'collection' => array( 'id' => 'https://example.com/collections/1', 'type' => 'Collection', ), 'item' => array( 'id' => 'https://example.com/notes/1', 'type' => 'Note', ), 'expectedDataSet' => array( 'objects' => array( array( 'id' => 1, 'created' => $this->getTime('objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), array( 'id' => 2, 'created' => $this->getTime('collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), array( 'id' => 3, 'created' => $this->getTime('collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), ), 'fields' => array( array( 'id' => 1, 'object_id' => 1, 'name' => 'id', 'value' => 'https://example.com/collections/1', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 2, 'object_id' => 1, 'name' => 'type', 'value' => 'Collection', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 3, 'object_id' => 1, 'name' => 'items', 'value' => null, 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => 2, ), array( 'id' => 4, 'object_id' => 3, 'name' => 'id', 'value' => 'https://example.com/notes/1', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), array( 'id' => 5, 'object_id' => 3, 'name' => 'type', 'value' => 'Note', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), array( 'id' => 6, 'object_id' => 2, 'name' => '0', 'value' => null, 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => 3, ), array( 'id' => 7, 'object_id' => 1, 'name' => 'totalItems', 'value' => '1', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), ), ), ) ), array( array( 'id' => 'existingItems', 'collection' => array( 'id' => 'https://example.com/collections/1', 'type' => 'Collection', 'items' => array( array( 'id' => 'https://example.com/activities/1' ) ), ), 'item' => array( 'id' => 'https://example.com/notes/1', 'type' => 'Note', ), 'expectedDataSet' => array( 'objects' => array( array( 'id' => 1, 'created' => $this->getTime('objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), array( 'id' => 2, 'created' => $this->getTime('objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), array( 'id' => 3, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), ), array( 'id' => 4, 'created' => $this->getTime('collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), ), ), 'fields' => array( array( 'id' => 1, 'object_id' => 1, 'name' => 'id', 'value' => 'https://example.com/collections/1', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 2, 'object_id' => 1, 'name' => 'type', 'value' => 'Collection', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 3, 'object_id' => 3, 'name' => 'id', 'value' => 'https://example.com/activities/1', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 4, 'object_id' => 2, 'name' => '0', 'value' => null, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => 3, ), array( 'id' => 5, 'object_id' => 1, 'name' => 'items', 'value' => null, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => 2, ), array( 'id' => 6, 'object_id' => 4, 'name' => 'id', 'value' => 'https://example.com/notes/1', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), array( 'id' => 7, 'object_id' => 4, 'name' => 'type', 'value' => 'Note', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), array( 'id' => 8, 'object_id' => 2, 'name' => '1', 'value' => null, 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => 4, ), array( 'id' => 9, 'object_id' => 1, 'name' => 'totalItems', 'value' => '2', 'created' => $this->getTime( 'collections-service.add' ), 'lastUpdated' => $this->getTime( 'collections-service.add' ), 'targetObject_id' => null, ), ), ), ) ), ); } /** * @dataProvider provideTestAddItem */ public function testAddItem( $testCase ) { $collection = $this->objectsService->persist( $testCase['collection'] ); $this->collectionsService->addItem( $collection, $testCase['item'] ); $expectedDataSet = new ArrayDataSet( $testCase['expectedDataSet'] ); $expectedObjects = $expectedDataSet->getTable( 'objects' ); $expectedFields = $expectedDataSet->getTable( 'fields' ); $actualObjects = $this->getConnection()->createQueryTable( 'objects', 'SELECT * FROM objects' ); $actualFields = $this->getConnection()->createQueryTable( 'fields', 'SELECT * FROM fields' ); $this->assertTablesEqual( $expectedObjects, $actualObjects, "Error on test $testCase[id]"); $this->assertTablesEqual( $expectedFields, $actualFields, "Error on test $testCase[id]"); } public function provideTestRemoveItem() { return array( array( array( 'id' => 'basicRemoveTest', 'collection' => array( 'id' => 'https://example.com/collections/1', 'type' => 'Collection', 'items' => array( array( 'id' => 'https://example.com/items/1' ), array( 'id' => 'https://example.com/items/2' ), array( 'id' => 'https://example.com/items/3' ), ), ), 'itemIdToRemove' => 'https://example.com/items/2', 'expectedDataSet' => array( 'objects' => array( array( 'id' => 1, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.remove' ), ), array( 'id' => 2, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.remove' ), ), array( 'id' => 3, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), ), array( 'id' => 4, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), ), array( 'id' => 5, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), ), ), 'fields' => array( array( 'id' => 1, 'object_id' => 1, 'name' => 'id', 'value' => 'https://example.com/collections/1', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 2, 'object_id' => 1, 'name' => 'type', 'value' => 'Collection', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 3, 'object_id' => 3, 'name' => 'id', 'value' => 'https://example.com/items/1', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 4, 'object_id' => 2, 'name' => 0, 'value' => null, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => 3, ), array( 'id' => 5, 'object_id' => 4, 'name' => 'id', 'value' => 'https://example.com/items/2', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 7, 'object_id' => 5, 'name' => 'id', 'value' => 'https://example.com/items/3', 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => null, ), array( 'id' => 8, 'object_id' => 2, 'name' => 1, 'value' => null, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'collections-service.remove' ), 'targetObject_id' => 5, ), array( 'id' => 9, 'object_id' => 1, 'name' => 'items', 'value' => null, 'created' => $this->getTime( 'objects-service.create' ), 'lastUpdated' => $this->getTime( 'objects-service.create' ), 'targetObject_id' => 2, ), ), ), ) ), ); } /** * @dataProvider provideTestRemoveItem */ public function testRemoveItem( $testCase ) { $collection = $this->objectsService->persist( $testCase['collection'] ); $this->collectionsService->removeItem( $collection, $testCase['itemIdToRemove'] ); $expectedDataSet = new ArrayDataSet( $testCase['expectedDataSet'] ); $expectedObjects = $expectedDataSet->getTable( 'objects' ); $expectedFields = $expectedDataSet->getTable( 'fields' ); $actualObjects = $this->getConnection()->createQueryTable( 'objects', 'SELECT * FROM objects' ); $actualFields = $this->getConnection()->createQueryTable( 'fields', 'SELECT * FROM fields' ); $this->assertTablesEqual( $expectedObjects, $actualObjects, "Error on test $testCase[id]"); $this->assertTablesEqual( $expectedFields, $actualFields, "Error on test $testCase[id]"); $this->tearDown(); } /** * Returns the test dataset. * * @return PHPUnit_Extensions_Database_DataSet_IDataSet */ protected function getDataSet() { return new ArrayDataSet( array( 'objects' => array(), 'fields' => array() ) ); } }