Site du proximo, utilisé pour gérer le stock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApiFoodCacheTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. use GuzzleHttp\Exception\ServerException;
  3. use OpenFoodFacts\FilesystemTrait;
  4. use PHPUnit\Framework\TestCase;
  5. use OpenFoodFacts\Api;
  6. use OpenFoodFacts\Collection;
  7. use OpenFoodFacts\Document\FoodDocument;
  8. use OpenFoodFacts\Document;
  9. use OpenFoodFacts\Exception\{
  10. ProductNotFoundException,
  11. BadRequestException
  12. };
  13. use Monolog\Logger;
  14. use Monolog\Handler\StreamHandler;
  15. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  16. use Symfony\Component\Cache\Psr16Cache;
  17. use Symfony\Component\Console\Logger\ConsoleLogger;
  18. class ApiFoodCacheTest extends TestCase
  19. {
  20. use FilesystemTrait;
  21. /**
  22. * @var Api
  23. */
  24. private $api;
  25. protected function setUp()
  26. {
  27. @rmdir('tests/tmp');
  28. @mkdir('tests/tmp');
  29. @mkdir('tests/tmp/cache');
  30. $log = new Logger('name');
  31. $log->pushHandler(new StreamHandler('log/test.log'));
  32. $psr6Cache = new FilesystemAdapter(sprintf('testrun_%u', rand(0, 1000)), 10, 'tests/tmp/cache');
  33. $cache = new Psr16Cache($psr6Cache);
  34. $httpClient = new GuzzleHttp\Client([
  35. // "http_errors" => false, // MUST not use as it crashes error handling
  36. 'Connection' => 'close',
  37. CURLOPT_FORBID_REUSE => true,
  38. CURLOPT_FRESH_CONNECT => true,
  39. 'defaults' => [
  40. 'headers' => [
  41. 'CURLOPT_USERAGENT' => 'OFF - PHP - SDK - Unit Test',
  42. ],
  43. ],
  44. ]);
  45. $api = new Api('food', 'fr-en', $log, $httpClient, $cache);
  46. $this->assertInstanceOf(Api::class, $api);
  47. $this->api = $api;
  48. }
  49. public function testApi(): void
  50. {
  51. $prd = $this->api->getProduct('3057640385148');
  52. $this->assertInstanceOf(FoodDocument::class, $prd);
  53. $this->assertInstanceOf(Document::class, $prd);
  54. $this->assertTrue(isset($prd->product_name));
  55. $this->assertNotEmpty($prd->product_name);
  56. try {
  57. $product = $this->api->getProduct('305764038514800');
  58. $this->assertTrue(false);
  59. } catch (ProductNotFoundException $e) {
  60. $this->assertTrue(true);
  61. }
  62. try {
  63. $result = $this->api->downloadData('tests/mongodb', 'nopeFile');
  64. $this->assertTrue(false);
  65. } catch (BadRequestException $e) {
  66. $this->assertEquals($e->getMessage(), 'File type not recognized!');
  67. }
  68. // $result = $this->api->downloadData('tests/tmp/mongodb');
  69. // $this->assertTrue(true);
  70. }
  71. public function testApiCollection(): void
  72. {
  73. $collection = $this->api->getByFacets([]);
  74. $this->assertInstanceOf(Collection::class, $collection);
  75. $this->assertEquals($collection->pageCount(), 0);
  76. try {
  77. $collection = $this->api->getByFacets(['trace' => 'egg', 'country' => 'france'], 3);
  78. $this->assertTrue(false);
  79. } catch (\PHPUnit\Framework\Error\Notice $e) {
  80. $this->assertEquals($e->getMessage(), 'OpenFoodFact - Your request has been redirect');
  81. }
  82. $collection = $this->api->getByFacets(['trace' => 'eggs', 'country' => 'france'], 3);
  83. $this->assertInstanceOf(Collection::class, $collection);
  84. $this->assertEquals($collection->pageCount(), 20);
  85. $this->assertEquals($collection->getPage(), 3);
  86. $this->assertEquals($collection->getSkip(), 40);
  87. $this->assertEquals($collection->getPageSize(), 20);
  88. $this->assertGreaterThan(1000, $collection->searchCount());
  89. foreach ($collection as $key => $doc) {
  90. if ($key > 1) {
  91. break;
  92. }
  93. $this->assertInstanceOf(FoodDocument::class, $doc);
  94. $this->assertInstanceOf(Document::class, $doc);
  95. }
  96. }
  97. public function testApiSearch(): void
  98. {
  99. $collection = $this->api->search('volvic', 3, 30);
  100. $this->assertInstanceOf(Collection::class, $collection);
  101. $this->assertEquals($collection->pageCount(), 30);
  102. $this->assertGreaterThan(100, $collection->searchCount());
  103. }
  104. public function testFacets(): void
  105. {
  106. $collection = $this->api->getIngredients();
  107. $this->assertInstanceOf(Collection::class, $collection);
  108. $this->assertEquals($collection->pageCount(), 20);
  109. $this->assertEquals($collection->getPageSize(), 20);
  110. $this->assertGreaterThan(70000, $collection->searchCount());
  111. try {
  112. $collection = $this->api->getIngredient();
  113. $this->assertInstanceOf(Collection::class, $collection);
  114. $this->assertTrue(false);
  115. } catch (BadRequestException $e) {
  116. $this->assertEquals($e->getMessage(), 'Facet "ingredient" not found');
  117. }
  118. $collection = $this->api->getPurchase_places();
  119. $this->assertInstanceOf(Collection::class, $collection);
  120. $collection = $this->api->getPackaging_codes();
  121. $this->assertInstanceOf(Collection::class, $collection);
  122. $collection = $this->api->getEntry_dates();
  123. $this->assertInstanceOf(Collection::class, $collection);
  124. try {
  125. $collection = $this->api->getIngredient();
  126. $this->assertTrue(false);
  127. } catch (BadRequestException $e) {
  128. $this->assertEquals($e->getMessage(), 'Facet "ingredient" not found');
  129. }
  130. try {
  131. $collection = $this->api->nope();
  132. } catch (\Exception $e) {
  133. $this->assertTrue(true);
  134. }
  135. }
  136. protected function tearDown()
  137. {
  138. $this->recursiveDeleteDirectory('tests/tmp');
  139. }
  140. }