[Taxon] Implement removing images of a taxon

This commit is contained in:
Grzegorz Sadowski 2016-09-07 23:19:50 +02:00
parent b6461303c5
commit 5d8ac19fad
6 changed files with 173 additions and 12 deletions

View file

@ -9,7 +9,7 @@ Feature: Removing images of an existing taxon
And the store classifies its products as "T-Shirts"
And I am logged in as an administrator
@todo
@ui @javascript
Scenario: Removing a single image of a taxon
Given the "T-Shirts" taxon has an image "t-shirts.jpg" with a code "banner"
And I want to modify the "T-Shirts" taxon
@ -18,7 +18,7 @@ Feature: Removing images of an existing taxon
Then I should be notified that it has been successfully edited
And this taxon should not have images
@todo
@ui @javascript
Scenario: Removing all images of a taxon
Given the "T-Shirts" taxon has an image "t-shirts.jpg" with a code "banner"
And the "T-Shirts" taxon has also an image "t-shirts.jpg" with a code "thumbnail"
@ -29,7 +29,7 @@ Feature: Removing images of an existing taxon
Then I should be notified that it has been successfully edited
And this taxon should not have images
@todo
@ui @javascript
Scenario: Removing only one image of a taxon
Given the "T-Shirts" taxon has an image "t-shirts.jpg" with a code "banner"
And the "T-Shirts" taxon has also an image "t-shirts.jpg" with a code "thumbnail"
@ -40,12 +40,12 @@ Feature: Removing images of an existing taxon
And this taxon should have an image with a code "thumbnail"
But this taxon should not have an image with a code "banner"
@todo
@ui @javascript
Scenario: Adding multiple images and removing a single image of a taxon
Given I want to modify the "T-Shirts" taxon
When I attach "t-shirts.jpg" image with a code "banner"
And I attach "t-shirts.jpg" image with a code "thumbnail"
And I remove an image with a code "banner"
And I remove the first image
And I save my changes
Then I should be notified that it has been successfully edited
And this taxon should have an image with a code "thumbnail"

View file

@ -12,11 +12,14 @@
namespace Sylius\Behat\Context\Setup;
use Behat\Behat\Context\Context;
use Behat\Mink\Element\NodeElement;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
@ -33,24 +36,48 @@ final class TaxonomyContext implements Context
*/
private $taxonFactory;
/**
* @var FactoryInterface
*/
private $taxonImageFactory;
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var ImageUploaderInterface
*/
private $imageUploader;
/**
* @var array
*/
private $minkParameters;
/**
* @param RepositoryInterface $taxonRepository
* @param FactoryInterface $taxonFactory
* @param FactoryInterface $taxonImageFactory
* @param ObjectManager $objectManager
* @param ImageUploaderInterface $imageUploader
* @param array $minkParameters
*/
public function __construct(
RepositoryInterface $taxonRepository,
FactoryInterface $taxonFactory,
ObjectManager $objectManager
FactoryInterface $taxonImageFactory,
ObjectManager $objectManager,
ImageUploaderInterface $imageUploader,
array $minkParameters
) {
$this->taxonRepository = $taxonRepository;
$this->taxonFactory = $taxonFactory;
$this->taxonImageFactory = $taxonImageFactory;
$this->objectManager = $objectManager;
$this->imageUploader = $imageUploader;
$this->minkParameters = $minkParameters;
}
/**
@ -85,6 +112,23 @@ final class TaxonomyContext implements Context
$this->objectManager->flush($product);
}
/**
* @Given /^the ("[^"]+" taxon) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/
*/
public function theTaxonHasAnImageWithACode(TaxonInterface $taxon, $imagePath, $imageCode)
{
$filesPath = $this->getParameter('files_path');
$taxonImage = $this->taxonImageFactory->createNew();
$taxonImage->setFile(new UploadedFile($filesPath.$imagePath, basename($imagePath)));
$taxonImage->setCode($imageCode);
$this->imageUploader->upload($taxonImage);
$taxon->addImage($taxonImage);
$this->objectManager->flush($taxon);
}
/**
* @param string $name
*
@ -108,4 +152,14 @@ final class TaxonomyContext implements Context
{
return str_replace([' ', '-'], '_', strtolower($name));
}
/**
* @param string $name
*
* @return NodeElement
*/
private function getParameter($name)
{
return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null;
}
}

