Skip to content

Commit 04e85d8

Browse files
committed
Items properties can be deleted properly
- Tests expectException with method
1 parent 635dd99 commit 04e85d8

15 files changed

+87
-42
lines changed

Model/Item.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ public function addMetadata(
290290
$this->metadata[$key] = $value;
291291
}
292292

293+
/**
294+
* @param string $key
295+
*/
296+
public function deleteMetadata(string $key)
297+
{
298+
unset($this->metadata[$key]);
299+
}
300+
293301
/**
294302
* Get IndexedMetadata.
295303
*
@@ -323,6 +331,14 @@ public function addIndexedMetadata(
323331
$this->indexedMetadata[$key] = $value;
324332
}
325333

334+
/**
335+
* @param string $key
336+
*/
337+
public function deleteIndexedMetadata(string $key)
338+
{
339+
unset($this->indexedMetadata[$key]);
340+
}
341+
326342
/**
327343
* Get SearchableMetadata.
328344
*
@@ -356,6 +372,14 @@ public function addSearchableMetadata(
356372
$this->searchableMetadata[$key] = $value;
357373
}
358374

375+
/**
376+
* @param string $key
377+
*/
378+
public function deleteSearchableMetadata(string $key)
379+
{
380+
unset($this->searchableMetadata[$key]);
381+
}
382+
359383
/**
360384
* Get ExactMatchingMetadata.
361385
*
@@ -389,6 +413,14 @@ public function addExactMatchingMetadata(
389413
$this->exactMatchingMetadata[$key] = $value;
390414
}
391415

416+
/**
417+
* @param string $key
418+
*/
419+
public function deleteExactMatchingMetadata(string $key)
420+
{
421+
unset($this->exactMatchingMetadata[$key]);
422+
}
423+
392424
/**
393425
* Get all metadata.
394426
*

Tests/App/AppRepositoryTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use Apisearch\App\AppRepository;
1919
use Apisearch\Config\Config;
20+
use Apisearch\Exception\ResourceExistsException;
21+
use Apisearch\Exception\ResourceNotAvailableException;
2022
use Apisearch\Model\AppUUID;
2123
use Apisearch\Model\IndexUUID;
2224
use Apisearch\Model\Token;
@@ -49,14 +51,13 @@ public function testCreateIndex()
4951

5052
/**
5153
* Test create index already created.
52-
*
53-
* @expectedException \Apisearch\Exception\ResourceExistsException
5454
*/
5555
public function testCreateIndexAlreadyCreated()
5656
{
5757
$inMemoryAppRepository = $this->getRepository();
5858
$inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx')));
5959
$inMemoryAppRepository->createIndex(IndexUUID::createById('yyy'), new Config());
60+
$this->expectException(ResourceExistsException::class);
6061
$inMemoryAppRepository->createIndex(IndexUUID::createById('yyy'), new Config());
6162
}
6263

@@ -75,39 +76,36 @@ public function testDeleteIndex()
7576

7677
/**
7778
* Test delete index.
78-
*
79-
* @expectedException \Apisearch\Exception\ResourceNotAvailableException
8079
*/
8180
public function testDeleteIndexAlreadyDeleted()
8281
{
8382
$inMemoryAppRepository = $this->getRepository();
8483
$inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx')));
8584
$inMemoryAppRepository->createIndex(IndexUUID::createById('yyy'), new Config());
8685
$inMemoryAppRepository->deleteIndex(IndexUUID::createById('yyy'));
86+
$this->expectException(ResourceNotAvailableException::class);
8787
$inMemoryAppRepository->deleteIndex(IndexUUID::createById('yyy'));
8888
}
8989

9090
/**
9191
* Test delete non existing index.
92-
*
93-
* @expectedException \Apisearch\Exception\ResourceNotAvailableException
9492
*/
9593
public function testDeleteNotExistingIndex()
9694
{
9795
$inMemoryAppRepository = $this->getRepository();
9896
$inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx')));
97+
$this->expectException(ResourceNotAvailableException::class);
9998
$inMemoryAppRepository->resetIndex(IndexUUID::createById('yyy'));
10099
}
101100

