search_api-8.x-1.15/tests/search_api_test/src/PluginTestTrait.php
tests/search_api_test/src/PluginTestTrait.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <?php namespace Drupal\search_api_test; /** * Provides functionality for tests that deal with test plugins. * * @see \Drupal\search_api_test\TestPluginTrait */ trait PluginTestTrait { /** * Sets an exception to be thrown on calls to the given method. * * @param string $plugin_type * The "short" plugin type. * @param string $method * The method on the plugin object which should throw an exception. * @param bool $error * (optional) If TRUE, further calls to the method will throw exceptions, * otherwise they won't. */ protected function setError( $plugin_type , $method , $error = TRUE) { $key = "search_api_test.$plugin_type.exception.$method" ; \Drupal::state()->set( $key , $error ); } /** * Sets the return value for a certain method on a test plugin. * * @param string $plugin_type * The "short" plugin type. * @param string $method * The method name. * @param mixed $value * The value that should be returned. */ protected function setReturnValue( $plugin_type , $method , $value ) { $key = "search_api_test.$plugin_type.return.$method" ; \Drupal::state()->set( $key , $value ); } /** * Overrides a method for a certain plugin. * * @param string $plugin_type * The "short" plugin type. * @param string $method * The name of the method to override. * @param callable|null $override * The new code of the method, or NULL to use the default. */ protected function setMethodOverride( $plugin_type , $method , callable $override = NULL) { $key = "search_api_test.$plugin_type.method.$method" ; \Drupal::state()->set( $key , $override ); } /** * Retrieves the methods called on a given plugin. * * @param string $plugin_type * The "short" plugin type. * @param bool $reset * (optional) If TRUE, also clear the list of called methods for that type. * * @return string[] * The methods called on the given plugin. */ protected function getCalledMethods( $plugin_type , $reset = TRUE) { $key = "search_api_test.$plugin_type.methods_called" ; $methods = \Drupal::state()->get( $key , []); if ( $reset ) { \Drupal::state()-> delete ( $key ); } return $methods ; } /** * Retrieves the arguments of a certain method called on the given plugin. * * @param string $plugin_type * The "short" plugin type. * @param string $method * The method name. * * @return array * The arguments of the last call to the method. */ protected function getMethodArguments( $plugin_type , $method ) { $key = "search_api_test.$plugin_type.method_arguments.$method" ; return \Drupal::state()->get( $key ); } } |