/** * Checks that the given select field doesn't have the listed options. * * @Then I should not have the following options for :select: */ public function assertNoSelectOptions($select, TableNode $options) { // Retrieve the specified field. if (!($field = $this->getSession()->getPage()->findField($select))) { throw new ExpectationException("Field '{$select}' not found.", $this->getSession()); } // Check that the specified field is a <select> field. $this->assertElementType($field, 'select'); // Retrieve the options table from the test scenario and flatten it. $expected_options = $options->getColumnsHash(); array_walk($expected_options, function (&$value) { $value = reset($value); }); // Retrieve the actual options that are shown in the page. $actual_options = $field->findAll('css', 'option'); // Convert into a flat list of option text strings. array_walk($actual_options, function (&$value) { $value = $value->getText(); }); // Check that none of the expected options are present. foreach ($expected_options as $expected_option) { if (in_array($expected_option, $actual_options)) { throw new ExpectationException("Option '{$expected_option}' is unexpectedly found in select list '{$select}'.", $this->getSession()); } } }
/** * @Given /^I should see the row in the table "(?P<element>[^"]*)":$/ * * @param string $element * @param TableNode $table * * @throws \Exception */ public function iShouldSeeTheRow($element, TableNode $table) { // Check that the (html) table exists $this->assertNumElements(1, $element); // Get the (html) table rows $headerNodeElements = $this->getSession()->getPage()->findAll('css', sprintf('%s thead > tr > th', $element)); $rowNodeElements = $this->getSession()->getPage()->findAll('css', sprintf('%s > tbody > tr', $element)); foreach ($table->getColumnsHash() as $rowIndex => $row) { // At this point, we want to check that $rowNodeElements contains a rows matching $row $rowMatches = false; foreach ($rowNodeElements as $rowNodeElement) { /* @var NodeElement $rowNodeElement */ try { // Get the row cells $cellNodeElements = $rowNodeElement->findAll('css', 'td'); // Check that for each cells of $row, we got a matching value foreach ($row as $columnText => $cellValue) { $this->assertNodeElementContainsText($cellNodeElements[$this->findTableIndex($headerNodeElements, $columnText)], $cellValue); } // At this point the row match otherwise an exception would have been thrown $rowMatches = true; continue; } catch (\Exception $exception) { // Exception thrown because the row was not matching // Do nothing and pass to the next row } } PHPUnit::assertTrue($rowMatches, sprintf('Expected to find at least one row matching row #%d', $rowIndex)); } }
/** * @Given order events exists: */ public function orderEventsExists(TableNode $table) { foreach ($table->getColumnsHash() as $eventData) { $event = $this->getContainer()->get('crm.customer_event.factory')->createOrder(['value' => (double) $eventData['Value'], 'valueCurrency' => $eventData['Currency'], 'customerId' => $eventData['Customer ID'], 'time' => new \DateTime($eventData['Time'])]); $this->getContainer()->get('crm.customer_event.repository')->add($event); } }
/** * @Given the following people exist: */ public function theFollowingPeopleExist(TableNode $table) { $data = new Sample(); $values = $table->getColumnsHash(); foreach ($values as $val) { $data->set($val['name'], $val['id']); } }
/** * @Given the following roles exist: */ public function theFollowingRolesExist(TableNode $roles) { $roleData = $roles->getColumnsHash(); foreach ($roleData as $roleDatum) { $this->getOrCreateRole($roleDatum['name'], $roleDatum['system']); } $this->getEntityManager()->flush(); }
/** * @Given users and permissions: */ public function usersAndPermissions(TableNode $table) { $permission = array(); foreach ($table->getColumnsHash() as $userInfo) { $permission[$userInfo['user']] = explode(',', $userInfo['resource']); } $this->userPermission = new \Dgafka\Fixtures\IBAC\SimpleACL($permission); }
/** * @Given There are the following responses: */ public function thereAreTheFollowingResponses(TableNode $table) { $em = $this->getManager(); $applicationRepository = $em->getRepository('JSONMockBundle:Application'); foreach ($table->getColumnsHash() as $row) { $response = new \JSONMockBundle\Entity\Response(); $response->setName($row['Name']); $response->setUrl($row['Url']); $response->setValue(json_decode($row['Value'])); $response->setMethod($row['Method']); $response->setStatusCode($row['Status_code']); $application = $applicationRepository->find($row['APP_ID']); $response->setApplication($application); $this->getManager()->persist($response); } $this->getManager()->flush(); $this->getManager()->clear(); }
/** * @Given /^User "([^"]*)" exists with:$/ */ public function userExistsWith($username, TableNode $table) { $entity = new User($username, 'password'); foreach ($table->getColumnsHash() as $row) { $value = $row['Value']; if ('Roles' === $row['Property']) { $roles = $entity->getRoles(); if (!in_array($value, $roles)) { $roles[] = $value; } $entity->setRoles($roles); continue; } $setter = 'set' . $row['Property']; $entity->{$setter}($value); } $this->getParameterBag()->set('user', $entity); $em = $this->getEntityManager(); $em->persist($entity); $em->flush(); }
/** * @Given /^it should contain the following set of labels, and input fields of the following types:$/ */ public function itShouldContainTheFollowingSetOfLabelsAndInputFieldsTypes(TableNode $table) { $fieldsExpectations = $table->getColumnsHash(); $inputNodeElements = $this->getSession()->getPage()->findAll('css', sprintf('div.ezfield-identifier-%s fieldset input', self::$fieldIdentifier)); /** @var NodeElement $nodeElement */ foreach ($inputNodeElements as $nodeElement) { foreach ($fieldsExpectations as $expectationId => $fieldExpectation) { if ($fieldExpectation['type'] === $nodeElement->getAttribute('type')) { $inputId = $nodeElement->getAttribute('id'); $this->assertSession()->elementExists('css', "label[for={$inputId}]"); $this->assertSession()->elementTextContains('css', "label[for={$inputId}]", $fieldExpectation['label']); unset($fieldsExpectations[$expectationId]); reset($fieldsExpectations); break; } } } Assertion::assertEmpty($fieldsExpectations, 'The following input fields were not found:' . implode(', ', array_map(function ($v) { return $v['label']; }, $fieldsExpectations))); }
/** * @Then the JSON response should have the following nodes: * * @param TableNode $table */ public function theJSONResponseIsComposedOf(TableNode $table) { $count = 0; foreach ($table->getColumnsHash() as $row) { ++$count; $expectedValue = $row['value']; // Check for null value // The `~` is used to specify null value (like in YAML) unless the type is explicitly set to string // in which case will be processed as the string '~'. if ('~' === $expectedValue && (false === array_key_exists('type', $row) || 'string' !== $row['type'])) { $this->theJsonNodeShouldBeNull($row['node']); continue; } // Default type is set to string $expectedValueType = 'string'; if (array_key_exists('type', $row)) { // Trim the expected value type of all spaces before using its value $_expectedValueType = str_replace(' ', '', $row['type']); if (false === empty($_expectedValueType)) { $expectedValueType = $_expectedValueType; } unset($_expectedValueType); } $expectedValue = $this->normalizeValue($row['value'], $expectedValueType); if (true === is_bool($expectedValue) || true === is_int($expectedValue)) { PHPUnit::assertEquals($expectedValue, $this->inspector->evaluate($this->getJson(), $row['node'])); continue; } if ('array' === $expectedValueType) { $this->theJsonNodeShouldBeAnArray($row['node']); continue; } if ('object' === $expectedValueType) { $this->theJsonNodeShouldBeAnObject($row['node']); continue; } // If want to compare to an empty string, the value must be `""` in the table // Otherwise an empty string means no check on the value if ('""' === $expectedValue) { $this->theJsonNodeShouldBeEqualTo($row['node'], ''); continue; } if ('' === $expectedValue) { $this->theJsonNodeShouldExist($row['node']); continue; } $this->theJsonNodeShouldBeEqualTo($row['node'], $expectedValue); } $nbrOfNodes = $this->getNumberOfNodes($this->getJson()->getContent()); PHPUnit::assertEquals($nbrOfNodes, $count, sprintf('Expected to find %d nodes. Found %d instead', $nbrOfNodes, $count)); }
/** * @Given the following pages exist: */ public function theFollowingPagesExist(TableNode $table) { $data = $table->getColumnsHash(); $this->createStructures('page', $data); }
/** * @Transform table:article_id * * @param TableNode $articleIds * * @return array */ public function castArticleIds(TableNode $articleIds) { return array_column($articleIds->getColumnsHash(), 'article_id'); }
/** * @Then I should get: */ public function iShouldGet(TableNode $table) { if (isset($this->response["error"])) { throw new \Exception($this->response["error"]); } $result = array_map(function ($item) { return ['Name' => $item["name"]]; }, $this->response["completion"]); expect($table->getColumnsHash())->to->be->equal($result); }