102101
/**
103102
* Test configure non existing index.
104-
*
105-
* @expectedException \Apisearch\Exception\ResourceNotAvailableException
106103
*/
107104
public function testConfigureNotExistingIndex()
108105
{
109106
$inMemoryAppRepository = $this->getRepository();
110107
$inMemoryAppRepository->setRepositoryReference(RepositoryReference::create(AppUUID::createById('xxx')));
108+
$this->expectException(ResourceNotAvailableException::class);
111109
$inMemoryAppRepository->configureIndex(IndexUUID::createById('yyy'), Config::createFromArray([]));
112110
}
113111

Tests/Config/SynonymReaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Apisearch\Tests\Config;
1717

1818
use Apisearch\Config\SynonymReader;
19+
use Apisearch\Exception\SynonymsException;
1920
use PHPUnit\Framework\TestCase;
2021

2122
/**
@@ -25,12 +26,11 @@ class SynonymReaderTest extends TestCase
2526
{
2627
/**
2728
* Test non existing file.
28-
*
29-
* @expectedException \Apisearch\Exception\SynonymsException
3029
*/
3130
public function testNonExistingFile()
3231
{
3332
$synonymReader = new SynonymReader();
33+
$this->expectException(SynonymsException::class);
3434
$synonymReader->readSynonymsFromFile(__DIR__.'/nonexistingfile276892.csv');
3535
}
3636

Tests/Exporter/ExporterCollectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Exporter;
1717

18+
use Apisearch\Exception\ExporterFormatNotImplementedException;
1819
use Apisearch\Exporter\CSVExporter;
1920
use Apisearch\Exporter\ExporterCollection;
2021
use Apisearch\Exporter\JSONExporter;
@@ -41,14 +42,13 @@ public function testCollection()
4142

4243
/**
4344
* Test collection with exception.
44-
*
45-
* @expectedException \Apisearch\Exception\ExporterFormatNotImplementedException
4645
*/
4746
public function testCollectionException()
4847
{
4948
$exporterCollection = new ExporterCollection();
5049
$exporterCollection->addExporter(new JSONExporter());
5150
$exporterCollection->addExporter(new CSVExporter());
51+
$this->expectException(ExporterFormatNotImplementedException::class);
5252
$exporterCollection->getExporterByName('xml');
5353
}
5454
}

Tests/Model/AppUUIDTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\AppUUID;
1920
use PHPUnit\Framework\TestCase;
2021

@@ -27,11 +28,10 @@ class AppUUIDTest extends TestCase
2728
* Test creation with bad data.
2829
*
2930
* @dataProvider dataEmptyCreation
30-
*
31-
* @expectedException \Apisearch\Exception\InvalidFormatException
3231
*/
3332
public function testEmptyCreation(array $data): void
3433
{
34+
$this->expectException(InvalidFormatException::class);
3535
AppUUID::createFromArray($data);
3636
}
3737

Tests/Model/IndexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\AppUUID;
1920
use Apisearch\Model\Index;
2021
use Apisearch\Model\IndexUUID;
@@ -29,11 +30,10 @@ class IndexTest extends TestCase
2930
* Test creation with bad data.
3031
*
3132
* @dataProvider dataEmptyCreation
32-
*
33-
* @expectedException \Apisearch\Exception\InvalidFormatException
3433
*/
3534
public function testEmptyCreation(array $data): void
3635
{
36+
$this->expectException(InvalidFormatException::class);
3737
Index::createFromArray($data);
3838
}
3939

Tests/Model/IndexUUIDTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\IndexUUID;
1920
use PHPUnit\Framework\TestCase;
2021

@@ -27,11 +28,10 @@ class IndexUUIDTest extends TestCase
2728
* Test creation with bad data.
2829
*
2930
* @dataProvider dataEmptyCreation
30-
*
31-
* @expectedException \Apisearch\Exception\InvalidFormatException
3231
*/
3332
public function testEmptyCreation(array $data): void
3433
{
34+
$this->expectException(InvalidFormatException::class);
3535
IndexUUID::createFromArray($data);
3636
}
3737

