|
| 1 | +<?php |
| 2 | +namespace Elastica\Test; |
| 3 | + |
| 4 | +use Elastica\Client; |
| 5 | +use Elastica\Index; |
| 6 | +use Elastica\IndexTemplate; |
| 7 | +use Elastica\Request; |
| 8 | +use Elastica\Response; |
| 9 | +use Elastica\Test\Base as BaseTest; |
| 10 | +use Elastica\Type; |
| 11 | + |
| 12 | +/** |
| 13 | + * IndexTemplate class tests |
| 14 | + * |
| 15 | + * @author Dmitry Balabka <[email protected]> |
| 16 | + */ |
| 17 | +class IndexTemplateTest extends BaseTest |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @group unit |
| 21 | + */ |
| 22 | + public function testInstantiate() |
| 23 | + { |
| 24 | + $name = 'index_template1'; |
| 25 | + $client = $this->_getClient(); |
| 26 | + $indexTemplate = new IndexTemplate($client, $name); |
| 27 | + $indexTemplate->getName(); |
| 28 | + $this->assertSame($client, $indexTemplate->getClient()); |
| 29 | + $this->assertEquals($name, $indexTemplate->getName()); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @expectedException \Elastica\Exception\InvalidException |
| 34 | + * @group unit |
| 35 | + */ |
| 36 | + public function testIncorrectInstantiate() |
| 37 | + { |
| 38 | + $client = $this->_getClient(); |
| 39 | + new IndexTemplate($client, null); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @group unit |
| 44 | + */ |
| 45 | + public function testDelete() |
| 46 | + { |
| 47 | + $name = 'index_template1'; |
| 48 | + $response = new Response(''); |
| 49 | + /** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */ |
| 50 | + $clientMock = $this->getMock('\Elastica\Client', array('request')); |
| 51 | + $clientMock->expects($this->once()) |
| 52 | + ->method('request') |
| 53 | + ->with('/_template/' . $name, Request::DELETE, array(), array()) |
| 54 | + ->willReturn($response); |
| 55 | + $indexTemplate = new IndexTemplate($clientMock, $name); |
| 56 | + $this->assertSame($response, $indexTemplate->delete()); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @group unit |
| 61 | + */ |
| 62 | + public function testCreate() |
| 63 | + { |
| 64 | + $args = array(1); |
| 65 | + $response = new Response(''); |
| 66 | + $name = 'index_template1'; |
| 67 | + /** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */ |
| 68 | + $clientMock = $this->getMock('\Elastica\Client', array('request')); |
| 69 | + $clientMock->expects($this->once()) |
| 70 | + ->method('request') |
| 71 | + ->with('/_template/' . $name, Request::PUT, $args, array()) |
| 72 | + ->willReturn($response); |
| 73 | + $indexTemplate = new IndexTemplate($clientMock, $name); |
| 74 | + $this->assertSame($response, $indexTemplate->create($args)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @group unit |
| 79 | + */ |
| 80 | + public function testExists() |
| 81 | + { |
| 82 | + $name = 'index_template1'; |
| 83 | + $response = new Response(''); |
| 84 | + $response->setTransferInfo(array('http_code' => 200)); |
| 85 | + /** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */ |
| 86 | + $clientMock = $this->getMock('\Elastica\Client', array('request')); |
| 87 | + $clientMock->expects($this->once()) |
| 88 | + ->method('request') |
| 89 | + ->with('/_template/' . $name, Request::HEAD, array(), array()) |
| 90 | + ->willReturn($response); |
| 91 | + $indexTemplate = new IndexTemplate($clientMock, $name); |
| 92 | + $this->assertTrue($indexTemplate->exists()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @group functional |
| 97 | + */ |
| 98 | + public function testCreateTemplate() |
| 99 | + { |
| 100 | + $template = array( |
| 101 | + 'template' => 'te*', |
| 102 | + 'settings' => array( |
| 103 | + 'number_of_shards' => 1 |
| 104 | + ), |
| 105 | + ); |
| 106 | + $name = 'index_template1'; |
| 107 | + $indexTemplate = new IndexTemplate($this->_getClient(), $name); |
| 108 | + $indexTemplate->create($template); |
| 109 | + $this->assertTrue($indexTemplate->exists()); |
| 110 | + $indexTemplate->delete(); |
| 111 | + $this->assertFalse($indexTemplate->exists()); |
| 112 | + } |
| 113 | +} |
0 commit comments