Skip to content

Commit

Permalink
Merge pull request #4 from interactive-solutions/feature/use-aliases
Browse files Browse the repository at this point in the history
Use aliases in tables
  • Loading branch information
macnibblet authored Dec 12, 2016
2 parents 6f3d541 + b72c91a commit 3ec7a8c
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 13 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ Things to note with `EntityOptions`
- aliases are used to create an alias for a configured entity
- defaultProperties are also used as default parameters when sending api requests

## Using aliases in TableNodes or PyStringNode
```php
Given an existing "TYPE" created with static method "create" with alias "TYPE1"
When I add a new "TYPE" with:
"""
{
"clinicId": "%Alias:FieldName%"
}
"""
```
If Alias does exist, %Alias:FieldName% is replaced with the FieldName of the Alias entity

You can also reference only the alias, if so, it will use the primary key of the entity.


## License
Copyright (c) 2016 Interactive Solutions Bodama AB

Expand Down
115 changes: 102 additions & 13 deletions src/Context/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Closure;
use Doctrine\ORM\EntityManager;
use DomainException;
use InteractiveSolutions\ZfBehat\Assertions;
use InteractiveSolutions\ZfBehat\Context\Aware\ApiClientAwareInterface;
use InteractiveSolutions\ZfBehat\Context\Aware\ApiClientAwareTrait;
use InteractiveSolutions\ZfBehat\Util\PluralisationUtil;
use PHPUnit_Framework_ExpectationFailedException;
use RuntimeException;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use function GuzzleHttp\Psr7\parse_query;
Expand All @@ -40,6 +42,11 @@ class ApiContext implements SnippetAcceptingContext, ApiClientAwareInterface, Se
*/
private $serviceManager;

/**
* @var EntityManager
*/
private $entityManager;

/**
* Inject the other contexts
*
Expand All @@ -53,6 +60,7 @@ public function bootstrap(BeforeScenarioScope $scope)
{
$this->userFixtureContext = $scope->getEnvironment()->getContext(UserFixtureContext::class);
$this->entityFixtureContext = $scope->getEnvironment()->getContext(EntityFixtureContext::class);
$this->entityManager = $scope->getEnvironment()->getContext(DatabaseContext::class)->getEntityManager();
}

/**
Expand All @@ -65,6 +73,40 @@ public function setServiceManager(ServiceManager $serviceManager)
$this->serviceManager = $serviceManager;
}

/**
* This will check if the value should be considered a alias, and if so, convert the value to the specified alias field
*
* @param $value
*
* @throws RuntimeException
* @return string
*/
private function convertValueToAlias($value)
{
if (!(strpos($value, '%') === 0) && !(strpos(strrev($value), '%') === 0)) {
return $value;
}

// Remove the last and first characters
$value = substr($value, 1, -1);

// Covers the case when a field is not specified
$alias = $value;
$field = null;

if (strpos($value, ':') !== 0) {
list($alias, $field) = explode(':', $value);
}

$entity = $this->entityFixtureContext->getEntityFromAlias($alias);

if (!$field) {
$field = $this->entityManager->getClassMetadata(get_class($entity))->getSingleIdentifierColumnName();
}

return $this->getFieldOfObject($entity, $field);
}

/**
* @When I retrieve all :type
*/
Expand Down Expand Up @@ -221,7 +263,7 @@ public function iAddANewWithValues($type, TableNode $values)
$body = $this->entityFixtureContext->getDefaultEntityProperties($type);

foreach ($values->getRows() as list ($key, $values)) {
$body[$key] = $values;
$body[$key] = $this->convertValueToAlias($values);
}