View file

@ -323,4 +323,43 @@ final class ManagingTaxonsContext implements Context
sprintf('Image with a code %s should have been displayed.', $code)
);
}
/**
* @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/
*/
public function thisTaxonShouldNotHaveAnImageWithCode($code)
{
Assert::false(
$this->updatePage->isImageWithCodeDisplayed($code),
sprintf('Image with a code %s should not have been displayed.', $code)
);
}
/**
* @When /^I remove(?:| also) an image with a code "([^"]*)"$/
*/
public function iRemoveAnImageWithACode($code)
{
$this->updatePage->removeImageWithCode($code);
}
/**
* @When I remove the first image
*/
public function iRemoveTheFirstImage()
{
$this->updatePage->removeFirstImage();
}
/**
* @Then this taxon should not have images
*/
public function thisTaxonShouldNotHaveImages()
{
Assert::eq(
0,
$this->updatePage->countImages(),
'This taxon has %2$s, but it should not have.'
);
}
}

View file

@ -74,8 +74,13 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
*/
public function isImageWithCodeDisplayed($code)
{
$imageUrl = $this->getImageElementByCode($code)->find('css', 'img')->getAttribute('src');
$imageElement = $this->getImageElementByCode($code);
if (null === $imageElement) {
return false;
}
$imageUrl = $imageElement->find('css', 'img')->getAttribute('src');
$this->getDriver()->visit($imageUrl);
$pageText = $this->getDocument()->getText();
$this->getDriver()->back();
@ -83,6 +88,31 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
return false === stripos($pageText, '404 Not Found');
}
/**
* {@inheritdoc}
*/
public function removeImageWithCode($code)
{
$imageElement = $this->getImageElementByCode($code);
$imageElement->clickLink('Delete');
}
public function removeFirstImage()
{
$imageElement = $this->getFirstImageElement();
$imageElement->clickLink('Delete');
}
/**
* {@inheritdoc}
*/
public function countImages()
{
$imageElements = $this->getImageElements();
return count($imageElements);
}
/**
* @return NodeElement
*/
@ -111,10 +141,29 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
*/
private function getLastImageElement()
{
$images = $this->getElement('images');
$items = $images->findAll('css', 'div[data-form-collection="item"]');
$imageElements = $this->getImageElements();
return end($items);
return end($imageElements);
}
/**
* @return NodeElement
*/
private function getFirstImageElement()
{
$imageElements = $this->getImageElements();
return reset($imageElements);
}
/**
* @return NodeElement[]
*/
private function getImageElements()
{
$images = $this->getElement('images');
return $images->findAll('css', 'div[data-form-collection="item"]');
}
/**
@ -125,8 +174,12 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
private function getImageElementByCode($code)
{
$images = $this->getElement('images');
$item = $images->find('css', 'input[value="'.$code.'"]')->getParent()->getParent();
$inputCode = $images->find('css', 'input[value="'.$code.'"]');
return $item;
if (null === $inputCode) {
return null;
}
return $inputCode->getParent()->getParent()->getParent();
}
}

View file

@ -59,4 +59,16 @@ interface UpdatePageInterface extends BaseUpdatePageInterface
* @return bool
*/
public function isImageWithCodeDisplayed($code);
/**
* @param string $code
*/
public function removeImageWithCode($code);
public function removeFirstImage();
/**
* @return int
*/
public function countImages();
}

View file

@ -187,7 +187,10 @@
<service id="sylius.behat.context.setup.taxonomy" class="Sylius\Behat\Context\Setup\TaxonomyContext" scope="scenario">
<argument type="service" id="sylius.repository.taxon" container="symfony" />
<argument type="service" id="sylius.factory.taxon" container="symfony" />
<argument type="service" id="sylius.factory.taxon_image" container="symfony" />
<argument type="service" id="doctrine.orm.entity_manager" container="symfony" />
<argument type="service" id="sylius.image_uploader" container="symfony" />
<argument>%mink.parameters%</argument>
<tag name="sylius.behat.context" />
</service>