From 5f4f7c4bc4d933901e9321f43128ee7b6488af5a Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 23 Feb 2019 10:35:27 -0500 Subject: [PATCH] Test CollectionsService::addItem --- src/Objects/CollectionsService.php | 16 +- test/Objects/CollectionsServiceDbTest.php | 431 ++++++++++++++++++++++ test/Objects/CollectionsServiceTest.php | 16 +- test/TestConfig/SQLiteTestCase.php | 4 +- 4 files changed, 454 insertions(+), 13 deletions(-) create mode 100644 test/Objects/CollectionsServiceDbTest.php diff --git a/src/Objects/CollectionsService.php b/src/Objects/CollectionsService.php index b3fba63..6fefaa6 100644 --- a/src/Objects/CollectionsService.php +++ b/src/Objects/CollectionsService.php @@ -271,7 +271,9 @@ class CollectionsService $items = new ActivityPubObject( $this->dateTimeProvider->getTime( 'collections-service.add' ) ); - $itemsField = Field::withObject( $collection, $itemsFieldName, $items ); + $itemsField = Field::withObject( + $collection, $itemsFieldName, $items, $this->dateTimeProvider->getTime( 'collections-service.add' ) + ); $this->entityManager->persist( $itemsField ); $this->entityManager->persist( $items ); $this->entityManager->persist( $collection ); @@ -286,9 +288,13 @@ class CollectionsService $itemCount = count( $items->getFields() ); if ( is_array( $item ) ) { $item = $this->objectsService->persist( $item, 'collections-service.add' ); - $newItemField = Field::withObject( $items, $itemCount, $item ); + $newItemField = Field::withObject( + $items, $itemCount, $item, $this->dateTimeProvider->getTime( 'collections-service.add' ) + ); } else if ( is_string( $item ) ) { - $newItemField = Field::withValue( $items, $itemCount, $item ); + $newItemField = Field::withValue( + $items, $itemCount, $item, $this->dateTimeProvider->getTime( 'collections-service.add' ) + ); } $this->entityManager->persist( $newItemField ); $this->entityManager->persist( $items ); @@ -298,7 +304,9 @@ class CollectionsService } else { $totalItemsField = $collection->getField( 'totalItems' ); if ( !$totalItemsField ) { - $totalItemsField = Field::withValue( $collection, 'totalItems', strval( $itemCount ) ); + $totalItemsField = Field::withValue( + $collection, 'totalItems', strval( $itemCount ), $this->dateTimeProvider->getTime( 'collections-service.add' ) + ); } } diff --git a/test/Objects/CollectionsServiceDbTest.php b/test/Objects/CollectionsServiceDbTest.php new file mode 100644 index 0000000..1becfa2 --- /dev/null +++ b/test/Objects/CollectionsServiceDbTest.php @@ -0,0 +1,431 @@ +setNamingStrategy( $namingStrategy ); + $dbParams = array( + 'driver' => 'pdo_sqlite', + 'path' => $this->getDbPath(), + ); + $this->entityManager = EntityManager::create( $dbParams, $dbConfig ); + $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" ), + ) ); + $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 ) + { + return $this->dateTimeProvider + ->getTime( $context ) + ->format( "Y-m-d H:i:s" ); + } + + public function testAddItem() + { + $testCases = 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( + '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( + '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, + ), + ), + ), + ), + ); + foreach ( $testCases as $testCase ) + { + self::setUp(); + $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]"); + self::tearDown(); + } + } + + /** + * Returns the test dataset. + * + * @return PHPUnit_Extensions_Database_DataSet_IDataSet + */ + protected function getDataSet() + { + return new ArrayDataSet( array( 'objects' => array(), 'fields' => array() ) ); + } +} \ No newline at end of file diff --git a/test/Objects/CollectionsServiceTest.php b/test/Objects/CollectionsServiceTest.php index 7b799ef..f782734 100644 --- a/test/Objects/CollectionsServiceTest.php +++ b/test/Objects/CollectionsServiceTest.php @@ -6,7 +6,7 @@ use ActivityPub\Auth\AuthService; use ActivityPub\Objects\CollectionsService; use ActivityPub\Objects\ContextProvider; use ActivityPub\Objects\ObjectsService; -use ActivityPub\Test\TestConfig\APTestCase; +use ActivityPub\Test\TestConfig\SQLiteTestCase; use ActivityPub\Test\TestUtils\TestActivityPubObject; use ActivityPub\Utils\SimpleDateTimeProvider; use Doctrine\ORM\EntityManager; @@ -15,7 +15,7 @@ use GuzzleHttp\Psr7\Response as Psr7Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -class CollectionsServiceTest extends APTestCase +class CollectionsServiceTest extends SQLiteTestCase { /** * @var CollectionsService @@ -24,6 +24,7 @@ class CollectionsServiceTest extends APTestCase public function setUp() { + parent::setUp(); $authService = new AuthService(); $contextProvider = new ContextProvider(); $httpClient = $this->getMock( Client::class ); @@ -49,6 +50,11 @@ class CollectionsServiceTest extends APTestCase ); } + protected function getDataSet() + { + return new ArrayDataSet( array( 'objects' => array(), 'fields' => array() ) ); + } + public function testCollectionPaging() { $testCases = array( @@ -512,11 +518,5 @@ class CollectionsServiceTest extends APTestCase ); } } - - public function testAddItem() - { - // TODO implement me - $this->assertTrue( false ); - } } diff --git a/test/TestConfig/SQLiteTestCase.php b/test/TestConfig/SQLiteTestCase.php index 93c5be9..371e495 100644 --- a/test/TestConfig/SQLiteTestCase.php +++ b/test/TestConfig/SQLiteTestCase.php @@ -53,7 +53,9 @@ abstract class SQLiteTestCase extends APTestCase protected function tearDown() { parent::tearDown(); - unlink( $this->getDbPath() ); + if ( file_exists( $this->getDbPath() ) ) { + unlink( $this->getDbPath() ); + } unset( $this->conn ); unset( $this->pdo ); }