partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

DistributorAndManufacturerSearchTest.php (4026B)


      1 <?php
      2 
      3 namespace PartKeepr\PartBundle\Tests\Issues;
      4 
      5 use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
      6 use PartKeepr\CoreBundle\Tests\WebTestCase;
      7 use PartKeepr\DistributorBundle\Entity\Distributor;
      8 use PartKeepr\ManufacturerBundle\Entity\Manufacturer;
      9 use PartKeepr\PartBundle\Entity\Part;
     10 use PartKeepr\PartBundle\Entity\PartDistributor;
     11 use PartKeepr\PartBundle\Entity\PartManufacturer;
     12 
     13 /**
     14  * Class DistributorAndManufacturerSearchTest.
     15  *
     16  * Unit test against issue #647 (Filters for Distributor and Manufacturer don't return any results)
     17  */
     18 class DistributorAndManufacturerSearchTest extends WebTestCase
     19 {
     20     /**
     21      * @var ProxyReferenceRepository
     22      */
     23     protected $fixtures;
     24 
     25     public function setUp()
     26     {
     27         $this->fixtures = $this->loadFixtures(
     28             [
     29                 'PartKeepr\StorageLocationBundle\DataFixtures\CategoryDataLoader',
     30                 'PartKeepr\StorageLocationBundle\DataFixtures\StorageLocationLoader',
     31                 'PartKeepr\PartBundle\DataFixtures\CategoryDataLoader',
     32                 'PartKeepr\PartBundle\DataFixtures\PartDataLoader',
     33                 'PartKeepr\AuthBundle\DataFixtures\LoadUserData',
     34                 'PartKeepr\ManufacturerBundle\Tests\DataFixtures\ManufacturerDataLoader',
     35                 'PartKeepr\DistributorBundle\Tests\DataFixtures\DistributorDataLoader',
     36             ]
     37         )->getReferenceRepository();
     38     }
     39 
     40     public function testManufacturerFilter()
     41     {
     42         /**
     43          * @var Part
     44          */
     45         $part = $this->fixtures->getReference('part.1');
     46 
     47         /**
     48          * @var Manufacturer
     49          */
     50         $manufacturer = $this->fixtures->getReference('manufacturer.first');
     51 
     52         $partManufacturer = new PartManufacturer();
     53         $partManufacturer->setManufacturer($manufacturer);
     54         $partManufacturer->setPartNumber('1');
     55 
     56         $part->addManufacturer($partManufacturer);
     57         $this->getContainer()->get('doctrine.orm.default_entity_manager')->flush();
     58 
     59         $filter = [
     60             'property' => 'manufacturers.manufacturer',
     61             'operator' => '=',
     62             'value'    => $this->getContainer()->get('api.iri_converter')->getIriFromItem($manufacturer),
     63         ];
     64 
     65         $filters = [$filter];
     66 
     67         $client = static::makeClient(true);
     68 
     69         $partResource = $this->getContainer()->get('resource.part');
     70         $iri = $this->getContainer()->get('api.iri_converter')->getIriFromResource($partResource);
     71 
     72         $client->request('GET', $iri, ['filter' => json_encode($filters)]);
     73 
     74         $this->assertEquals(200, $client->getResponse()->getStatusCode());
     75 
     76         $data = json_decode($client->getResponse()->getContent());
     77 
     78         $this->assertEquals(1, $data->{'hydra:totalItems'});
     79     }
     80 
     81     public function testDistributorFilter()
     82     {
     83         /**
     84          * @var Part
     85          */
     86         $part = $this->fixtures->getReference('part.1');
     87 
     88         /**
     89          * @var Distributor
     90          */
     91         $distributor = $this->fixtures->getReference('distributor.first');
     92 
     93         $partDistributor = new PartDistributor();
     94         $partDistributor->setDistributor($distributor);
     95 
     96         $part->addDistributor($partDistributor);
     97         $this->getContainer()->get('doctrine.orm.default_entity_manager')->flush();
     98 
     99         $filter = [
    100             'property' => 'distributors.distributor',
    101             'operator' => '=',
    102             'value'    => $this->getContainer()->get('api.iri_converter')->getIriFromItem($distributor),
    103         ];
    104 
    105         $filters = [$filter];
    106 
    107         $client = static::makeClient(true);
    108 
    109         $partResource = $this->getContainer()->get('resource.part');
    110         $iri = $this->getContainer()->get('api.iri_converter')->getIriFromResource($partResource);
    111 
    112         $client->request('GET', $iri, ['filter' => json_encode($filters)]);
    113 
    114         $this->assertEquals(200, $client->getResponse()->getStatusCode());
    115 
    116         $data = json_decode($client->getResponse()->getContent());
    117 
    118         $this->assertEquals(1, $data->{'hydra:totalItems'});
    119     }
    120 }