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