Tests/Model/ItemTest.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\AppUUID;
1920
use Apisearch\Model\Coordinate;
2021
use Apisearch\Model\IndexUUID;
@@ -136,11 +137,10 @@ public function testCreateEmptyValues()
136137
* Test create item with bad formatted UUID.
137138
*
138139
* @dataProvider dataItemBadFormattedUUID
139-
*
140-
* @expectedException \Apisearch\Exception\InvalidFormatException
141140
*/
142141
public function testItemBadFormattedUUID($data)
143142
{
143+
$this->expectException(InvalidFormatException::class);
144144
Item::createFromArray($data);
145145
}
146146

@@ -167,11 +167,10 @@ public function dataItemBadFormattedUUID()
167167
* Test create Coordinate with bad formatted.
168168
*
169169
* @dataProvider dataCoordinateBadFormattedUUID
170-
*
171-
* @expectedException \Apisearch\Exception\InvalidFormatException
172170
*/
173171
public function testCoordinateFormattedUUID($data)
174172
{
173+
$this->expectException(InvalidFormatException::class);
175174
Item::createFromArray(array_merge(['uuid' => [
176175
'id' => '1',
177176
'type' => 'product',
@@ -466,4 +465,25 @@ public function testIndexAndAppUUID()
466465
$item->getAppUUID()
467466
);
468467
}
468+
469+
/**
470+
* Test delete methods.
471+
*/
472+
public function testDeleteMethods()
473+
{
474+
$item = Item::createFromArray([
475+
'uuid' => ['id' => 'A', 'type' => 'B'],
476+
'metadata' => ['A' => 1],
477+
'indexed_metadata' => ['B' => 2],
478+
'searchable_metadata' => ['C' => 3],
479+
'exact_matching_metadata' => ['D' => 4],
480+
]);
481+
482+
$item->deleteMetadata('A');
483+
$item->deleteIndexedMetadata('B');
484+
$item->deleteSearchableMetadata('C');
485+
$item->deleteExactMatchingMetadata('D');
486+
487+
$this->assertEquals(['uuid' => ['id' => 'A', 'type' => 'B']], $item->toArray());
488+
}
469489
}

Tests/Model/ItemUUIDTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\ItemUUID;
1920
use PHPUnit\Framework\TestCase;
2021

@@ -56,11 +57,10 @@ public function testCreateByComposedUUID()
5657
* Test create by composed UUID with exception.
5758
*
5859
* @dataProvider dataCreateByComposedUUIDException
59-
*
60-
* @expectedException \Apisearch\Exception\InvalidFormatException
6160
*/
6261
public function testCreateByComposedUUIDException(string $composedUUID)
6362
{
63+
$this->expectException(InvalidFormatException::class);
6464
ItemUUID::createByComposedUUID($composedUUID);
6565
}
6666

@@ -96,11 +96,10 @@ public function testCreateFromArray()
9696
* Test create from array with exception.
9797
*
9898
* @dataProvider dataCreateFromArrayException
99-
*
100-
* @expectedException \Apisearch\Exception\InvalidFormatException
10199
*/
102100
public function testCreateFromArrayException(array $composedUUID)
103101
{
102+
$this->expectException(InvalidFormatException::class);
104103
ItemUUID::createFromArray($composedUUID);
105104
}
106105

Tests/Model/TokenUUIDTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\TokenUUID;
1920
use PHPUnit\Framework\TestCase;
2021

@@ -27,11 +28,10 @@ class TokenUUIDTest extends TestCase
2728
* Test creation with bad data.
2829
*
2930
* @dataProvider dataEmptyCreation
30-
*
31-
* @expectedException \Apisearch\Exception\InvalidFormatException
3231
*/
3332
public function testEmptyCreation(array $data): void
3433
{
34+
$this->expectException(InvalidFormatException::class);
3535
TokenUUID::createFromArray($data);
3636
}
3737

Tests/Model/UserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\User;
1920
use PHPUnit\Framework\TestCase;
2021

@@ -54,11 +55,10 @@ public function testHttpTransport()
5455
* Test create from array with exception.
5556
*
5657
* @dataProvider dataCreateFromArrayException
57-
*
58-
* @expectedException \Apisearch\Exception\InvalidFormatException
5958
*/
6059
public function testCreateFromArrayException(array $user)
6160
{
61+
$this->expectException(InvalidFormatException::class);
6262
User::createFromArray($user);
6363
}
6464

0 commit comments

Comments
 (0)