<?php
namespace Project\Model\Component\Action;
use Behat\Mink\Element\NodeElement;
/**
* Class Action
*
* Responsible for performing an action on an element
*
* @package Project\Model\Component\Action
*/
class Action
{
/**
* @param NodeElement $element The element to perform the action on
* @param string $action The method name from NodeElement (the action to perform)
*
* @throws ActionNotImplementedException When no action exists
*
* @return string The user displayed string
*/
public function perform(NodeElement $element, $action)
{
if (!method_exists($element, $action = ucfirst($action)))
{
throw new ActionNotImplementedException();
}
$element->$action();
return sprintf('Action: %s on element: %s', $action, $this->figureElementIdentifier($element));
}
/**
* Returns the id attribute value or alternatively the xpath for the element
*
* @param NodeElement $element
*
* @return string
*/
private function figureElementIdentifier(NodeElement $element)
{
if (!$attribute = $element->getAttribute('id'))
{
$attribute = $element->getXpath();
}
return (string)$attribute;
}
}