Skip to content

Commit

Permalink
Merge pull request #30 from Charlie-Lucas/feature/add-link-content-va…
Browse files Browse the repository at this point in the history
…lidator

Add link content validator
  • Loading branch information
Charlie-Lucas authored May 3, 2017
2 parents 4c562f8 + 295e7b4 commit c93dcb9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
75 changes: 75 additions & 0 deletions src/Troopers/BehatContexts/ContentValidator/LinkValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Troopers\BehatContexts\ContentValidator;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DomCrawler\Crawler;

/**
* Class LinkValidator.
*/
class LinkValidator implements ContentValidatorInterface, ContainerAwareInterface
{
/** @var Router $router */
private $router;

/**
* Sets the container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function setContainer(ContainerInterface $container = null)
{
$this->router = $container->get('router');
}

/**
* @param mixed $value
*
* @throws \Troopers\BehatContexts\ContentValidator\ContentValidatorException
*/
public function supports($value)
{
if (!is_array($value)) {
throw new ContentValidatorException(sprintf('To define a link you need to use an array, value given (%s)', json_encode($value)));
} else {
if (!isset($value['link']) && !$value['link']) {
throw new ContentValidatorException(sprintf('To define a link you need to set "link", value given (%s)', json_encode($value)));
}
if (!isset($value['url']) && (!isset($value['route']) && $value['route'] && !isset($value['parameters']) && !is_array($value['parameters']))) {
throw new ContentValidatorException(sprintf('To define a link you need to set "url" or "route" and "parameters" array, value given (%s)', json_encode($value)));
}
}
}

/**
* @param array $value
* @param string $content
*
* @throws \InvalidArgumentException
*
* @return mixed
*/
public function valid($value = [], $content = '')
{
$link = $value['link'];
if (isset($value['url'])) {
$url = $value['url'];
} else {
$url = $this->router->generate($value['route'], $value['parameters']);
}
$crawler = new Crawler($content);
$mailLink = $crawler->selectLink($value['link']);
if (!$mailLink->html()) {
throw new \InvalidArgumentException(sprintf('Unable to find a link for "%s" link ', $link));
}
if (false === strpos($url, $mailLink->attr('href'))) {
throw new \InvalidArgumentException(sprintf('Unable to match link "%s" with "%s"', $url, $mailLink->attr('href')));
}
}
}
20 changes: 18 additions & 2 deletions src/Troopers/BehatContexts/Context/MailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Symfony\Component\HttpKernel\KernelInterface;
use Troopers\BehatContexts\Collection\MailCollection;
use Troopers\BehatContexts\Mail\MailChecker;

/**
* Class MailContext.
*/
class MailContext extends RawMinkContext implements Context
class MailContext extends RawMinkContext implements Context, KernelAwareContext
{
protected $kernel;
protected $container;
/** @var MailCollection */
protected $mailCollection;
/** @var MailChecker */
Expand All @@ -22,13 +24,27 @@ class MailContext extends RawMinkContext implements Context
/**
* @param \Troopers\BehatContexts\Collection\MailCollection $mailCollection
* @param \Troopers\BehatContexts\Mail\MailChecker $mailChecker
*
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function initialize(MailCollection $mailCollection, MailChecker $mailChecker)
{
$this->mailCollection = $mailCollection;
$mailChecker->setContainer($this->container);
$this->mailChecker = $mailChecker;
}

/**
* Sets Kernel instance.
*
* @param KernelInterface $kernel
*/
public function setKernel(KernelInterface $kernel)
{
$this->container = $kernel->getContainer();
}

/**
* @Then I should have the email :event with:
*
Expand Down
21 changes: 20 additions & 1 deletion src/Troopers/BehatContexts/Mail/MailChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Alex\MailCatcher\Client;
use Alex\MailCatcher\Message;
use Behat\Mink\Selector\NamedSelector;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DomCrawler\Crawler;
use Troopers\BehatContexts\Component\ConfigTranslator;
use Troopers\BehatContexts\ContentValidator\ContentValidatorInterface;
Expand All @@ -13,12 +15,13 @@
/**
* Class MailChecker.
*/
class MailChecker
class MailChecker implements ContainerAwareInterface
{
private $configTranslator;
private $mailConfig;
private $mailcatcherClient;
private $contentValidatorChain;
private $container;

/**
* MailChecker constructor.
Expand All @@ -36,6 +39,19 @@ public function __construct(ConfigTranslator $configTranslator, $mailConfig, Cli
$this->contentValidatorChain = $contentValidatorChain;
}

/**
* Sets the container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* @param array $mail
* @param array $values
Expand Down Expand Up @@ -98,6 +114,9 @@ private function build(array $mail, array $values)

foreach ($contentsToTest as $value) {
$contentValidator->supports($value);
if ($contentValidator instanceof ContainerAwareInterface) {
$contentValidator->setContainer($this->container);
}
$contentValidator->valid($value, $content);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Troopers/BehatContexts/services/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ services:
troopers.behatcontexts.content_validator.table:
class: Troopers\BehatContexts\ContentValidator\TableValidator
tags:
- { name: troopers.behatcontexts.content_validator, contentType: 'tables' }
- { name: troopers.behatcontexts.content_validator, contentType: 'tables' }

troopers.behatcontexts.content_validator.link:
class: Troopers\BehatContexts\ContentValidator\LinkValidator
tags:
- { name: troopers.behatcontexts.content_validator, contentType: 'links' }

0 comments on commit c93dcb9

Please sign in to comment.