diff --git a/Model/Item.php b/Model/Item.php index ac06695..b1c5661 100644 --- a/Model/Item.php +++ b/Model/Item.php @@ -290,6 +290,14 @@ public function addMetadata( $this->metadata[$key] = $value; } + /** + * @param string $key + */ + public function deleteMetadata(string $key) + { + unset($this->metadata[$key]); + } + /** * Get IndexedMetadata. * @@ -323,6 +331,14 @@ public function addIndexedMetadata( $this->indexedMetadata[$key] = $value; } + /** + * @param string $key + */ + public function deleteIndexedMetadata(string $key) + { + unset($this->indexedMetadata[$key]); + } + /** * Get SearchableMetadata. * @@ -356,6 +372,14 @@ public function addSearchableMetadata( $this->searchableMetadata[$key] = $value; } + /** + * @param string $key + */ + public function deleteSearchableMetadata(string $key) + { + unset($this->searchableMetadata[$key]); + } + /** * Get ExactMatchingMetadata. * @@ -389,6 +413,14 @@ public function addExactMatchingMetadata( $this->exactMatchingMetadata[$key] = $value; } + /** + * @param string $key + */ + public function deleteExactMatchingMetadata(string $key) + { + unset($this->exactMatchingMetadata[$key]); + } + /** * Get all metadata. * diff --git a/Tests/App/AppRepositoryTest.php b/Tests/App/AppRepositoryTest.php index a4c1d2a..8ff327f 100644 --- a/Tests/App/AppRepositoryTest.php +++ b/Tests/App/AppRepositoryTest.php @@ -17,6 +17,8 @@ use Apisearch\App\AppRepository; use Apisearch\Config\Config; +use Apisearch\Exception\ResourceExistsException; +use Apisearch\Exception\ResourceNotAvailableException; use Apisearch\Model\AppUUID; use Apisearch\Model\IndexUUID; use Apisearch\Model\Token; @@ -49,14 +51,13 @@ public function testCreateIndex() /** * Test create index already created. - * - * @expectedException \Apisearch\Exception\ResourceExistsException */ public function testCreateIndexAlreadyCreated() { $inMemoryAppRepository = $this->getRepository(); $inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx'))); $inMemoryAppRepository->createIndex(IndexUUID::createById('yyy'), new Config()); + $this->expectException(ResourceExistsException::class); $inMemoryAppRepository->createIndex(IndexUUID::createById('yyy'), new Config()); } @@ -75,8 +76,6 @@ public function testDeleteIndex() /** * Test delete index. - * - * @expectedException \Apisearch\Exception\ResourceNotAvailableException */ public function testDeleteIndexAlreadyDeleted() { @@ -84,30 +83,29 @@ public function testDeleteIndexAlreadyDeleted() $inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx'))); $inMemoryAppRepository->createIndex(IndexUUID::createById('yyy'), new Config()); $inMemoryAppRepository->deleteIndex(IndexUUID::createById('yyy')); + $this->expectException(ResourceNotAvailableException::class); $inMemoryAppRepository->deleteIndex(IndexUUID::createById('yyy')); } /** * Test delete non existing index. - * - * @expectedException \Apisearch\Exception\ResourceNotAvailableException */ public function testDeleteNotExistingIndex() { $inMemoryAppRepository = $this->getRepository(); $inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx'))); + $this->expectException(ResourceNotAvailableException::class); $inMemoryAppRepository->resetIndex(IndexUUID::createById('yyy')); } /** * Test configure non existing index. - * - * @expectedException \Apisearch\Exception\ResourceNotAvailableException */ public function testConfigureNotExistingIndex() { $inMemoryAppRepository = $this->getRepository(); $inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx'))); + $this->expectException(ResourceNotAvailableException::class); $inMemoryAppRepository->configureIndex(IndexUUID::createById('yyy'), Config::createFromArray([])); } diff --git a/Tests/Config/SynonymReaderTest.php b/Tests/Config/SynonymReaderTest.php index 9052e9b..5816410 100644 --- a/Tests/Config/SynonymReaderTest.php +++ b/Tests/Config/SynonymReaderTest.php @@ -16,6 +16,7 @@ namespace Apisearch\Tests\Config; use Apisearch\Config\SynonymReader; +use Apisearch\Exception\SynonymsException; use PHPUnit\Framework\TestCase; /** @@ -25,12 +26,11 @@ class SynonymReaderTest extends TestCase { /** * Test non existing file. - * - * @expectedException \Apisearch\Exception\SynonymsException */ public function testNonExistingFile() { $synonymReader = new SynonymReader(); + $this->expectException(SynonymsException::class); $synonymReader->readSynonymsFromFile(__DIR__.'/nonexistingfile276892.csv'); } diff --git a/Tests/Exporter/ExporterCollectionTest.php b/Tests/Exporter/ExporterCollectionTest.php index 05ca7c7..f88312a 100644 --- a/Tests/Exporter/ExporterCollectionTest.php +++ b/Tests/Exporter/ExporterCollectionTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Exporter; +use Apisearch\Exception\ExporterFormatNotImplementedException; use Apisearch\Exporter\CSVExporter; use Apisearch\Exporter\ExporterCollection; use Apisearch\Exporter\JSONExporter; @@ -41,14 +42,13 @@ public function testCollection() /** * Test collection with exception. - * - * @expectedException \Apisearch\Exception\ExporterFormatNotImplementedException */ public function testCollectionException() { $exporterCollection = new ExporterCollection(); $exporterCollection->addExporter(new JSONExporter()); $exporterCollection->addExporter(new CSVExporter()); + $this->expectException(ExporterFormatNotImplementedException::class); $exporterCollection->getExporterByName('xml'); } } diff --git a/Tests/Model/AppUUIDTest.php b/Tests/Model/AppUUIDTest.php index eebe6db..50a50a6 100644 --- a/Tests/Model/AppUUIDTest.php +++ b/Tests/Model/AppUUIDTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\AppUUID; use PHPUnit\Framework\TestCase; @@ -27,11 +28,10 @@ class AppUUIDTest extends TestCase * Test creation with bad data. * * @dataProvider dataEmptyCreation - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testEmptyCreation(array $data): void { + $this->expectException(InvalidFormatException::class); AppUUID::createFromArray($data); } diff --git a/Tests/Model/IndexTest.php b/Tests/Model/IndexTest.php index 647fc86..50739f6 100644 --- a/Tests/Model/IndexTest.php +++ b/Tests/Model/IndexTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\AppUUID; use Apisearch\Model\Index; use Apisearch\Model\IndexUUID; @@ -29,11 +30,10 @@ class IndexTest extends TestCase * Test creation with bad data. * * @dataProvider dataEmptyCreation - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testEmptyCreation(array $data): void { + $this->expectException(InvalidFormatException::class); Index::createFromArray($data); } diff --git a/Tests/Model/IndexUUIDTest.php b/Tests/Model/IndexUUIDTest.php index 7882c19..44f7dda 100644 --- a/Tests/Model/IndexUUIDTest.php +++ b/Tests/Model/IndexUUIDTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\IndexUUID; use PHPUnit\Framework\TestCase; @@ -27,11 +28,10 @@ class IndexUUIDTest extends TestCase * Test creation with bad data. * * @dataProvider dataEmptyCreation - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testEmptyCreation(array $data): void { + $this->expectException(InvalidFormatException::class); IndexUUID::createFromArray($data); } diff --git a/Tests/Model/ItemTest.php b/Tests/Model/ItemTest.php index ae06daf..ede8fa6 100644 --- a/Tests/Model/ItemTest.php +++ b/Tests/Model/ItemTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\AppUUID; use Apisearch\Model\Coordinate; use Apisearch\Model\IndexUUID; @@ -136,11 +137,10 @@ public function testCreateEmptyValues() * Test create item with bad formatted UUID. * * @dataProvider dataItemBadFormattedUUID - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testItemBadFormattedUUID($data) { + $this->expectException(InvalidFormatException::class); Item::createFromArray($data); } @@ -167,11 +167,10 @@ public function dataItemBadFormattedUUID() * Test create Coordinate with bad formatted. * * @dataProvider dataCoordinateBadFormattedUUID - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testCoordinateFormattedUUID($data) { + $this->expectException(InvalidFormatException::class); Item::createFromArray(array_merge(['uuid' => [ 'id' => '1', 'type' => 'product', @@ -466,4 +465,25 @@ public function testIndexAndAppUUID() $item->getAppUUID() ); } + + /** + * Test delete methods. + */ + public function testDeleteMethods() + { + $item = Item::createFromArray([ + 'uuid' => ['id' => 'A', 'type' => 'B'], + 'metadata' => ['A' => 1], + 'indexed_metadata' => ['B' => 2], + 'searchable_metadata' => ['C' => 3], + 'exact_matching_metadata' => ['D' => 4], + ]); + + $item->deleteMetadata('A'); + $item->deleteIndexedMetadata('B'); + $item->deleteSearchableMetadata('C'); + $item->deleteExactMatchingMetadata('D'); + + $this->assertEquals(['uuid' => ['id' => 'A', 'type' => 'B']], $item->toArray()); + } } diff --git a/Tests/Model/ItemUUIDTest.php b/Tests/Model/ItemUUIDTest.php index cde1ddc..b70a20d 100644 --- a/Tests/Model/ItemUUIDTest.php +++ b/Tests/Model/ItemUUIDTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\ItemUUID; use PHPUnit\Framework\TestCase; @@ -56,11 +57,10 @@ public function testCreateByComposedUUID() * Test create by composed UUID with exception. * * @dataProvider dataCreateByComposedUUIDException - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testCreateByComposedUUIDException(string $composedUUID) { + $this->expectException(InvalidFormatException::class); ItemUUID::createByComposedUUID($composedUUID); } @@ -96,11 +96,10 @@ public function testCreateFromArray() * Test create from array with exception. * * @dataProvider dataCreateFromArrayException - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testCreateFromArrayException(array $composedUUID) { + $this->expectException(InvalidFormatException::class); ItemUUID::createFromArray($composedUUID); } diff --git a/Tests/Model/TokenUUIDTest.php b/Tests/Model/TokenUUIDTest.php index c04adae..3e313f7 100644 --- a/Tests/Model/TokenUUIDTest.php +++ b/Tests/Model/TokenUUIDTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\TokenUUID; use PHPUnit\Framework\TestCase; @@ -27,11 +28,10 @@ class TokenUUIDTest extends TestCase * Test creation with bad data. * * @dataProvider dataEmptyCreation - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testEmptyCreation(array $data): void { + $this->expectException(InvalidFormatException::class); TokenUUID::createFromArray($data); } diff --git a/Tests/Model/UserTest.php b/Tests/Model/UserTest.php index 167a0a1..01772d6 100644 --- a/Tests/Model/UserTest.php +++ b/Tests/Model/UserTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Model; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\User; use PHPUnit\Framework\TestCase; @@ -54,11 +55,10 @@ public function testHttpTransport() * Test create from array with exception. * * @dataProvider dataCreateFromArrayException - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testCreateFromArrayException(array $user) { + $this->expectException(InvalidFormatException::class); User::createFromArray($user); } diff --git a/Tests/Query/AggregationTest.php b/Tests/Query/AggregationTest.php index 28a148a..d56767c 100644 --- a/Tests/Query/AggregationTest.php +++ b/Tests/Query/AggregationTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Query; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Query\Aggregation; use Apisearch\Query\Filter; use PHPUnit\Framework\TestCase; @@ -50,21 +51,19 @@ public function testCreate() /** * Test creation with bad name. - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testCreateBadName() { + $this->expectException(InvalidFormatException::class); Aggregation::createFromArray([]); } /** * Test creation with empty name. - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testCreateEmptyName() { + $this->expectException(InvalidFormatException::class); Aggregation::createFromArray([ 'name' => '', ]); diff --git a/Tests/Repository/HttpRepositoryTest.php b/Tests/Repository/HttpRepositoryTest.php index e0d0745..060ecc6 100644 --- a/Tests/Repository/HttpRepositoryTest.php +++ b/Tests/Repository/HttpRepositoryTest.php @@ -33,8 +33,6 @@ class HttpRepositoryTest extends TestCase { /** * Test add, delete and query items by UUID. - * - * @expectedException \Apisearch\Exception\ConnectionException */ public function testNullResponse() { @@ -42,13 +40,12 @@ public function testNullResponse() $client->get(Argument::cetera())->willReturn(['code' => 0, 'body' => null]); $repository = new HttpRepository($client->reveal()); $repository->setCredentials(RepositoryReference::create(AppUUID::createById('123'), IndexUUID::createById('456')), TokenUUID::createById('000')); + $this->expectException(ConnectionException::class); $repository->query(Query::createMatchAll()); } /** * Test add, delete and query items by UUID. - * - * @expectedException \Apisearch\Exception\ConnectionException */ public function testConnectionExceptionResponse() { @@ -56,6 +53,7 @@ public function testConnectionExceptionResponse() $client->get(Argument::cetera())->willThrow(ConnectionException::buildConnectExceptionByUrl('http://xxx.xx')); $repository = new HttpRepository($client->reveal()); $repository->setCredentials(RepositoryReference::create(AppUUID::createById('123'), IndexUUID::createById('456')), TokenUUID::createById('000')); + $this->expectException(ConnectionException::class); $repository->query(Query::createMatchAll()); } } diff --git a/Tests/Repository/RepositoryReferenceTest.php b/Tests/Repository/RepositoryReferenceTest.php index a390efa..0eea8bb 100644 --- a/Tests/Repository/RepositoryReferenceTest.php +++ b/Tests/Repository/RepositoryReferenceTest.php @@ -15,6 +15,7 @@ namespace Apisearch\Tests\Repository; +use Apisearch\Exception\InvalidFormatException; use Apisearch\Model\AppUUID; use Apisearch\Model\IndexUUID; use Apisearch\Repository\RepositoryReference; @@ -120,11 +121,10 @@ public function testChangeIndex() /** * Test bad ids. - * - * @expectedException \Apisearch\Exception\InvalidFormatException */ public function testBadIds() { + $this->expectException(InvalidFormatException::class); RepositoryReference::create( AppUUID::createById('1_2_3'), IndexUUID::createById('4_5_6') diff --git a/Tests/Repository/RepositoryTest.php b/Tests/Repository/RepositoryTest.php index db12163..4a2f88a 100644 --- a/Tests/Repository/RepositoryTest.php +++ b/Tests/Repository/RepositoryTest.php @@ -157,13 +157,12 @@ public function testQueryMultiple() * Test invalid queries. * * @dataProvider dataInvalidQueries - * - * @expectedException \Exception */ public function testInvalidQueries(Query $query) { $repository = $this->getRepository(); $repository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx'), IndexUUID::createById('xxx'))); + $this->expectException(\Exception::class); $repository->query($query); }