Skip to content

Commit

Permalink
return null when barcode find returns product without code + phpunit …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
epalmans committed Jan 4, 2020
1 parent 99d88d5 commit e11ed5d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -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;}}}
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;}}}
4 changes: 3 additions & 1 deletion src/OpenFoodFacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions tests/BarcodeFindTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Palmans\LaravelOpenFoodFacts\Tests;

use Orchestra\Testbench\TestCase;
use Palmans\LaravelOpenFoodFacts\Facades\OpenFoodFacts;

class BarcodeFindTest extends TestCase
{
protected function getPackageProviders($app)
{
return [\Palmans\LaravelOpenFoodFacts\OpenFoodFactsServiceProvider::class];
}

/** @test */
public function it_returns_a_document_when_product_found()
{
$doc = OpenFoodFacts::barcode('0737628064502');

$this->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('');
}
}
14 changes: 0 additions & 14 deletions tests/ExampleTest.php

This file was deleted.

45 changes: 45 additions & 0 deletions tests/ProductSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Palmans\LaravelOpenFoodFacts\Tests;

use OpenFoodFacts\Document;
use Orchestra\Testbench\TestCase;
use Palmans\LaravelOpenFoodFacts\Facades\OpenFoodFacts;

class ProductSearchTest extends TestCase
{
protected function getPackageProviders($app)
{
return [\Palmans\LaravelOpenFoodFacts\OpenFoodFactsServiceProvider::class];
}

/** @test */
public function it_returns_a_laravelcollection_with_documents()
{
$results = OpenFoodFacts::find('Stir-Fry Rice Noodles');

$this->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');
}
}

0 comments on commit e11ed5d

Please sign in to comment.