diff --git a/.phpunit.result.cache b/.phpunit.result.cache index fb8f6c8..9bcf071 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":120:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:1:{s:60:"Palmans\LaravelOpenFoodFacts\Tests\ExampleTest::true_is_true";d:0.005;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":1449:{a:2:{s:7:"defects";a:5:{s:92:"Palmans\LaravelOpenFoodFacts\Tests\BarcodeFindTest::it_returns_a_document_when_product_found";i:4;s:90:"Palmans\LaravelOpenFoodFacts\Tests\BarcodeFindTest::it_returns_null_when_product_not_found";i:3;s:94:"Palmans\LaravelOpenFoodFacts\Tests\BarcodeFindTest::it_throws_an_exception_when_argument_empty";i:3;s:99:"Palmans\LaravelOpenFoodFacts\Tests\ProductSearchTest::it_returns_a_laravelcollection_with_documents";i:3;s:113:"Palmans\LaravelOpenFoodFacts\Tests\ProductSearchTest::it_returns_an_empty_laravelcollection_when_no_results_found";i:3;}s:5:"times";a:8:{s:60:"Palmans\LaravelOpenFoodFacts\Tests\ExampleTest::true_is_true";d:0.005;s:92:"Palmans\LaravelOpenFoodFacts\Tests\BarcodeFindTest::it_returns_a_document_when_product_found";d:0.508;s:90:"Palmans\LaravelOpenFoodFacts\Tests\BarcodeFindTest::it_returns_null_when_product_not_found";d:0.215;s:84:"Palmans\LaravelOpenFoodFacts\Tests\ProductSearchTest::it_returns_a_laravelcollection";d:0.227;s:94:"Palmans\LaravelOpenFoodFacts\Tests\BarcodeFindTest::it_throws_an_exception_when_argument_empty";d:0.026;s:99:"Palmans\LaravelOpenFoodFacts\Tests\ProductSearchTest::it_returns_a_laravelcollection_with_documents";d:0.24;s:113:"Palmans\LaravelOpenFoodFacts\Tests\ProductSearchTest::it_returns_an_empty_laravelcollection_when_no_results_found";d:0.189;s:96:"Palmans\LaravelOpenFoodFacts\Tests\ProductSearchTest::it_throws_an_exception_on_too_many_results";d:0.811;}}} \ No newline at end of file diff --git a/src/OpenFoodFacts.php b/src/OpenFoodFacts.php index 6c617d5..3675cd8 100644 --- a/src/OpenFoodFacts.php +++ b/src/OpenFoodFacts.php @@ -27,7 +27,9 @@ public function barcode($value) throw new InvalidArgumentException("Argument must represent a barcode"); } - return $this->api->getProduct($value); + $doc = $this->api->getProduct($value); + + return empty($doc->code) ? null : $doc; } public function find($searchterm) diff --git a/tests/BarcodeFindTest.php b/tests/BarcodeFindTest.php new file mode 100644 index 0000000..36a4ef2 --- /dev/null +++ b/tests/BarcodeFindTest.php @@ -0,0 +1,39 @@ +assertInstanceOf(\OpenFoodFacts\Document::class, $doc); + } + + /** @test */ + public function it_returns_null_when_product_not_found() + { + $doc = OpenFoodFacts::barcode('this-barcode-does-not-exist'); + + $this->assertNull($doc); + } + + /** @test */ + public function it_throws_an_exception_when_argument_empty() + { + $this->expectException("InvalidArgumentException"); + $this->expectExceptionMessage("Argument must represent a barcode"); + + OpenFoodFacts::barcode(''); + } +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index e763586..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/ProductSearchTest.php b/tests/ProductSearchTest.php new file mode 100644 index 0000000..216764c --- /dev/null +++ b/tests/ProductSearchTest.php @@ -0,0 +1,45 @@ +assertInstanceOf(\Illuminate\Support\Collection::class, $results); + + $this->assertTrue($results->isNotEmpty()); + + $results->each(function($doc) { + $this->assertInstanceOf(\OpenFoodFacts\Document::class, $doc); + }); + } + + /** @test */ + public function it_returns_an_empty_laravelcollection_when_no_results_found() + { + $results = OpenFoodFacts::find('no-such-product-exists'); + + $this->assertTrue($results->isEmpty()); + } + + /** @test */ + public function it_throws_an_exception_on_too_many_results() + { + $this->expectException("Exception"); + + OpenFoodFacts::find('cola'); + } +}