/tests/QueryType/ManagedResources/RequestBuilder/StopwordsTest.php

https://github.com/Gasol/solarium · PHP · 278 lines · 216 code · 44 blank · 18 comment · 0 complexity · 1d99aa47c7b652befd8c5162834bd506 MD5 · raw file

  1. <?php
  2. namespace Solarium\Tests\QueryType\ManagedResources\RequestBuilder;
  3. use PHPUnit\Framework\TestCase;
  4. use Solarium\Core\Client\Request;
  5. use Solarium\Exception\RuntimeException;
  6. use Solarium\QueryType\ManagedResources\Query\AbstractCommand;
  7. use Solarium\QueryType\ManagedResources\Query\Command\Config as ConfigCommand;
  8. use Solarium\QueryType\ManagedResources\Query\Command\Delete as DeleteCommand;
  9. use Solarium\QueryType\ManagedResources\Query\Command\Exists as ExistsCommand;
  10. use Solarium\QueryType\ManagedResources\Query\Command\Remove as RemoveCommand;
  11. use Solarium\QueryType\ManagedResources\Query\Command\Stopwords\Add as AddCommand;
  12. use Solarium\QueryType\ManagedResources\Query\Command\Stopwords\Create as CreateCommand;
  13. use Solarium\QueryType\ManagedResources\Query\Stopwords as StopwordsQuery;
  14. use Solarium\QueryType\ManagedResources\Query\Stopwords\InitArgs;
  15. use Solarium\QueryType\ManagedResources\RequestBuilder\Resource as StopwordsRequestBuilder;
  16. class StopwordsTest extends TestCase
  17. {
  18. /**
  19. * @var StopwordsQuery
  20. */
  21. protected $query;
  22. /**
  23. * @var StopwordsRequestBuilder
  24. */
  25. protected $builder;
  26. public function setUp(): void
  27. {
  28. $this->query = new StopwordsQuery();
  29. $this->query->setName('dutch');
  30. $this->builder = new StopwordsRequestBuilder();
  31. }
  32. public function testBuild()
  33. {
  34. $handler = 'schema/analysis/stopwords/dutch';
  35. $request = $this->builder->build($this->query);
  36. $this->assertEquals(
  37. [
  38. 'wt' => 'json',
  39. 'json.nl' => 'flat',
  40. 'omitHeader' => 'true',
  41. ],
  42. $request->getParams()
  43. );
  44. $this->assertSame($handler, $request->getHandler());
  45. }
  46. public function testNoName()
  47. {
  48. $this->query->setName('');
  49. $this->expectException(RuntimeException::class);
  50. $this->expectExceptionMessage('Name of the resource is not set in the query.');
  51. $request = $this->builder->build($this->query);
  52. }
  53. public function testUnsupportedCommand()
  54. {
  55. $command = new UnsupportedStopwordsCommand();
  56. $this->query->setCommand($command);
  57. $this->expectException(RuntimeException::class);
  58. $this->expectExceptionMessage('Unsupported command type: unsupportedtype');
  59. $request = $this->builder->build($this->query);
  60. }
  61. public function testQuery()
  62. {
  63. $request = $this->builder->build($this->query);
  64. $this->assertSame(Request::METHOD_GET, $request->getMethod());
  65. $this->assertSame('schema/analysis/stopwords/dutch', $request->getHandler());
  66. $this->assertNull($request->getRawData());
  67. }
  68. public function testQueryWithTerm()
  69. {
  70. $this->query->setTerm('de');
  71. $request = $this->builder->build($this->query);
  72. $this->assertSame(Request::METHOD_GET, $request->getMethod());
  73. $this->assertSame('schema/analysis/stopwords/dutch/de', $request->getHandler());
  74. $this->assertNull($request->getRawData());
  75. }
  76. public function testAdd()
  77. {
  78. $command = new AddCommand();
  79. $command->setStopwords(['de']);
  80. $this->query->setCommand($command);
  81. $request = $this->builder->build($this->query);
  82. $this->assertSame(Request::METHOD_PUT, $request->getMethod());
  83. $this->assertSame('schema/analysis/stopwords/dutch', $request->getHandler());
  84. $this->assertSame('["de"]', $request->getRawData());
  85. }
  86. public function testAddWithoutStopwords()
  87. {
  88. $command = new AddCommand();
  89. $this->query->setCommand($command);
  90. $this->expectException(RuntimeException::class);
  91. $this->expectExceptionMessage('Missing data for ADD command.');
  92. $request = $this->builder->build($this->query);
  93. }
  94. public function testConfig()
  95. {
  96. $initArgs = new InitArgs();
  97. $initArgs->setInitArgs(['ignoreCase' => true]);
  98. $command = new ConfigCommand();
  99. $command->setInitArgs($initArgs);
  100. $this->query->setCommand($command);
  101. $request = $this->builder->build($this->query);
  102. $this->assertSame(Request::METHOD_PUT, $request->getMethod());
  103. $this->assertSame('schema/analysis/stopwords/dutch', $request->getHandler());
  104. $this->assertSame('{"initArgs":{"ignoreCase":true}}', $request->getRawData());
  105. }
  106. public function testConfigWithoutInitArgs()
  107. {
  108. $command = new ConfigCommand();
  109. $this->query->setCommand($command);
  110. $this->expectException(RuntimeException::class);
  111. $this->expectExceptionMessage('Missing initArgs for CONFIG command.');
  112. $request = $this->builder->build($this->query);
  113. }
  114. public function testCreate()
  115. {
  116. $command = new CreateCommand();
  117. $this->query->setCommand($command);
  118. $request = $this->builder->build($this->query);
  119. $this->assertSame(Request::METHOD_PUT, $request->getMethod());
  120. $this->assertSame('schema/analysis/stopwords/dutch', $request->getHandler());
  121. $this->assertSame('{"class":"org.apache.solr.rest.schema.analysis.ManagedWordSetResource"}', $request->getRawData());
  122. }
  123. public function testCreateWithoutClass()
  124. {
  125. $command = new UnsupportedStopwordsCreateCommand();
  126. $this->query->setCommand($command);
  127. $this->expectException(RuntimeException::class);
  128. $this->expectExceptionMessage('Missing class for CREATE command.');
  129. $request = $this->builder->build($this->query);
  130. }
  131. public function testDelete()
  132. {
  133. $command = new DeleteCommand();
  134. $command->setTerm('de');
  135. $this->query->setCommand($command);
  136. $request = $this->builder->build($this->query);
  137. $this->assertSame(Request::METHOD_DELETE, $request->getMethod());
  138. $this->assertSame('schema/analysis/stopwords/dutch/de', $request->getHandler());
  139. $this->assertNull($request->getRawData());
  140. }
  141. public function testDeleteWithoutTerm()
  142. {
  143. $command = new DeleteCommand();
  144. $this->query->setCommand($command);
  145. $this->expectException(RuntimeException::class);
  146. $this->expectExceptionMessage('Missing term for DELETE command.');
  147. $request = $this->builder->build($this->query);
  148. }
  149. public function testExists()
  150. {
  151. $command = new ExistsCommand();
  152. $command->setTerm('de');
  153. $this->query->setCommand($command);
  154. $request = $this->builder->build($this->query);
  155. // there's a bug since Solr 8.7 with HEAD requests if a term is set (SOLR-15116)
  156. $this->assertSame(Request::METHOD_GET, $request->getMethod());
  157. $this->assertSame('schema/analysis/stopwords/dutch/de', $request->getHandler());
  158. $this->assertNull($request->getRawData());
  159. }
  160. public function testExistsWithoutTerm()
  161. {
  162. $command = new ExistsCommand();
  163. $this->query->setCommand($command);
  164. $request = $this->builder->build($this->query);
  165. $this->assertSame(Request::METHOD_HEAD, $request->getMethod());
  166. $this->assertSame('schema/analysis/stopwords/dutch', $request->getHandler());
  167. $this->assertNull($request->getRawData());
  168. }
  169. /**
  170. * Reserved characters per RFC 3986 in a REST resource name need to be percent-encoded;
  171. * + the percent character that serves as the indicator for percent-encoded octets;
  172. * + the space character that mustn't be confused with embedded whitespace.
  173. *
  174. * When talking with Solr, these characters actually need to be encoded twice to make it
  175. * through the servlet, effectively encoding every octet indicator again (SOLR-6853).
  176. *
  177. * @see https://datatracker.ietf.org/doc/html/rfc3986#section-2
  178. * @see https://issues.apache.org/jira/browse/SOLR-6853
  179. */
  180. public function testReservedCharacters()
  181. {
  182. $unencoded = ':/?#[]@% ';
  183. $encoded = '%253A%252F%253F%2523%255B%255D%2540%2525%2520';
  184. $command = new ExistsCommand();
  185. $command->setTerm($unencoded);
  186. $this->query->setName($unencoded);
  187. $this->query->setCommand($command);
  188. $request = $this->builder->build($this->query);
  189. $this->assertStringEndsWith('/'.$encoded.'/'.$encoded, $request->getHandler());
  190. }
  191. public function testRemove()
  192. {
  193. $command = new RemoveCommand();
  194. $this->query->setCommand($command);
  195. $request = $this->builder->build($this->query);
  196. $this->assertSame(Request::METHOD_DELETE, $request->getMethod());
  197. $this->assertSame('schema/analysis/stopwords/dutch', $request->getHandler());
  198. $this->assertNull($request->getRawData());
  199. }
  200. }
  201. class UnsupportedStopwordsCommand extends AbstractCommand
  202. {
  203. public function getType(): string
  204. {
  205. return 'unsupportedtype';
  206. }
  207. public function getRequestMethod(): string
  208. {
  209. return '';
  210. }
  211. public function getRawData(): string
  212. {
  213. return '';
  214. }
  215. public function getTerm(): string
  216. {
  217. return '';
  218. }
  219. }
  220. class UnsupportedStopwordsCreateCommand extends AbstractCommand
  221. {
  222. public function getType(): string
  223. {
  224. return StopwordsQuery::COMMAND_CREATE;
  225. }
  226. public function getRequestMethod(): string
  227. {
  228. return Request::METHOD_PUT;
  229. }
  230. public function getRawData(): ?string
  231. {
  232. return null;
  233. }
  234. }