| @@ 15-84 (lines=70) @@ | ||
| 12 | use GBProd\Specification\Specification; |
|
| 13 | use PHPUnit\Framework\TestCase; |
|
| 14 | ||
| 15 | class AndXFactoryTest extends TestCase |
|
| 16 | { |
|
| 17 | public function testConstruct() |
|
| 18 | { |
|
| 19 | $factory = new AndXFactory(new Registry()); |
|
| 20 | ||
| 21 | $this->assertInstanceOf(AndXFactory::class, $factory); |
|
| 22 | } |
|
| 23 | ||
| 24 | public function testCreateReturnsAndxQuery() |
|
| 25 | { |
|
| 26 | $andx = $this->createAndX(); |
|
| 27 | $registry = $this->createRegistry($andx); |
|
| 28 | ||
| 29 | $factory = new AndXFactory($registry); |
|
| 30 | ||
| 31 | $query = $factory->create($andx, new QueryBuilder()); |
|
| 32 | ||
| 33 | $this->assertInstanceOf(BoolQuery::class, $query); |
|
| 34 | ||
| 35 | $this->assertArrayHasKey('bool', $query->toArray()); |
|
| 36 | $this->assertArrayHasKey('must', $query->toArray()['bool']); |
|
| 37 | $this->assertCount(2, $query->toArray()['bool']['must']); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @return AndX |
|
| 42 | */ |
|
| 43 | private function createAndX() |
|
| 44 | { |
|
| 45 | return new AndX( |
|
| 46 | $this->createMock(Specification::class), |
|
| 47 | $this->createMock(Specification::class) |
|
| 48 | ); |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * @param AndX $andx |
|
| 53 | * |
|
| 54 | * @return Registry |
|
| 55 | */ |
|
| 56 | private function createRegistry($andx) |
|
| 57 | { |
|
| 58 | $factory = $this->createMock(Factory::class); |
|
| 59 | $factory |
|
| 60 | ->expects($this->any()) |
|
| 61 | ->method('create') |
|
| 62 | ->willReturn($this->createMock(AbstractQuery::class)) |
|
| 63 | ; |
|
| 64 | ||
| 65 | $registry = new Registry(); |
|
| 66 | ||
| 67 | $registry->register(get_class($andx->getFirstPart()), $factory); |
|
| 68 | $registry->register(get_class($andx->getSecondPart()), $factory); |
|
| 69 | ||
| 70 | return $registry; |
|
| 71 | } |
|
| 72 | ||
| 73 | ||
| 74 | public function testCreateThrowExceptionIfNotAndXSpecification() |
|
| 75 | { |
|
| 76 | $spec = $this->createMock(Specification::class); |
|
| 77 | $registry = new Registry(); |
|
| 78 | $factory = new AndXFactory($registry); |
|
| 79 | ||
| 80 | $this->expectException(\InvalidArgumentException::class); |
|
| 81 | ||
| 82 | $factory->create($spec, new QueryBuilder()); |
|
| 83 | } |
|
| 84 | } |
|
| 85 | ||
| @@ 15-84 (lines=70) @@ | ||
| 12 | use GBProd\Specification\Specification; |
|
| 13 | use PHPUnit\Framework\TestCase; |
|
| 14 | ||
| 15 | class OrXFactoryTest extends TestCase |
|
| 16 | { |
|
| 17 | public function testConstruct() |
|
| 18 | { |
|
| 19 | $factory = new OrXFactory(new Registry()); |
|
| 20 | ||
| 21 | $this->assertInstanceOf(OrXFactory::class, $factory); |
|
| 22 | } |
|
| 23 | ||
| 24 | public function testCreateReturnsOrxQuery() |
|
| 25 | { |
|
| 26 | $orx = $this->createOrX(); |
|
| 27 | $registry = $this->createRegistry($orx); |
|
| 28 | ||
| 29 | $factory = new OrXFactory($registry); |
|
| 30 | ||
| 31 | $query = $factory->create($orx, new QueryBuilder()); |
|
| 32 | ||
| 33 | $this->assertInstanceOf(BoolQuery::class, $query); |
|
| 34 | ||
| 35 | $this->assertArrayHasKey('bool', $query->toArray()); |
|
| 36 | $this->assertArrayHasKey('should', $query->toArray()['bool']); |
|
| 37 | $this->assertCount(2, $query->toArray()['bool']['should']); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @return OrX |
|
| 42 | */ |
|
| 43 | private function createOrX() |
|
| 44 | { |
|
| 45 | return new OrX( |
|
| 46 | $this->createMock(Specification::class), |
|
| 47 | $this->createMock(Specification::class) |
|
| 48 | ); |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * @param OrX $orx |
|
| 53 | * |
|
| 54 | * @return Registry |
|
| 55 | */ |
|
| 56 | private function createRegistry($orx) |
|
| 57 | { |
|
| 58 | $factory = $this->createMock(Factory::class); |
|
| 59 | $factory |
|
| 60 | ->expects($this->any()) |
|
| 61 | ->method('create') |
|
| 62 | ->willReturn($this->createMock(AbstractQuery::class)) |
|
| 63 | ; |
|
| 64 | ||
| 65 | $registry = new Registry(); |
|
| 66 | ||
| 67 | $registry->register(get_class($orx->getFirstPart()), $factory); |
|
| 68 | $registry->register(get_class($orx->getSecondPart()), $factory); |
|
| 69 | ||
| 70 | return $registry; |
|
| 71 | } |
|
| 72 | ||
| 73 | ||
| 74 | public function testCreateThrowExceptionIfNotOrXSpecification() |
|
| 75 | { |
|
| 76 | $spec = $this->createMock(Specification::class); |
|
| 77 | $registry = new Registry(); |
|
| 78 | $factory = new OrXFactory($registry); |
|
| 79 | ||
| 80 | $this->expectException(\InvalidArgumentException::class); |
|
| 81 | ||
| 82 | $factory->create($spec, new QueryBuilder()); |
|
| 83 | } |
|
| 84 | } |
|
| 85 | ||