$this->getClient()->post($uri, $body);
Expand Down Expand Up @@ -265,7 +307,7 @@ public function iAddANewToAndTheValues($type, $parentType, $id, TableNode $value
$body = $this->entityFixtureContext->getDefaultEntityProperties($type);

foreach ($values->getRows() as list ($key, $values)) {
$body[$key] = $values;
$body[$key] = $this->convertValueToAlias($values);
}

$this->getClient()->post($uri, $body);
Expand Down Expand Up @@ -314,7 +356,7 @@ public function iUpdateATypeWithIdFromRelationParentTypeWithId($type, $typeId, $
$body = $this->entityFixtureContext->getDefaultEntityProperties($type);

foreach ($values->getRows() as list ($key, $values)) {
$body[$key] = $values;
$body[$key] = $this->convertValueToAlias($values);
}

$this->getClient()->put($uri, $body);
Expand All @@ -333,7 +375,7 @@ public function iUpdateAWithIdAndTheValues($type, $id, TableNode $values)
$body = $this->entityFixtureContext->getDefaultEntityProperties($type);

foreach ($values->getRows() as list ($key, $values)) {
$body[$key] = $values;
$body[$key] = $this->convertValueToAlias($values);
}

$this->getClient()->put($uri, $body);
Expand Down Expand Up @@ -375,7 +417,7 @@ public function iUpdateAWithIdWith($type, $id, PyStringNode $string)
$data = json_decode($string, true);

foreach ($data as $key => $value) {
$body[$key] = $value;
$body[$key] = $this->convertValueToAlias($value);
}

$this->getClient()->put($uri, $body);
Expand Down Expand Up @@ -416,7 +458,7 @@ public function iAddANewWith($type, PyStringNode $string)
$data = json_decode($string, true);

foreach ($data as $key => $value) {
$body[$key] = $value;
$body[$key] = $this->convertValueToAlias($value);
}

$this->getClient()->post($uri, $body);
Expand All @@ -431,7 +473,7 @@ public function iPartiallyUpdateAWithIdAndTheValues($type, $id, TableNode $value
$body = [];

foreach ($values->getRows() as list ($key, $values)) {
$body[$key] = $values;
$body[$key] = $this->convertValueToAlias($values);
}

$this->getClient()->patch($uri, $body);
Expand Down Expand Up @@ -529,7 +571,7 @@ public function iSendAActionToResourceWithIdWithMethodAndValues($action, $type,
$body = [];

foreach ($values->getRows() as list ($key, $values)) {
$body[$key] = $values;
$body[$key] = $this->convertValueToAlias($values);
}

$uri = $this->createUri($type, $id);
Expand All @@ -556,7 +598,7 @@ public function iSendAActionToResourceWithIdWithMethodWith($action, $type, $id,
$data = json_decode($string, true);

foreach ($data as $key => $value) {
$body[$key] = $value;
$body[$key] = $this->convertValueToAlias($value);
}

$uri = $this->createUri($type, $id);
Expand Down Expand Up @@ -783,7 +825,7 @@ public function iShouldReceiveAValidationErrorWithCount($count)
Assertions::assertJson($responseBody);
Assertions::assertEquals(422, $this->getClient()->lastResponse->getStatusCode());

Assertions::assertCount((int) $count, json_decode($responseBody, true)['errors']);
Assertions::assertCount((int)$count, json_decode($responseBody, true)['errors']);

} catch (PHPUnit_Framework_ExpectationFailedException $e) {

Expand Down Expand Up @@ -873,7 +915,7 @@ public function theResponseShouldMatchTheRequestProperties()
Assertions::assertArrayHasKey($key, $json);

if (is_bool($json[$key])) {
Assertions::assertEquals((bool) $value, $json[$key]);
Assertions::assertEquals((bool)$value, $json[$key]);
} else {
Assertions::assertEquals($value, $json[$key]);
}
Expand Down Expand Up @@ -906,7 +948,7 @@ public function itShouldContainRecords($records)
$json = json_decode($responseBody, true);

Assertions::assertArrayHasKey('data', $json);
Assertions::assertCount((int) $records, $json['data']);
Assertions::assertCount((int)$records, $json['data']);

} catch (PHPUnit_Framework_ExpectationFailedException $e) {

Expand Down Expand Up @@ -936,7 +978,7 @@ public function itShouldMatchTheFollowingProperties(TableNode $table)
Assertions::assertArrayHasKey($key, $json);

if (is_bool($json[$key])) {
Assertions::assertEquals((bool) $value, $json[$key]);
Assertions::assertEquals((bool)$value, $json[$key]);
} else {
Assertions::assertEquals($value, $json[$key]);
}
Expand All @@ -953,6 +995,53 @@ public function itShouldDumpTheLastResponse()
var_dump($responseBody);
}

/**
* :direction can be "not be" or "be"
*
* @Then /^the "([^"]*)" field "([^"]*)" should ([^"]*) "([^"]*)"$/
*
* @param $typeOfValue
* @param $field
* @param $direction
* @param $shouldBe
*/
public function theBooleanFieldFieldShouldBeValue($typeOfValue, $field, $direction, $shouldBe)
{
$direction = ($direction === 'be') ? 'assertEquals' : 'assertNotEquals';

$body = json_decode($this->getClient()->lastResponseBody, true);

$fields = explode('.', $field);

foreach ($fields as $field) {
$body = $body[$field];
}

$shouldBe = $this->convertValueToAlias($shouldBe);

switch ($typeOfValue) {
case 'boolean':
case 'bool':
$shouldBe = (strtolower($shouldBe) === 'false') ? false : $shouldBe;
$shouldBe = (boolean)$shouldBe;
break;
case 'string':
$shouldBe = (string)$shouldBe;
break;
case 'int':
case 'integer':
$shouldBe = (int)$shouldBe;
break;
case 'nullable':
$shouldBe = null;
break;
default:
$shouldBe = (string)$shouldBe;
}

Assertions::$direction($shouldBe, $body);
}

/**
* Binds an anonymous function to an object, allowing us to access
* instance variables directly
Expand Down
20 changes: 20 additions & 0 deletions src/Context/EntityFixtureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ public function anExistingTypeCreatedWithStaticMethod($type, $staticMethodName)
return $entity;
}

/**
* Add a entity of the given type with the default values created with the entities static method
*
* @Given an existing :type created with static method :staticMethodName with alias :alias
*
* @param string $type
*
* @throws ORMException
* @throws OptimisticLockException
* @throws TransactionRequiredException
*
* @return void
*/
public function anExistingTypeCreatedWithStaticMethodAndAlias($type, $staticMethodName, $alias)
{
$entity = $this->anExistingTypeCreatedWithStaticMethod($type, $staticMethodName);

$this->aliases[$alias] = $entity;
}

/**
* Add a entity of the given type and merging the default values with the new ones
*
Expand Down

0 comments on commit 3ec7a8c

Please sign in to comment.