diff --git a/build.xml b/build.xml index 55fac7d8..3a513d24 100644 --- a/build.xml +++ b/build.xml @@ -4,216 +4,237 @@ schematypens="http://relaxng.org/ns/structure/1.0" ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + default="wordhat:test"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/docker/docker-entrypoint.sh b/build/docker/docker-entrypoint.sh index d5464bc4..98426f5c 100644 --- a/build/docker/docker-entrypoint.sh +++ b/build/docker/docker-entrypoint.sh @@ -5,7 +5,7 @@ WH_WORDPRESS_DIR=/usr/src/wordpress # wait for mysql to come up -while ! mysqladmin ping -h "db" --silent; do +while ! mysqladmin ping -h "db"; do echo "Waiting for mysql..." sleep 5 done diff --git a/build/templates/docker-compose.yml.template b/build/templates/docker-compose.yml.template index 0f5fb987..d66d3850 100644 --- a/build/templates/docker-compose.yml.template +++ b/build/templates/docker-compose.yml.template @@ -2,7 +2,7 @@ version: '3' services: db: - image: healthcheck/mysql:latest + image: mysql:5.7.22 ports: - "3306:3306" environment: @@ -30,6 +30,6 @@ services: - seccomp:unconfined selenium: - image: selenium/standalone-chrome-debug:latest + image: selenium/standalone-chrome-debug:3.11.0 ports: - "4444:4444" diff --git a/phpstan.neon b/phpstan.neon index 9246f2ac..d3c2acf8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: excludes_analyse: # These reference global WP functions. - - %rootDir%/../../../src/Driver/Element/Wpphp/* - - src/Driver/WpphpDriver.php + - %rootDir%/../../../src/Driver/Wpphp/Element/* + - src/Driver/Wpphp/WpphpDriver.php diff --git a/src/Compiler/DriverElementPass.php b/src/Compiler/DriverElementPass.php index 1174c630..8fd0f32f 100644 --- a/src/Compiler/DriverElementPass.php +++ b/src/Compiler/DriverElementPass.php @@ -2,8 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Compiler; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\PluginElementInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; /** @@ -18,22 +18,7 @@ class DriverElementPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - $wordpress = $container->getDefinition('wordpress.wordpress'); - if (! $wordpress) { - return; - } - - foreach ($container->findTaggedServiceIds('wordpress.element') as $id => $attributes) { - foreach ($attributes as $attribute) { - if (! isset($attribute['alias'], $attribute['driver'])) { - continue; - } - - $wordpress->addMethodCall( - 'registerDriverElement', - [$attribute['alias'], new Reference($id), $attribute['driver']] - ); - } - } + $container->registerForAutoconfiguration(PluginElementInterface::class) + ->addTag('wordpress.driver.element.plugin'); } } diff --git a/src/Compiler/DriverPass.php b/src/Compiler/DriverPass.php index 00849b9c..c1299005 100644 --- a/src/Compiler/DriverPass.php +++ b/src/Compiler/DriverPass.php @@ -1,41 +1,78 @@ getDefinition('wordpress.wordpress'); - if (! $wordpress) { - return; - } + $config = $this->getConfigs($container); + + // Grab the default driver name from the config + $defaultDriverName = $container->getParameter('wordpress.default_driver'); + + // Tell WordpressDriverManager about the default driver name + //$wordpress->addMethodCall('setDefaultDriverName', [ + // $defaultDriverName + //]); foreach ($container->findTaggedServiceIds('wordpress.driver') as $id => $attributes) { - foreach ($attributes as $attribute) { - if (empty($attribute['alias'])) { - continue; - } + // Grab the definition + $definition = $container->getDefinition($id); + $class = $definition->getClass(); + + /* TODO: The setParameters method below should be split into an activate + * method and a setParameters method. The setParameters part should be called + * here. + */ + + $driverName = call_user_func($class . '::getShortName'); - $wordpress->addMethodCall('registerDriver', [$attribute['alias'], new Reference($id)]); + // bootstrap the driver at the point it is created + $definition->addMethodCall('bootstrap'); + + if ($driverName === $defaultDriverName) { + $container->setAlias(DriverManagerInterface::class, $id); + $driverName = call_user_func($class . '::setParameters', $container, $config); } } + } - $wordpress->addMethodCall( - 'setDefaultDriverName', - [$container->getParameter('wordpress.wordpress.default_driver')] - ); + /** + * grab the configuration from the container builder + */ + protected function getConfigs(ContainerBuilder $container) + { + return $container->getParameter('wordpress.parameters'); } } diff --git a/src/Compiler/EventSubscriberPass.php b/src/Compiler/EventSubscriberPass.php index 419477bc..a69f315f 100755 --- a/src/Compiler/EventSubscriberPass.php +++ b/src/Compiler/EventSubscriberPass.php @@ -2,29 +2,48 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Compiler; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use RuntimeException; /** - * Event subscribers pass. + * A specific driver can be specified for a scenario or example by using the @driver tag in the gherkin feature file. + * + * To make this work, we need to be able to switch drivers at the start of a scenario (if necessary). This + * is done by the DriverListener which has its prepareWordpressDriver method called whenever a scenario or + * example is started. + * + * It is the role of this class to make that happen. It is a Symfony compiler pass and is registered as such + * by BehatWordpressExtension. During compilation it scans for services which are tagged with 'wordpress.event_subscriber', + * and adds a call to the event_dispatchers 'addSubscriber' method for each service. * - * Register all available event subscribers. */ class EventSubscriberPass implements CompilerPassInterface { /** - * Processes container. + * This method registers each service tagged with 'wordpress.event_subscriber' to the event_dispatcher service. + * + * Firstly it gets the event_dispatcher from the ServiceContainer by looking for a service named 'event_dispatcher'; this + * service is automatically provided by the event_dispatcher bundle. + * + * Then it scans for 'wordpress.event_subscriber' tagged services. These services are tagged by WordpressBehatExtension, + * which reads them (at the moment) from behat.yml. + * + * It checks if a priority has been configured on the service, if it has it uses that value, otherwise it uses 0. + * + * Finally it adds a call to the addSubsciber method at creation time of the event_dispatcher with a reference to the + * subscriber service and the priority as arguments. * * @param ContainerBuilder $container */ public function process(ContainerBuilder $container) { - if (! $container->hasDefinition('wordpress.event_dispatcher')) { - return; + if (! $container->hasDefinition('event_dispatcher')) { + throw new RuntimeException('Event dispatcher cannot be found'); } - $dispatcher = $container->getDefinition('wordpress.event_dispatcher'); + $dispatcher = $container->getDefinition('event_dispatcher'); foreach ($container->findTaggedServiceIds('wordpress.event_subscriber') as $id => $attributes) { foreach ($attributes as $attribute) { diff --git a/src/Context/ContentContext.php b/src/Context/ContentContext.php index 2d549f16..af91f23e 100644 --- a/src/Context/ContentContext.php +++ b/src/Context/ContentContext.php @@ -2,16 +2,18 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context; -use UnexpectedValueException; -use RuntimeException; use Behat\Gherkin\Node\TableNode; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\ContentAwareContextInterface; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\UserAwareContextInterface; use PaulGibbs\WordpressBehatExtension\Context\Traits\ContentAwareContextTrait; use PaulGibbs\WordpressBehatExtension\Context\Traits\UserAwareContextTrait; +use RuntimeException; +use UnexpectedValueException; /** * Provides step definitions for creating content: post types, comments, and terms. */ -class ContentContext extends RawWordpressContext +class ContentContext extends RawWordpressContext implements UserAwareContextInterface, ContentAwareContextInterface { use ContentAwareContextTrait, UserAwareContextTrait; diff --git a/src/Context/EditPostContext.php b/src/Context/EditPostContext.php index cb71468f..68c08e0e 100644 --- a/src/Context/EditPostContext.php +++ b/src/Context/EditPostContext.php @@ -6,11 +6,12 @@ use Behat\Gherkin\Node\PyStringNode; use Behat\Mink\Exception\ExpectationException; use PaulGibbs\WordpressBehatExtension\Context\Traits\ContentAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\ContentAwareContextInterface; /** * Provides step definitions relating to editing content in wp-admin. */ -class EditPostContext extends RawWordpressContext +class EditPostContext extends RawWordpressContext implements ContentAwareContextInterface { use ContentAwareContextTrait; /** diff --git a/src/Context/Initialiser/WordpressAwareInitialiser.php b/src/Context/Initialiser/WordpressAwareInitialiser.php index c6e285d0..cfe78972 100644 --- a/src/Context/Initialiser/WordpressAwareInitialiser.php +++ b/src/Context/Initialiser/WordpressAwareInitialiser.php @@ -1,18 +1,42 @@ wordpress = $wordpress; + public function __construct( + CacheElementInterface $cacheElement, + CommentElementInterface $commentElement, + ContentElementInterface $contentElement, + DatabaseElementInterface $databaseElement, + TermElementInterface $termElement, + ThemeElementInterface $themeElement, + PluginElementInterface $pluginElement, + UserElementInterface $userElement, + WidgetElementInterface $widgetElement, + WordpressDriverManager $wordpress, + array $parameters + ) { + $this->cacheElement = $cacheElement; + $this->commentElement = $commentElement; + $this->contentElement = $contentElement; + $this->databaseElement = $databaseElement; + $this->termElement = $termElement; + $this->themeElement = $themeElement; + $this->pluginElement = $pluginElement; + $this->userElement = $userElement; + $this->widgetElement = $widgetElement; + $this->wordpress = $wordpress; + $this->parameters = $parameters; } /** - * Prepare everything that the Context needs. + * Prepare everything that a Context might need. + * + * It will be great to lose this class and this method and use Symfony Autowiring + * with constructor injection for the following reasons: + * 1) We won't need to do all this horrid type checking below + * 2) Constructor injection of the Elements will mean that they can't get altered by accident + * 3) The setters won't be necessary so there will be fewer methods and therefore a simpler API + * 4) PHPStan won't get upset by all the unknown methods (I might have fixed that) + * + * That won't happen until this + * https://github.com/Behat/Symfony2Extension/issues/128 + * is resolved. * * @param Context $context */ public function initializeContext(Context $context) { - if (! $context instanceof WordpressAwareInterface) { - return; + if ($context instanceof CacheAwareContextInterface) { + $this->initializeCacheAwareContext($context); } + if ($context instanceof CommentAwareContextInterface) { + $this->initializeCommentAwareContext($context); + } + + if ($context instanceof ContentAwareContextInterface) { + $this->initializeContentAwareContext($context); + } + + if ($context instanceof DatabaseAwareContextInterface) { + $this->initializeDatabaseAwareContext($context); + } + + if ($context instanceof PluginAwareContextInterface) { + $this->initializePluginAwareContext($context); + } + + if ($context instanceof TermAwareContextInterface) { + $this->initializeTermAwareContext($context); + } + + if ($context instanceof ThemeAwareContextInterface) { + $this->initializeThemeAwareContext($context); + } + + if ($context instanceof UserAwareContextInterface) { + $this->initializeUserAwareContext($context); + } + + if ($context instanceof WidgetAwareContextInterface) { + $this->initializeWidgetAwareContext($context); + } + + if ($context instanceof WordpressAwareInterface) { + $this->initializeWordpressAwareContext($context); + } + } + + protected function initializeCacheAwareContext(CacheAwareContextInterface $context) + { + $context->setCacheElement($this->cacheElement); + } + + protected function initializeCommentAwareContext(CommentAwareContextInterface $context) + { + $context->setCommentElement($this->commentElement); + } + + protected function initializeContentAwareContext(ContentAwareContextInterface $context) + { + $context->setContentElement($this->contentElement); + } + + protected function initializeDatabaseAwareContext(DatabaseAwareContextInterface $context) + { + $context->setDatabaseElement($this->databaseElement); + } + + protected function initializePluginAwareContext(PluginAwareContextInterface $context) + { + $context->setPluginElement($this->pluginElement); + } + + protected function initializeTermAwareContext(TermAwareContextInterface $context) + { + $context->setTermElement($this->termElement); + } + + protected function initializeThemeAwareContext(ThemeAwareContextInterface $context) + { + $context->setThemeElement($this->themeElement); + } + + protected function initializeUserAwareContext(UserAwareContextInterface $context) + { + $context->setUserElement($this->userElement); + } + + protected function initializeWidgetAwareContext(WidgetAwareContextInterface $context) + { + $context->setWidgetElement($this->widgetElement); + } + + protected function initializeWordpressAwareContext(WordpressAwareInterface $context) + { $context->setWordpress($this->wordpress); $context->setWordpressParameters($this->parameters); } diff --git a/src/Context/Interfaces/CacheAwareContextInterface.php b/src/Context/Interfaces/CacheAwareContextInterface.php new file mode 100644 index 00000000..4aa7e0dc --- /dev/null +++ b/src/Context/Interfaces/CacheAwareContextInterface.php @@ -0,0 +1,16 @@ +getWordpress()->getDriver($name); } diff --git a/src/Context/SiteContext.php b/src/Context/SiteContext.php index ef65065d..69e7215e 100644 --- a/src/Context/SiteContext.php +++ b/src/Context/SiteContext.php @@ -2,12 +2,24 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\PluginAwareContextInterface; +use PaulGibbs\WordpressBehatExtension\Context\Traits\CacheAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Traits\PluginAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Traits\ThemeAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\CacheAwareContextInterface; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\ThemeAwareContextInterface; + /** * Provides step definitions for managing plugins and themes. */ -class SiteContext extends RawWordpressContext +class SiteContext extends RawWordpressContext implements CacheAwareContextInterface, PluginAwareContextInterface, ThemeAwareContextInterface { - use Traits\CacheAwareContextTrait, Traits\PluginAwareContextTrait, Traits\ThemeAwareContextTrait; + use CacheAwareContextTrait, PluginAwareContextTrait, ThemeAwareContextTrait; + + public function __construct() + { + parent::__construct(); + } /** * Clear object cache. diff --git a/src/Context/Traits/BaseAwarenessTrait.php b/src/Context/Traits/BaseAwarenessTrait.php index 0f2f7e14..8a6f9428 100644 --- a/src/Context/Traits/BaseAwarenessTrait.php +++ b/src/Context/Traits/BaseAwarenessTrait.php @@ -2,8 +2,6 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; -use PaulGibbs\WordpressBehatExtension\Driver\DriverInterface; - /** * Parent class for all awareness traits providing common code. */ @@ -14,7 +12,6 @@ trait BaseAwarenessTrait * * @param string $name Optional. Name of specific driver to retrieve. * - * @return DriverInterface */ - public abstract function getDriver(string $name = ''): DriverInterface; + public abstract function getDriver(string $name = ''); } diff --git a/src/Context/Traits/CacheAwareContextTrait.php b/src/Context/Traits/CacheAwareContextTrait.php index 168a7f38..9a5b44c0 100644 --- a/src/Context/Traits/CacheAwareContextTrait.php +++ b/src/Context/Traits/CacheAwareContextTrait.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\CacheElementInterface; + /** * Provides driver agnostic logic (helper methods) relating to caching. */ @@ -9,11 +11,26 @@ trait CacheAwareContextTrait { use BaseAwarenessTrait; + /** + * var CacheElementInterface $cacheElement + */ + protected $cacheElement; + /** * Clear object cache. */ public function clearCache() { - $this->getDriver()->cache->clear(); + $this->cacheElement->clear(); + } + + /** + * Set the cacheElement + * + * @param CacheElementInterface $cacheElement + */ + public function setCacheElement(CacheElementInterface $cacheElement) + { + $this->cacheElement = $cacheElement; } } diff --git a/src/Context/Traits/CommentAwareContextTrait.php b/src/Context/Traits/CommentAwareContextTrait.php index b3243e8c..963deeb8 100644 --- a/src/Context/Traits/CommentAwareContextTrait.php +++ b/src/Context/Traits/CommentAwareContextTrait.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\CommentElementInterface; + /** * Provides driver agnostic logic (helper methods) relating to comments. */ @@ -9,6 +11,13 @@ trait CommentAwareContextTrait { use BaseAwarenessTrait; + /** + * The the Comment Element, injected in by the CommentAwareContextInitializer + * + * @var CommentElementInterface $comment + */ + private $commentElement; + /** * Create a comment. * @@ -20,7 +29,7 @@ trait CommentAwareContextTrait */ public function createComment(array $args): array { - $comment = $this->getDriver()->comment->create($args); + $comment = $this->commentElement->create($args); return array( 'id' => $comment->comment_ID @@ -35,6 +44,14 @@ public function createComment(array $args): array */ public function deleteComment(int $comment_id, array $args = []) { - $this->getDriver()->comment->delete($comment_id, $args); + $this->commentElement->delete($comment_id, $args); + } + + /** + * Set the comment element + */ + public function setCommentElement(CommentElementInterface $commentElement) + { + $this->commentElement = $commentElement; } } diff --git a/src/Context/Traits/ContentAwareContextTrait.php b/src/Context/Traits/ContentAwareContextTrait.php index b2be8014..1b627e81 100644 --- a/src/Context/Traits/ContentAwareContextTrait.php +++ b/src/Context/Traits/ContentAwareContextTrait.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\ContentElementInterface; + /** * Provides driver agnostic logic (helper methods) relating to posts and content. */ @@ -9,6 +11,12 @@ trait ContentAwareContextTrait { use BaseAwarenessTrait; + /** + * + * @var ContentElementInterface $contentElement; + */ + protected $contentElement; + /** * Create content. * @@ -22,7 +30,7 @@ trait ContentAwareContextTrait */ public function createContent(array $args): array { - $post = $this->getDriver()->content->create($args); + $post = $this->contentElement->create($args); return array( 'id' => (int) $post->ID, @@ -45,7 +53,7 @@ public function createContent(array $args): array */ public function getContentFromTitle(string $title, string $post_type = ''): array { - $post = $this->getDriver()->content->get($title, ['by' => 'title', 'post_type' => $post_type]); + $post = $this->contentElement->get($title, ['by' => 'title', 'post_type' => $post_type]); return array( 'id' => (int) $post->ID, @@ -62,6 +70,14 @@ public function getContentFromTitle(string $title, string $post_type = ''): arra */ public function deleteContent(int $content_id, array $args = []) { - $this->getDriver()->content->delete($content_id, $args); + $this->contentElement->delete($content_id, $args); + } + + /** + * Set content element + */ + public function setContentElement(ContentElementInterface $contentElement) + { + $this->contentElement = $contentElement; } } diff --git a/src/Context/Traits/DatabaseAwareContextTrait.php b/src/Context/Traits/DatabaseAwareContextTrait.php index 6ff7586a..189de583 100644 --- a/src/Context/Traits/DatabaseAwareContextTrait.php +++ b/src/Context/Traits/DatabaseAwareContextTrait.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\DatabaseElementInterface; + /** * Provides driver agnostic logic (helper methods) relating to the database. */ @@ -10,24 +12,22 @@ trait DatabaseAwareContextTrait use BaseAwarenessTrait; /** - * Export WordPress database. - * - * @param array $args - * - * @return string Path to the export file. + * @var DatabaseElementInterface $database */ + protected $databaseElement; + public function exportDatabase(array $args): string { - return $this->getDriver()->database->export(0, $args); + return $this->databaseElement->export(0, $args); } - /** - * Import WordPress database. - * - * @param array $args - */ public function importDatabase(array $args) { - $this->getDriver()->database->import(0, $args); + $this->databaseElement->import(0, $args); + } + + public function setDatabaseElement(DatabaseElementInterface $databaseElement) + { + $this->databaseElement = $databaseElement; } } diff --git a/src/Context/Traits/PluginAwareContextTrait.php b/src/Context/Traits/PluginAwareContextTrait.php index 64f9cb83..b989bf96 100644 --- a/src/Context/Traits/PluginAwareContextTrait.php +++ b/src/Context/Traits/PluginAwareContextTrait.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\PluginElementInterface; + /** * Provides driver agnostic logic (helper methods) relating to plugins. */ @@ -9,6 +11,11 @@ trait PluginAwareContextTrait { use BaseAwarenessTrait; + /** + * @var PluginElementInterface + */ + private $pluginElement; + /** * Activate a plugin. * @@ -16,7 +23,7 @@ trait PluginAwareContextTrait */ public function activatePlugin(string $plugin) { - $this->getDriver()->plugin->activate($plugin); + $this->pluginElement->activate($plugin); } /** @@ -26,6 +33,11 @@ public function activatePlugin(string $plugin) */ public function deactivatePlugin(string $plugin) { - $this->getDriver()->plugin->deactivate($plugin); + $this->pluginElement->deactivate($plugin); + } + + public function setPluginElement(PluginElementInterface $pluginElement) + { + $this->pluginElement = $pluginElement; } } diff --git a/src/Context/Traits/ThemeAwareContextTrait.php b/src/Context/Traits/ThemeAwareContextTrait.php index a9d83922..ba21ccb9 100644 --- a/src/Context/Traits/ThemeAwareContextTrait.php +++ b/src/Context/Traits/ThemeAwareContextTrait.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\ThemeElementInterface; + /** * Provides driver agnostic logic (helper methods) relating to themes. */ @@ -9,6 +11,11 @@ trait ThemeAwareContextTrait { use BaseAwarenessTrait; + /** + * @var ThemeElementInterface $themeElement; + */ + protected $themeElement; + /** * Switch active theme. * @@ -16,6 +23,11 @@ trait ThemeAwareContextTrait */ public function switchTheme(string $theme) { - $this->getDriver()->theme->change($theme); + $this->themeElement->change($theme); + } + + public function setThemeElement(ThemeElementInterface $themeElement) + { + $this->themeElement = $themeElement; } } diff --git a/src/Context/Traits/UserAwareContextTrait.php b/src/Context/Traits/UserAwareContextTrait.php index d2c832d0..e85893b9 100644 --- a/src/Context/Traits/UserAwareContextTrait.php +++ b/src/Context/Traits/UserAwareContextTrait.php @@ -6,6 +6,7 @@ use Behat\Mink\Exception\ExpectationException; use Behat\Mink\Exception\UnsupportedDriverActionException; use UnexpectedValueException; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\UserElementInterface; /** * Provides driver agnostic logic (helper methods) relating to users. @@ -14,6 +15,12 @@ trait UserAwareContextTrait { use BaseAwarenessTrait; + /** + * + * @var UserElementInterface $userElement + */ + protected $userElement; + /** * Log in the user. * @@ -116,7 +123,7 @@ public function createUser(string $user_login, string $user_email, array $args = $args['user_login'] = $user_login; try { - $user = $this->getDriver()->user->create($args); + $user = $this->userElement->create($args); } catch (UnexpectedValueException $exception) { $user = $this->getExistingMatchingUser($args); } @@ -144,7 +151,7 @@ public function createUser(string $user_login, string $user_email, array $args = private function getExistingMatchingUser(array $args) { $user_id = $this->getUserIdFromLogin($args['user_login']); - $user = $this->getDriver()->user->get($user_id); + $user = $this->userElement->get($user_id); /* users can have more than one role so needs to be a special case */ if (array_key_exists('role', $args)) { @@ -159,7 +166,7 @@ private function getExistingMatchingUser(array $args) foreach ($args as $parameter => $value) { if ($parameter === 'password') { try { - if (! $this->getDriver()->user->validateCredentials($args['user_login'], $value)) { + if (! $this->userElement->validateCredentials($args['user_login'], $value)) { throw new UnexpectedValueException('User with login : ' . $user->user_login . ' exists but password is incorrect'); } } catch (UnsupportedDriverActionException $exception) { @@ -241,7 +248,7 @@ private function isValidUserParameter(string $user_parameter): bool */ public function getUserIdFromLogin(string $username): int { - return (int) $this->getDriver()->user->get($username, ['by' => 'login'])->ID; + return (int) $this->userElement->get($username, ['by' => 'login'])->ID; } /** @@ -252,7 +259,7 @@ public function getUserIdFromLogin(string $username): int */ public function deleteUser(int $user_id, array $args = []) { - $this->getDriver()->user->delete($user_id, $args); + $this->userElement->delete($user_id, $args); } /** @@ -265,6 +272,16 @@ public function deleteUser(int $user_id, array $args = []) */ public function getUserDataFromUsername(string $data, string $username) { - return $this->getDriver()->user->get($username, ['by' => 'login'])->{$data}; + return $this->userElement->get($username, ['by' => 'login'])->{$data}; + } + + /** + * Set the userElement + * + * @param UserElementInterface $userElement + */ + public function setUserElement(UserElementInterface $userElement) + { + $this->userElement = $userElement; } } diff --git a/src/Context/Traits/WidgetAwareContextTrait.php b/src/Context/Traits/WidgetAwareContextTrait.php index 34d2a7e5..fcbe2eb4 100644 --- a/src/Context/Traits/WidgetAwareContextTrait.php +++ b/src/Context/Traits/WidgetAwareContextTrait.php @@ -2,10 +2,18 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Context\Traits; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\WidgetElementInterface; + trait WidgetAwareContextTrait { use BaseAwarenessTrait; + /** + * + * @var WidgetElementInterface $widgetElement; + */ + protected $widgetElement; + /** * Gets a sidebar ID from its human-readable name. * @@ -15,7 +23,7 @@ trait WidgetAwareContextTrait */ public function getWidgetSidebar(string $sidebar_name): string { - return $this->getDriver()->widget->getSidebar($sidebar_name); + return $this->widgetElement->getSidebar($sidebar_name); } /** @@ -27,6 +35,11 @@ public function getWidgetSidebar(string $sidebar_name): string */ public function addWidgetToSidebar(string $widget_name, string $sidebar_id, array $args) { - $this->getDriver()->widget->addToSidebar($widget_name, $sidebar_id, $args); + $this->widgetElement->addToSidebar($widget_name, $sidebar_id, $args); + } + + public function setWidgetElement(WidgetElementInterface $widgetElement) + { + $this->widgetElement = $widgetElement; } } diff --git a/src/Context/UserContext.php b/src/Context/UserContext.php index 377ed9d8..b9c87587 100644 --- a/src/Context/UserContext.php +++ b/src/Context/UserContext.php @@ -4,14 +4,17 @@ use Behat\Gherkin\Node\TableNode; use Behat\Mink\Exception\ExpectationException; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\UserAwareContextInterface; +use PaulGibbs\WordpressBehatExtension\Context\Traits\CacheAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Traits\UserAwareContextTrait; use RuntimeException; /** * Provides step definitions for all things relating to users. */ -class UserContext extends RawWordpressContext +class UserContext extends RawWordpressContext implements UserAwareContextInterface { - use Traits\UserAwareContextTrait, Traits\CacheAwareContextTrait; + use UserAwareContextTrait, CacheAwareContextTrait; /** * Add specified user accounts. diff --git a/src/Context/WidgetContext.php b/src/Context/WidgetContext.php index 8746ea24..25a2369d 100644 --- a/src/Context/WidgetContext.php +++ b/src/Context/WidgetContext.php @@ -4,11 +4,12 @@ use Behat\Gherkin\Node\TableNode; use PaulGibbs\WordpressBehatExtension\Context\Traits\WidgetAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\WidgetAwareContextInterface; /** * Provides step definitions relating to widgets. */ -class WidgetContext extends RawWordpressContext +class WidgetContext extends RawWordpressContext implements WidgetAwareContextInterface { use WidgetAwareContextTrait; diff --git a/src/Context/WordpressContext.php b/src/Context/WordpressContext.php index d449125d..c0f273c0 100644 --- a/src/Context/WordpressContext.php +++ b/src/Context/WordpressContext.php @@ -8,11 +8,12 @@ use PaulGibbs\WordpressBehatExtension\Context\Traits\CacheAwareContextTrait; use PaulGibbs\WordpressBehatExtension\Context\Traits\DatabaseAwareContextTrait; use PaulGibbs\WordpressBehatExtension\Context\Traits\PageObjectAwareContextTrait; +use PaulGibbs\WordpressBehatExtension\Context\Interfaces\DatabaseAwareContextInterface; /** * Provides step definitions for a range of common tasks. Recommended for all test suites. */ -class WordpressContext extends RawWordpressContext +class WordpressContext extends RawWordpressContext implements DatabaseAwareContextInterface { use PageObjectAwareContextTrait, CacheAwareContextTrait, DatabaseAwareContextTrait; diff --git a/src/Driver/BaseDriver.php b/src/Driver/BaseManager.php similarity index 88% rename from src/Driver/BaseDriver.php rename to src/Driver/BaseManager.php index ec4547da..d3810819 100644 --- a/src/Driver/BaseDriver.php +++ b/src/Driver/BaseManager.php @@ -2,8 +2,8 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension\Driver; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\ElementInterface; use PaulGibbs\WordpressBehatExtension\Exception\UnsupportedDriverActionException; -use PaulGibbs\WordpressBehatExtension\Driver\Element\ElementInterface; /** * Common base class for WordPress drivers. @@ -19,7 +19,7 @@ * @property \PaulGibbs\WordpressBehatExtension\Driver\ElementInterface $theme * @property \PaulGibbs\WordpressBehatExtension\Driver\ElementInterface $user */ -abstract class BaseDriver implements DriverInterface +abstract class BaseManager implements DriverManagerInterface { /** * Track driver bootstrapping. @@ -31,7 +31,7 @@ abstract class BaseDriver implements DriverInterface /** * Registered driver elements. * - * @var \PaulGibbs\WordpressBehatExtension\Driver\Element\ElementInterface[] + * @var ElementInterface[] */ protected $elements = []; @@ -42,7 +42,7 @@ abstract class BaseDriver implements DriverInterface * * @throws UnsupportedDriverActionException * - * @return null|\PaulGibbs\WordpressBehatExtension\Driver\Element\ElementInterface Return element object. + * @return null|ElementInterface Return element object. */ public function __get(string $name) { diff --git a/src/Driver/BlackboxDriver.php b/src/Driver/BlackboxDriver.php deleted file mode 100644 index 6e4fe993..00000000 --- a/src/Driver/BlackboxDriver.php +++ /dev/null @@ -1,12 +0,0 @@ -drivers = $drivers; - } - - /** - * Create an item for this element. - * - * @param array $args Data used to create an object. - * - * @return mixed The new item. - * - * @throws UnsupportedDriverActionException - */ - public function create($args) - { - throw new UnsupportedDriverActionException(sprintf('use the %s element create method', static::class)); - } - - /** - * Retrieve an item for this element. - * - * @param int|string $id Object ID. - * @param array $args Optional data used to fetch an object. - * - * @throws UnsupportedDriverActionException - * - * @return mixed The item. - */ - public function get($id, $args = []) - { - throw new UnsupportedDriverActionException(sprintf('use the %s element get method', static::class)); - } - - /** - * Update an item for this element. - * - * @param int|string $id Object ID. - * @param array $args Optional data used to update an object. - * - * @throws UnsupportedDriverActionException - */ - public function update($id, $args = []) - { - throw new UnsupportedDriverActionException(sprintf('use the %s element update method', static::class)); - } - /** - * Delete an item for this element. - * - * @param int|string $id Object ID. - * @param array $args Optional data used to delete an object. - * - * @throws UnsupportedDriverActionException - */ - public function delete($id, $args = []) - { - throw new UnsupportedDriverActionException(sprintf('use the %s element delete method', static::class)); - } } diff --git a/src/Driver/Element/BasePluginElement.php b/src/Driver/Element/BasePluginElement.php new file mode 100644 index 00000000..5de2cb82 --- /dev/null +++ b/src/Driver/Element/BasePluginElement.php @@ -0,0 +1,14 @@ +drivers->getDriver()->wpcli('cache', 'flush'); - } - - - /* - * Convenience methods. - */ - - /** - * Alias of update(). - * - * @see update() - */ - public function clear() - { - $this->update(0); - } -} diff --git a/src/Driver/Element/Wpcli/ThemeElement.php b/src/Driver/Element/Wpcli/ThemeElement.php deleted file mode 100644 index 39aa8b67..00000000 --- a/src/Driver/Element/Wpcli/ThemeElement.php +++ /dev/null @@ -1,40 +0,0 @@ -drivers->getDriver()->wpcli('theme', 'activate', [$id]); - } - - - /* - * Convenience methods. - */ - - /** - * Alias of update(). - * - * @see update() - * - * @param string $id Theme name to switch to. - * @param array $args Not used. - */ - public function change($id, $args = []) - { - $this->update($id, $args); - } -} diff --git a/src/Driver/Element/Wpphp/DatabaseElement.php b/src/Driver/Element/Wpphp/DatabaseElement.php deleted file mode 100644 index efd6bf85..00000000 --- a/src/Driver/Element/Wpphp/DatabaseElement.php +++ /dev/null @@ -1,184 +0,0 @@ - ['pipe', 'w'], - 2 => ['pipe', 'w'], - ), - $pipes - ); - - $stdout = trim(stream_get_contents($pipes[1])); - $stderr = trim(stream_get_contents($pipes[2])); - fclose($pipes[1]); - fclose($pipes[2]); - $exit_code = proc_close($proc); - putenv('MYSQL_PWD=' . $old_pwd); - - // Sometimes the error message is in stderr. - if (! $stdout && $stderr) { - $stdout = $stderr; - } - - if ($exit_code || strpos($stdout, 'Warning: ') === 0 || strpos($stdout, 'Error: ') === 0) { - throw new RuntimeException( - sprintf( - "[W606] Could not export database in method %1\$s(): \n\n%2\$s.\n(%3\$s)", - debug_backtrace()[1]['function'], - $stdout, - $exit_code - ) - ); - } - - return $path; - } - - /** - * Import site database. - * - * @param int $id Not used. - * @param array $args - */ - public function update($id, $args = []) - { - $bin = ''; - $command_args = sprintf( - '--no-defaults --no-auto-rehash --host=%1$s --user=%2$s --database=%3$s --execute=%4$s', - DB_HOST, - DB_USER, - DB_NAME, - escapeshellarg(sprintf( - 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %1$s; COMMIT;', - $args['path'] - )) - ); - - $old_pwd = getenv('MYSQL_PWD'); - putenv('MYSQL_PWD=' . DB_PASSWORD); - - // Support Windows. - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { - $bin = '/usr/bin/env '; - } - - // Import DB via mysql-cli. - $proc = proc_open( - "{$bin}mysql {$command_args}", - array( - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'], - ), - $pipes - ); - - $stdout = trim(stream_get_contents($pipes[1])); - $stderr = trim(stream_get_contents($pipes[2])); - fclose($pipes[1]); - fclose($pipes[2]); - $exit_code = proc_close($proc); - putenv('MYSQL_PWD=' . $old_pwd); - - // Sometimes the error message is in stderr. - if (! $stdout && $stderr) { - $stdout = $stderr; - } - - if ($exit_code || strpos($stdout, 'Warning: ') === 0 || strpos($stdout, 'Error: ') === 0) { - throw new RuntimeException( - sprintf( - "[W607] Could not import database in method %1\$s(): \n\n%2\$s.\n(%3\$s)", - debug_backtrace()[1]['function'], - $stdout, - $exit_code - ) - ); - } - - /* - * clear the cache after restoration - this is probably only necessary - * because of some kind of global state issue - the WPCLI driver doensn't - * need it. There is some discussion about it here: - * - * https://github.com/paulgibbs/behat-wordpress-extension/pull/150 - */ - \wp_cache_flush(); - } - - - /* - * Convenience methods. - */ - - /** - * Alias of get(). - * - * @see get() - * - * @param int $id Not used. - * @param array $args - * - * @return string Path to the export file. - */ - public function export($id, $args = []) - { - return $this->get($id, $args); - } - - /** - * Alias of update(). - * - * @see update() - * - * @param int $id Not used. - * @param array $args - */ - public function import($id, $args = []) - { - $this->update($id, $args); - } -} diff --git a/src/Driver/Wpcli/Element/CacheElement.php b/src/Driver/Wpcli/Element/CacheElement.php new file mode 100644 index 00000000..3f8f89aa --- /dev/null +++ b/src/Driver/Wpcli/Element/CacheElement.php @@ -0,0 +1,31 @@ +driver = $driver; + } + + /** + * Clear object cache. + */ + public function clear() + { + $this->driver->wpcli('cache', 'flush'); + } +} diff --git a/src/Driver/Element/Wpcli/CommentElement.php b/src/Driver/Wpcli/Element/CommentElement.php similarity index 72% rename from src/Driver/Element/Wpcli/CommentElement.php rename to src/Driver/Wpcli/Element/CommentElement.php index d58f8d9f..d439a52f 100644 --- a/src/Driver/Element/Wpcli/CommentElement.php +++ b/src/Driver/Wpcli/Element/CommentElement.php @@ -1,16 +1,27 @@ driver = $driver; + } + /** * Create an item for this element. * @@ -29,7 +40,7 @@ public function create($args) ); array_unshift($wpcli_args, '--porcelain'); - $comment_id = (int) $this->drivers->getDriver()->wpcli('comment', 'create', $wpcli_args)['stdout']; + $comment_id = (int) $this->driver->wpcli('comment', 'create', $wpcli_args)['stdout']; return $this->get($comment_id); } @@ -55,7 +66,7 @@ public function get($id, $args = []) ); array_unshift($wpcli_args, $id, '--format=json'); - $comment = $this->drivers->getDriver()->wpcli('comment', 'get', $wpcli_args)['stdout']; + $comment = $this->driver->wpcli('comment', 'get', $wpcli_args)['stdout']; $comment = json_decode($comment); if (! $comment) { @@ -80,6 +91,6 @@ public function delete($id, $args = []) array_unshift($wpcli_args, $id); - $this->drivers->getDriver()->wpcli('comment', 'delete', $wpcli_args); + $this->driver->wpcli('comment', 'delete', $wpcli_args); } } diff --git a/src/Driver/Element/Wpcli/ContentElement.php b/src/Driver/Wpcli/Element/ContentElement.php similarity index 80% rename from src/Driver/Element/Wpcli/ContentElement.php rename to src/Driver/Wpcli/Element/ContentElement.php index 788c7432..cd1eaad3 100644 --- a/src/Driver/Element/Wpcli/ContentElement.php +++ b/src/Driver/Wpcli/Element/ContentElement.php @@ -1,16 +1,27 @@ driver = $driver; + } + /** * Create an item for this element. * @@ -56,7 +67,7 @@ public function create($args) ); array_unshift($wpcli_args, '--porcelain'); - $post_id = (int) $this->drivers->getDriver()->wpcli('post', 'create', $wpcli_args)['stdout']; + $post_id = (int) $this->driver->wpcli('post', 'create', $wpcli_args)['stdout']; // Apply taxonomy values. if ($tax_args) { @@ -70,7 +81,7 @@ public function create($args) escapeshellarg($split_term), ]; - $this->drivers->getDriver()->wpcli('post', 'term add', $args); + $this->driver->wpcli('post', 'term add', $args); } } } @@ -84,7 +95,7 @@ public function create($args) escapeshellarg($meta_value), ]; - $this->drivers->getDriver()->wpcli('post', 'meta update', $args)['stdout']; + $this->driver->wpcli('post', 'meta update', $args)['stdout']; } } @@ -108,7 +119,7 @@ public function get($id, $args = []) // Support fetching via arbitrary field. if (! is_numeric($id)) { $wpcli_args = ['--fields=ID,url', "--{$args['by']}=" . escapeshellarg($id), '--post_type=any', '--format=json']; - $result = json_decode($this->drivers->getDriver()->wpcli('post', 'list', $wpcli_args)['stdout']); + $result = json_decode($this->driver->wpcli('post', 'list', $wpcli_args)['stdout']); if (empty($result)) { throw new UnexpectedValueException(sprintf('[W501] Could not find post with ID %d', $id)); @@ -128,7 +139,7 @@ public function get($id, $args = []) ); array_unshift($wpcli_args, $id, '--format=json'); - $post = $this->drivers->getDriver()->wpcli('post', 'get', $wpcli_args)['stdout']; + $post = $this->driver->wpcli('post', 'get', $wpcli_args)['stdout']; $post = json_decode($post); if (! $post) { @@ -137,7 +148,7 @@ public function get($id, $args = []) if (! $url) { $wpcli_args = ['--post__in=' . $post->ID, '--fields=url', '--post_type=any', '--format=json']; - $result = json_decode($this->drivers->getDriver()->wpcli('post', 'list', $wpcli_args)['stdout']); + $result = json_decode($this->driver->wpcli('post', 'list', $wpcli_args)['stdout']); $url = $result[0]->url; } @@ -161,6 +172,6 @@ public function delete($id, $args = []) array_unshift($wpcli_args, $id); - $this->drivers->getDriver()->wpcli('post', 'delete', $wpcli_args); + $this->driver->wpcli('post', 'delete', $wpcli_args); } } diff --git a/src/Driver/Element/Wpcli/DatabaseElement.php b/src/Driver/Wpcli/Element/DatabaseElement.php similarity index 63% rename from src/Driver/Element/Wpcli/DatabaseElement.php rename to src/Driver/Wpcli/Element/DatabaseElement.php index 81ea86df..91f45edb 100644 --- a/src/Driver/Element/Wpcli/DatabaseElement.php +++ b/src/Driver/Wpcli/Element/DatabaseElement.php @@ -1,15 +1,29 @@ driver = $driver; + } + /** * Export site database. * @@ -18,7 +32,7 @@ class DatabaseElement extends BaseElement * * @return string Path to the database dump. */ - public function get($id, $args = []) + public function export($id, $args = []) : string { $wpcli_args = ['--porcelain', '--add-drop-table']; @@ -30,7 +44,7 @@ public function get($id, $args = []) }; // Protect against WP-CLI changing the filename. - $path = $this->drivers->getDriver()->wpcli('db', 'export', $wpcli_args)['stdout']; + $path = $this->driver->wpcli('db', 'export', $wpcli_args)['stdout']; if (! $path) { throw new RuntimeException('[W502] Could not export database'); } @@ -44,9 +58,9 @@ public function get($id, $args = []) * @param int $id Not used. * @param array $args */ - public function update($id, $args = []) + public function import($id, $args = []) { - $this->drivers->getDriver()->wpcli('db', 'import', [$args['path']]); + $this->driver->wpcli('db', 'import', [$args['path']]); /* * The WPPHP driver needs the WP cache flushed at this point. However @@ -57,37 +71,4 @@ public function update($id, $args = []) * https://github.com/paulgibbs/behat-wordpress-extension/pull/150 */ } - - - /* - * Convenience methods. - */ - - /** - * Alias of get(). - * - * @see get() - * - * @param int $id Not used. - * @param array $args - * - * @return string Path to the export file. - */ - public function export($id, $args = []) - { - return $this->get($id, $args); - } - - /** - * Alias of update(). - * - * @see update() - * - * @param int $id Not used. - * @param array $args - */ - public function import($id, $args = []) - { - $this->update($id, $args); - } } diff --git a/src/Driver/Element/Wpcli/PluginElement.php b/src/Driver/Wpcli/Element/PluginElement.php similarity index 60% rename from src/Driver/Element/Wpcli/PluginElement.php rename to src/Driver/Wpcli/Element/PluginElement.php index bc17ff82..b40ba108 100644 --- a/src/Driver/Element/Wpcli/PluginElement.php +++ b/src/Driver/Wpcli/Element/PluginElement.php @@ -1,26 +1,40 @@ driver = $driver; + } + /** * Activate or deactivate specified plugin. * * @param string $id Plugin name. * @param array $args Optional data used to update an object. */ - public function update($id, $args = []) + protected function update($id, $args = []) { - $this->drivers->getDriver()->wpcli('plugin', $args['status'], [$id]); + $this->driver->wpcli('plugin', $args['status'], [$id]); } - /* * Convenience methods. */ diff --git a/src/Driver/Element/Wpcli/TermElement.php b/src/Driver/Wpcli/Element/TermElement.php similarity index 72% rename from src/Driver/Element/Wpcli/TermElement.php rename to src/Driver/Wpcli/Element/TermElement.php index 43637a88..4315ad1e 100644 --- a/src/Driver/Element/Wpcli/TermElement.php +++ b/src/Driver/Wpcli/Element/TermElement.php @@ -1,16 +1,28 @@ driver = $driver; + } + /** * Create an item for this element. * @@ -30,7 +42,7 @@ public function create($args) ); array_unshift($wpcli_args, $args['taxonomy'], $args['term'], '--porcelain'); - $term_id = (int) $this->drivers->getDriver()->wpcli('term', 'create', $wpcli_args)['stdout']; + $term_id = (int) $this->driver->wpcli('term', 'create', $wpcli_args)['stdout']; return $this->get($term_id); } @@ -56,7 +68,7 @@ public function get($id, $args = []) ); array_unshift($wpcli_args, $args['taxonomy'], $id, '--format=json'); - $term = $this->drivers->getDriver()->wpcli('term', 'get', $wpcli_args)['stdout']; + $term = $this->driver->wpcli('term', 'get', $wpcli_args)['stdout']; $term = json_decode($term); if (! $term) { @@ -79,6 +91,6 @@ public function delete($id, $args = []) $id, ]; - $this->drivers->getDriver()->wpcli('term', 'delete', $wpcli_args); + $this->driver->wpcli('term', 'delete', $wpcli_args); } } diff --git a/src/Driver/Wpcli/Element/ThemeElement.php b/src/Driver/Wpcli/Element/ThemeElement.php new file mode 100644 index 00000000..69820335 --- /dev/null +++ b/src/Driver/Wpcli/Element/ThemeElement.php @@ -0,0 +1,34 @@ +driver = $driver; + } + + /** + * Switch active theme. + * + * @param string $id Theme name to switch to. + * @param array $args Not used. + */ + public function change($id, $args = []) + { + $this->driver->wpcli('theme', 'activate', [$id]); + } +} diff --git a/src/Driver/Element/Wpcli/UserElement.php b/src/Driver/Wpcli/Element/UserElement.php similarity index 82% rename from src/Driver/Element/Wpcli/UserElement.php rename to src/Driver/Wpcli/Element/UserElement.php index 391c33a5..12eaad74 100644 --- a/src/Driver/Element/Wpcli/UserElement.php +++ b/src/Driver/Wpcli/Element/UserElement.php @@ -1,17 +1,29 @@ driver = $driver; + } + /** * Create an item for this element. * @@ -31,7 +43,7 @@ public function create($args) ); array_unshift($wpcli_args, $args['user_login'], $args['user_email'], '--porcelain'); - $user_id = (int) $this->drivers->getDriver()->wpcli('user', 'create', $wpcli_args)['stdout']; + $user_id = (int) $this->driver->wpcli('user', 'create', $wpcli_args)['stdout']; return $this->get($user_id); } @@ -78,7 +90,7 @@ public function get($id, $args = []) ); array_unshift($wpcli_args, $id, '--format=json'); - $user = $this->drivers->getDriver()->wpcli('user', 'get', $wpcli_args)['stdout']; + $user = $this->driver->wpcli('user', 'get', $wpcli_args)['stdout']; $user = json_decode($user); if (! $user) { @@ -116,6 +128,6 @@ public function delete($id, $args = []) array_unshift($wpcli_args, $id, '--yes'); - $this->drivers->getDriver()->wpcli('user', 'delete', $wpcli_args); + $this->driver->wpcli('user', 'delete', $wpcli_args); } } diff --git a/src/Driver/Element/Wpcli/WidgetElement.php b/src/Driver/Wpcli/Element/WidgetElement.php similarity index 73% rename from src/Driver/Element/Wpcli/WidgetElement.php rename to src/Driver/Wpcli/Element/WidgetElement.php index 79352213..65a2c20f 100644 --- a/src/Driver/Element/Wpcli/WidgetElement.php +++ b/src/Driver/Wpcli/Element/WidgetElement.php @@ -1,17 +1,30 @@ driver = $driver; + } + /** * Adds a widget to the sidebar with the specified arguments * @@ -28,7 +41,7 @@ public function addToSidebar($widget_name, $sidebar_id, $args) $sidebar_id ], buildCLIArgs(array_keys($args), $args)); - $this->drivers->getDriver()->wpcli('widget', 'add', $wpcli_args); + $this->driver->wpcli('widget', 'add', $wpcli_args); } /** @@ -42,7 +55,7 @@ public function addToSidebar($widget_name, $sidebar_id, $args) */ public function getSidebar($sidebar_name) { - $registered_sidebars = json_decode($this->drivers->getDriver()->wpcli('sidebar', 'list', [ + $registered_sidebars = json_decode($this->driver->wpcli('sidebar', 'list', [ '--format=json', ])['stdout']); diff --git a/src/Driver/Wpcli/Element/WpcliBaseElement.php b/src/Driver/Wpcli/Element/WpcliBaseElement.php new file mode 100644 index 00000000..69c18ee2 --- /dev/null +++ b/src/Driver/Wpcli/Element/WpcliBaseElement.php @@ -0,0 +1,27 @@ +driver = $driver; + } + + protected function getDriver() : WpcliDriverInterface + { + return $this->driver; + } +} diff --git a/src/Driver/WpcliDriver.php b/src/Driver/Wpcli/WpcliDriver.php similarity index 72% rename from src/Driver/WpcliDriver.php rename to src/Driver/Wpcli/WpcliDriver.php index b69998b7..61eb5438 100644 --- a/src/Driver/WpcliDriver.php +++ b/src/Driver/Wpcli/WpcliDriver.php @@ -1,6 +1,6 @@ binary = $binary; } - /** - * Set up anything required for the driver. - * - * Called when the driver is used for the first time. - * Checks `core is-installed`, and the version number. - * - * @throws \RuntimeException - */ - public function bootstrap() - { - $version = ''; - - preg_match('#^WP-CLI (.*)$#', $this->wpcli('cli', 'version')['stdout'], $match); - if (! empty($match)) { - $version = array_pop($match); - } - - if (! version_compare($version, '1.5.0', '>=')) { - throw new RuntimeException('[W100] Your WP-CLI is too old; version 1.5.0 or newer is required.'); - } - - $status = $this->wpcli('core', 'is-installed')['exit_code']; - if ($status !== 0) { - throw new RuntimeException('[W101] WordPress does not seem to be installed. Check "path" and/or "alias" settings in behat.yml.'); - } - - putenv('WP_CLI_STRICT_ARGS_MODE=1'); - - $this->is_bootstrapped = true; - } - - /** - * Execute a WP-CLI command. - * - * @param string $command Command name. - * @param string $subcommand Subcommand name. - * @param string[] $raw_arguments Optional. Associative array of arguments for the command. - * - * @throws \UnexpectedValueException - * - * @return array { - * WP-CLI command results. - * - * @type string $stdout Response text from WP-CLI. - * @type int $exit_code Returned status code of the executed command. - * } - */ public function wpcli(string $command, string $subcommand, array $raw_arguments = []): array { $arguments = implode(' ', $raw_arguments); @@ -158,4 +105,21 @@ public function wpcli(string $command, string $subcommand, array $raw_arguments return compact('stdout', 'exit_code'); } + + public function bootstrap() + { + $version = ''; + preg_match('#^WP-CLI (.*)$#', $this->wpcli('cli', 'version')['stdout'], $match); + if (! empty($match)) { + $version = array_pop($match); + } + if (! version_compare($version, '1.5.0', '>=')) { + throw new RuntimeException('[W100] Your WP-CLI is too old; version 1.5.0 or newer is required.'); + } + $status = $this->wpcli('core', 'is-installed')['exit_code']; + if ($status !== 0) { + throw new RuntimeException('[W101] WordPress does not seem to be installed. Check "path" and/or "alias" settings in behat.yml.'); + } + putenv('WP_CLI_STRICT_ARGS_MODE=1'); + } } diff --git a/src/Driver/Wpcli/WpcliDriverInterface.php b/src/Driver/Wpcli/WpcliDriverInterface.php new file mode 100644 index 00000000..efbc1b10 --- /dev/null +++ b/src/Driver/Wpcli/WpcliDriverInterface.php @@ -0,0 +1,9 @@ +driver = $driver; + } + + /** + * setParameters + * + * @param ContainerBuilder $container + * @param array $config + */ + public static function setParameters(ContainerBuilder $container, array $config) + { + if (! isset($config['wpcli'])) { + throw new RuntimeException("Cannot find wpcli configuration in behat.yml\n"); + } + + $container->setAlias(CacheElementInterface::class, CacheElement::class); + $container->setAlias(CommentElementInterface::class, CommentElement::class); + $container->setAlias(ContentElementInterface::class, ContentElement::class); + $container->setAlias(DatabaseElementInterface::class, DatabaseElement::class); + $container->setAlias(PluginElementInterface::class, PluginElement::class); + $container->setAlias(TermElementInterface::class, TermElement::class); + $container->setAlias(ThemeElementInterface::class, ThemeElement::class); + $container->setAlias(UserElementInterface::class, UserElement::class); + $container->setAlias(WidgetElementInterface::class, WidgetElement::class); + + $container->setAlias(WpcliDriverInterface::class, WpcliDriver::class); + + $definition = $container->getDefinition(WpcliDriver::class); + + $config['wpcli']['alias'] = isset($config['wpcli']['alias']) ? $config['wpcli']['alias'] : ''; + $container->setParameter('wordpress.driver.wpcli.alias', $config['wpcli']['alias']); + + $config['wpcli']['binary'] = isset($config['wpcli']['binary']) ? $config['wpcli']['binary'] : null; + $container->setParameter('wordpress.driver.wpcli.binary', $config['wpcli']['binary']); + + $definition->addArgument('%wordpress.driver.wpcli.alias%'); // $alias + $definition->addArgument('%wordpress.site_url%'); // $url + $definition->addArgument('%wordpress.driver.wpcli.binary%'); // $binary + $definition->addArgument('%wordpress.path%'); // $path + } + + public static function getShortName(): string + { + return get_called_class()::SHORTNAME; + } + + public function isBootstrapped(): bool + { + return $this->is_bootstrapped; + } + + public function bootstrap() + { + $this->driver->bootstrap(); + $this->is_bootstrapped = true; + } +} diff --git a/src/Driver/Element/Wpphp/CacheElement.php b/src/Driver/Wpphp/Element/CacheElement.php similarity index 90% rename from src/Driver/Element/Wpphp/CacheElement.php rename to src/Driver/Wpphp/Element/CacheElement.php index da89fd08..05f47656 100644 --- a/src/Driver/Element/Wpphp/CacheElement.php +++ b/src/Driver/Wpphp/Element/CacheElement.php @@ -1,6 +1,6 @@ [ + 'pipe', + 'w' + ], + 2 => [ + 'pipe', + 'w' + ] + ), $pipes); + + $stdout = trim(stream_get_contents($pipes[1])); + $stderr = trim(stream_get_contents($pipes[2])); + fclose($pipes[1]); + fclose($pipes[2]); + $exit_code = proc_close($proc); + putenv('MYSQL_PWD=' . $old_pwd); + + // Sometimes the error message is in stderr. + if (! $stdout && $stderr) { + $stdout = $stderr; + } + + if ($exit_code || strpos($stdout, 'Warning: ') === 0 || strpos($stdout, 'Error: ') === 0) { + throw new RuntimeException(sprintf("[W606] Could not export database in method %1\$s(): \n\n%2\$s.\n(%3\$s)", debug_backtrace()[1]['function'], $stdout, $exit_code)); + } + + return $path; + } + + /** + * Import site database. + * + * @param int $id + * Not used. + * @param array $args + */ + public function import($id, $args = []) + { + $bin = ''; + $command_args = sprintf('--no-defaults --no-auto-rehash --host=%1$s --user=%2$s --database=%3$s --execute=%4$s', DB_HOST, DB_USER, DB_NAME, escapeshellarg(sprintf('SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %1$s; COMMIT;', $args['path']))); + + $old_pwd = getenv('MYSQL_PWD'); + putenv('MYSQL_PWD=' . DB_PASSWORD); + + // Support Windows. + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + $bin = '/usr/bin/env '; + } + + // Import DB via mysql-cli. + $proc = proc_open("{$bin}mysql {$command_args}", array( + 1 => [ + 'pipe', + 'w' + ], + 2 => [ + 'pipe', + 'w' + ] + ), $pipes); + + $stdout = trim(stream_get_contents($pipes[1])); + $stderr = trim(stream_get_contents($pipes[2])); + fclose($pipes[1]); + fclose($pipes[2]); + $exit_code = proc_close($proc); + putenv('MYSQL_PWD=' . $old_pwd); + + // Sometimes the error message is in stderr. + if (! $stdout && $stderr) { + $stdout = $stderr; + } + + if ($exit_code || strpos($stdout, 'Warning: ') === 0 || strpos($stdout, 'Error: ') === 0) { + throw new RuntimeException(sprintf("[W607] Could not import database in method %1\$s(): \n\n%2\$s.\n(%3\$s)", debug_backtrace()[1]['function'], $stdout, $exit_code)); + } + + /* + * clear the cache after restoration - this is probably only necessary + * because of some kind of global state issue - the WPCLI driver doensn't + * need it. There is some discussion about it here: + * + * https://github.com/paulgibbs/behat-wordpress-extension/pull/150 + */ + \wp_cache_flush(); + } +} diff --git a/src/Driver/Element/Wpphp/PluginElement.php b/src/Driver/Wpphp/Element/PluginElement.php similarity index 88% rename from src/Driver/Element/Wpphp/PluginElement.php rename to src/Driver/Wpphp/Element/PluginElement.php index 421243a8..4395d905 100644 --- a/src/Driver/Element/Wpphp/PluginElement.php +++ b/src/Driver/Wpphp/Element/PluginElement.php @@ -1,14 +1,15 @@ find('css', '.wp-menu-name')->getHtml()); + $item_name = stripTagsAndContent($first_level_item->find('css', '.wp-menu-name')->getHtml()); if (strtolower($items[0]) !== strtolower($item_name)) { continue; @@ -51,7 +51,7 @@ public function clickMenuItem(string $item) $second_level_items = $first_level_item->findAll('css', 'ul li a'); foreach ($second_level_items as $second_level_item) { - $item_name = Util\stripTagsAndContent($second_level_item->getHtml()); + $item_name = stripTagsAndContent($second_level_item->getHtml()); if (strtolower($items[1]) !== strtolower($item_name)) { continue; } diff --git a/src/ServiceContainer/WordpressBehatExtension.php b/src/ServiceContainer/WordpressBehatExtension.php index 42f0c5a6..26263b88 100644 --- a/src/ServiceContainer/WordpressBehatExtension.php +++ b/src/ServiceContainer/WordpressBehatExtension.php @@ -1,39 +1,47 @@ children() - // Common settings. + // Common settings. ->enumNode('default_driver') // "wpapi" is for backwards compatibility; means "wpphp". - ->values(['wpcli', 'wpapi', 'wpphp', 'blackbox']) + ->values(['wpcli','wpapi','wpphp','blackbox']) ->defaultValue('wpcli') ->end() ->scalarNode('path') ->defaultValue('') ->end() - // WordPress' "siteurl" option. - ->scalarNode('site_url')->defaultValue('%mink.base_url%')->end() + // WordPress' "siteurl" option. + ->scalarNode('site_url') + ->defaultValue('%mink.base_url%') + ->end() - // Account roles -> username/password. + // Account roles -> username/password. ->arrayNode('users') ->addDefaultsIfNotSet() ->children() @@ -102,7 +114,6 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() ->end() - ->arrayNode('editor') ->addDefaultsIfNotSet() ->children() @@ -114,7 +125,6 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() ->end() - ->arrayNode('author') ->addDefaultsIfNotSet() ->children() @@ -126,7 +136,6 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() ->end() - ->arrayNode('contributor') ->addDefaultsIfNotSet() ->children() @@ -138,7 +147,6 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() ->end() - ->arrayNode('subscriber') ->addDefaultsIfNotSet() ->children() @@ -153,32 +161,33 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() - // WP-CLI driver. + // WP-CLI driver. ->arrayNode('wpcli') ->addDefaultsIfNotSet() ->children() - ->scalarNode('alias')->end() + ->scalarNode('alias') + ->end() ->scalarNode('binary') ->defaultValue('wp') ->end() ->end() ->end() - // WordPress PHP driver. + // WordPress PHP driver. ->arrayNode('wpphp') ->addDefaultsIfNotSet() ->children() ->end() ->end() - // Blackbox driver. + // Blackbox driver. ->arrayNode('blackbox') ->addDefaultsIfNotSet() ->children() ->end() ->end() - // Database management. + // Database management. ->arrayNode('database') ->addDefaultsIfNotSet() ->children() @@ -190,7 +199,7 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() - // Permalink patterns. + // Permalink patterns. ->arrayNode('permalinks') ->addDefaultsIfNotSet() ->children() @@ -200,7 +209,7 @@ public function configure(ArrayNodeDefinition $builder) ->end() ->end() - // Internal use only. Don't use it. Or else. + // Internal use only. Don't use it. Or else. ->arrayNode('internal') ->addDefaultsIfNotSet() ->end() @@ -216,21 +225,35 @@ public function configure(ArrayNodeDefinition $builder) */ public function load(ContainerBuilder $container, array $config) { + $container->registerForAutoconfiguration(DriverManagerInterface::class)->addTag('wordpress.driver'); + $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/config')); $loader->load('services.yml'); + $this->setParameters($container, $config); + + $container->addCompilerPass(new DriverPass()); + $container->addCompilerPass(new DriverElementPass()); + $container->addCompilerPass(new EventSubscriberPass()); + } + + /** + * Register settings with symfony + * + * @param ContainerBuilder $container + * @param array $config + */ + protected function setParameters(ContainerBuilder $container, array $config) + { // Backwards compatibility for pre-1.0. Will be removed in 2.0. if ($config['default_driver'] === 'wpapi') { $config['default_driver'] = 'wpphp'; } - $container->setParameter('wordpress.wordpress.default_driver', $config['default_driver']); + $container->setParameter('wordpress.site_url', $config['site_url']); + $container->setParameter('wordpress.default_driver', $config['default_driver']); $container->setParameter('wordpress.path', $config['path']); $container->setParameter('wordpress.parameters', $config); - - $this->setupWpcliDriver($loader, $container, $config); - $this->setupWpphpDriver($loader, $container, $config); - $this->setupBlackboxDriver($loader, $container, $config); } /** @@ -245,15 +268,11 @@ protected function setupWpcliDriver(FileLoader $loader, ContainerBuilder $contai if (! isset($config['wpcli'])) { return; } - - $loader->load('drivers/wpcli.yml'); - + // $loader->load('drivers/wpcli.yml'); $config['wpcli']['alias'] = isset($config['wpcli']['alias']) ? $config['wpcli']['alias'] : ''; $container->setParameter('wordpress.driver.wpcli.alias', $config['wpcli']['alias']); - $config['wpcli']['path'] = isset($config['path']) ? $config['path'] : ''; $container->setParameter('wordpress.driver.wpcli.path', $config['path']); - $config['wpcli']['binary'] = isset($config['wpcli']['binary']) ? $config['wpcli']['binary'] : null; $container->setParameter('wordpress.driver.wpcli.binary', $config['wpcli']['binary']); } @@ -267,14 +286,10 @@ protected function setupWpcliDriver(FileLoader $loader, ContainerBuilder $contai */ protected function setupWpphpDriver(FileLoader $loader, ContainerBuilder $container, array $config) { - if (! isset($config['wpphp'])) { - return; - } - $loader->load('drivers/wpphp.yml'); $config['wpphp']['path'] = isset($config['path']) ? $config['path'] : ''; - $container->setParameter('wordpress.driver.wpphp.path', $config['wpphp']['path']); + $container->setParameter('wordpress.driver.path', $config['wpphp']['path']); } /** @@ -300,46 +315,51 @@ protected function setupBlackboxDriver(FileLoader $loader, ContainerBuilder $con */ public function process(ContainerBuilder $container) { - $this->processDriverPass($container); - $this->processDriverElementPass($container); - $this->processEventSubscriberPass($container); - $this->processClassGenerator($container); - $this->setPageObjectNamespaces($container); $this->injectSiteUrlIntoPageObjects($container); + $this->listServicesAndTags($container); } /** - * Set up driver registration. + * Helper method for debugging the compiler passes. * - * @param ContainerBuilder $container + * When called all the services are sent to stdout. */ - protected function processDriverPass(ContainerBuilder $container) + private function listServicesAndTags(ContainerBuilder $container) { - $driver = new DriverPass(); - $driver->process($container); - } - - /** - * Set up driver extension registration. - * - * @param ContainerBuilder $container - */ - protected function processDriverElementPass(ContainerBuilder $container) - { - $driver = new DriverElementPass(); - $driver->process($container); - } - - /** - * Process the Event Subscriber Pass. - * - * @param ContainerBuilder $container - */ - private function processEventSubscriberPass(ContainerBuilder $container) - { - $event = new EventSubscriberPass(); - $event->process($container); + $serviceIds = $container->getServiceIds(); + + foreach ($serviceIds as $serviceId) { + echo "\tServiceId: " . $serviceId, ','; + try { + $definition = $container->getDefinition($serviceId); + $class = $definition->getClass(); + echo 'Class: ' . $class, ','; + $tags = $definition->getTags(); + if ($tags) { + $tagInformation = array(); + foreach ($tags as $tagName => $tagData) { + echo "[$tagName"; + foreach ($tagData as $tagParameters) { + $parameters = array_map(function ($key, $value) { + return sprintf('%s: %s', $key, $value); + }, array_keys($tagParameters), array_values($tagParameters)); + + $parameters = implode(', ', $parameters); + if ('' !== $parameters) { + $tagInformation[] = sprintf('(%s)', $parameters); + } + } + echo implode(',', $tagInformation) . ']'; + } + } + echo "\n"; + } catch (ServiceNotFoundException $e) { + echo "\n"; + continue; + } + // print_r(array_merge( class_parents("$class"), class_implements("$class"))); + } } /** @@ -351,6 +371,7 @@ private function processEventSubscriberPass(ContainerBuilder $container) */ protected function processClassGenerator(ContainerBuilder $container) { + echo __FUNCTION__ . "\n"; $definition = new Definition('PaulGibbs\WordpressBehatExtension\Context\ContextClass\ClassGenerator'); $container->setDefinition(ContextExtension::CLASS_GENERATOR_TAG . '.simple', $definition); } diff --git a/src/ServiceContainer/config/drivers/blackbox.yml b/src/ServiceContainer/config/drivers/blackbox.yml deleted file mode 100755 index 556c1588..00000000 --- a/src/ServiceContainer/config/drivers/blackbox.yml +++ /dev/null @@ -1,8 +0,0 @@ -parameters: - wordpress.driver.blackbox.class: PaulGibbs\WordpressBehatExtension\Driver\BlackboxDriver - -services: - wordpress.driver.blackbox: - class: "%wordpress.driver.blackbox.class%" - tags: - - { name: wordpress.driver, alias: blackbox } diff --git a/src/ServiceContainer/config/drivers/wpcli.yml b/src/ServiceContainer/config/drivers/wpcli.yml deleted file mode 100755 index 96fee3b9..00000000 --- a/src/ServiceContainer/config/drivers/wpcli.yml +++ /dev/null @@ -1,85 +0,0 @@ -parameters: - wordpress.driver.wpcli.class: PaulGibbs\WordpressBehatExtension\Driver\WpcliDriver - wordpress.element.wpcli.cache.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\CacheElement - wordpress.element.wpcli.comment.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\CommentElement - wordpress.element.wpcli.content.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\ContentElement - wordpress.element.wpcli.database.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\DatabaseElement - wordpress.element.wpcli.plugin.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\PluginElement - wordpress.element.wpcli.term.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\TermElement - wordpress.element.wpcli.theme.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\ThemeElement - wordpress.element.wpcli.user.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\UserElement - wordpress.element.wpcli.widget.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpcli\WidgetElement - -services: - wordpress.driver.wpcli: - class: "%wordpress.driver.wpcli.class%" - arguments: - - "%wordpress.driver.wpcli.alias%" - - "%mink.base_url%" - - "%wordpress.driver.wpcli.binary%" - - "%wordpress.path%" - tags: - - { name: wordpress.driver, alias: wpcli } - - wordpress.element.wpcli.cache: - class: "%wordpress.element.wpcli.cache.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: cache, driver: wpcli } - - wordpress.element.wpcli.comment: - class: "%wordpress.element.wpcli.comment.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: comment, driver: wpcli } - - wordpress.element.wpcli.content: - class: "%wordpress.element.wpcli.content.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: content, driver: wpcli } - - wordpress.element.wpcli.database: - class: "%wordpress.element.wpcli.database.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: database, driver: wpcli } - - wordpress.element.wpcli.plugin: - class: "%wordpress.element.wpcli.plugin.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: plugin, driver: wpcli } - - wordpress.element.wpcli.term: - class: "%wordpress.element.wpcli.term.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: term, driver: wpcli } - - wordpress.element.wpcli.theme: - class: "%wordpress.element.wpcli.theme.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: theme, driver: wpcli } - - wordpress.element.wpcli.user: - class: "%wordpress.element.wpcli.user.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: user, driver: wpcli } - - wordpress.element.wpcli.widget: - class: "%wordpress.element.wpcli.widget.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: widget, driver: wpcli } diff --git a/src/ServiceContainer/config/drivers/wpphp.yml b/src/ServiceContainer/config/drivers/wpphp.yml deleted file mode 100755 index 7454a726..00000000 --- a/src/ServiceContainer/config/drivers/wpphp.yml +++ /dev/null @@ -1,82 +0,0 @@ -parameters: - wordpress.driver.wpphp.class: PaulGibbs\WordpressBehatExtension\Driver\WpphpDriver - wordpress.element.wpphp.cache.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\CacheElement - wordpress.element.wpphp.comment.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\CommentElement - wordpress.element.wpphp.content.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\ContentElement - wordpress.element.wpphp.database.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\DatabaseElement - wordpress.element.wpphp.plugin.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\PluginElement - wordpress.element.wpphp.term.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\TermElement - wordpress.element.wpphp.theme.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\ThemeElement - wordpress.element.wpphp.user.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\UserElement - wordpress.element.wpphp.widget.class: PaulGibbs\WordpressBehatExtension\Driver\Element\Wpphp\WidgetElement - -services: - wordpress.driver.wpphp: - class: "%wordpress.driver.wpphp.class%" - arguments: - - "%wordpress.path%" - tags: - - { name: wordpress.driver, alias: wpphp } - - wordpress.element.wpphp.cache: - class: "%wordpress.element.wpphp.cache.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: cache, driver: wpphp } - - wordpress.element.wpphp.comment: - class: "%wordpress.element.wpphp.comment.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: , driver: wpphp } - - wordpress.element.wpphp.content: - class: "%wordpress.element.wpphp.content.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: content, driver: wpphp } - - wordpress.element.wpphp.database: - class: "%wordpress.element.wpphp.database.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: database, driver: wpphp } - - wordpress.element.wpphp.plugin: - class: "%wordpress.element.wpphp.plugin.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: plugin, driver: wpphp } - - wordpress.element.wpphp.term: - class: "%wordpress.element.wpphp.term.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: term, driver: wpphp } - - wordpress.element.wpphp.theme: - class: "%wordpress.element.wpphp.theme.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: theme, driver: wpphp } - - wordpress.element.wpphp.user: - class: "%wordpress.element.wpphp.user.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: user, driver: wpphp } - - wordpress.element.wpphp.widget: - class: "%wordpress.element.wpphp.widget.class%" - arguments: - - "@wordpress.wordpress" - tags: - - { name: wordpress.element, alias: widget, driver: wpphp } diff --git a/src/ServiceContainer/config/services.yml b/src/ServiceContainer/config/services.yml index 75791fe6..0dc59958 100755 --- a/src/ServiceContainer/config/services.yml +++ b/src/ServiceContainer/config/services.yml @@ -3,17 +3,27 @@ parameters: wordpress.parameters: {} services: + # default configuration for services in *this* file + _defaults: + autowire: true + autoconfigure: true + public: false + PaulGibbs\WordpressBehatExtension\Driver\: + resource: '../../Driver/*' + public: true wordpress.wordpress: - class: 'PaulGibbs\WordpressBehatExtension\WordpressDriverManager' - arguments: - - {} + class: 'PaulGibbs\WordpressBehatExtension\WordpressDriverManager' + arguments: + - {} wordpress.context.initializer: - class: 'PaulGibbs\WordpressBehatExtension\Context\Initialiser\WordpressAwareInitialiser' - arguments: - - "@wordpress.wordpress" - - "%wordpress.parameters%" - tags: - - { name: context.initializer } + class: 'PaulGibbs\WordpressBehatExtension\Context\Initialiser\WordpressAwareInitialiser' + arguments: + $wordpress: "@wordpress.wordpress" + $parameters: "%wordpress.parameters%" + $pluginElement: '@PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\PluginElementInterface' + $databaseElement: '@PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\DatabaseElementInterface' + tags: + - { name: context.initializer } wordpress.listener.driver: class: 'PaulGibbs\WordpressBehatExtension\Listener\DriverListener' arguments: diff --git a/src/WordpressDriverManager.php b/src/WordpressDriverManager.php index b8e98d9a..88bbd2db 100644 --- a/src/WordpressDriverManager.php +++ b/src/WordpressDriverManager.php @@ -2,9 +2,9 @@ declare(strict_types=1); namespace PaulGibbs\WordpressBehatExtension; -use PaulGibbs\WordpressBehatExtension\Driver\DriverInterface; -use PaulGibbs\WordpressBehatExtension\Driver\Element\ElementInterface; - +use Behat\Mink\Driver\DriverInterface; +use PaulGibbs\WordpressBehatExtension\Driver\DriverManagerInterface; +use PaulGibbs\WordpressBehatExtension\Driver\Element\Interfaces\ElementInterface; use InvalidArgumentException; /** @@ -55,8 +55,9 @@ public function __construct(array $drivers = []) * @param string $name Driver name. * @param DriverInterface $driver An instance of a DriverInterface. */ - public function registerDriver(string $name, DriverInterface $driver) + public function registerDriver(string $name, DriverManagerInterface $driver) { + print("Registering driver: $name"); $name = strtolower($name); $this->drivers[$name] = $driver; } @@ -83,11 +84,9 @@ public function registerDriverElement(string $name, ElementInterface $element, s * @param string $name Optional. The name of the driver to return. If omitted, the default driver is returned. * @param string $bootstrap Optional. If "skip bootstrap", driver bootstrap is skipped. Default: "do bootstrap". * - * @return DriverInterface The requested driver. - * * @throws \InvalidArgumentException */ - public function getDriver(string $name = '', string $bootstrap = 'do bootstrap'): DriverInterface + public function getDriver(string $name = '', string $bootstrap = 'do bootstrap') { $do_bootstrap = ($bootstrap === 'do bootstrap'); $name = $name ? strtolower($name) : $this->default_driver; @@ -127,9 +126,9 @@ public function setDefaultDriverName($name) { $name = strtolower($name); - if (! isset($this->drivers[$name])) { + /* if (! isset($this->drivers[$name])) { throw new InvalidArgumentException("[W002] Driver '{$name}' is not registered."); - } + } */ $this->default_driver = $name; }