Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created December 7, 2016 14:51
Show Gist options
  • Save omerucel/aac9900da52a85cdfe6707c245cf03d4 to your computer and use it in GitHub Desktop.
Save omerucel/aac9900da52a85cdfe6707c245cf03d4 to your computer and use it in GitHub Desktop.
<?php
namespace {
class Databasecontext extends BaseContext
{
/**
* @Then /^"([^"]*)" tablosunda aşağıdaki kayıtlar bulunmalı:$/
*/
public function tablosundaAsagidakiKayitlarBulunmali($arg1, \Behat\Gherkin\Node\TableNode $table)
{
$columns = implode(',', array_keys($table->getHash()[0]));
$sql = 'SELECT ' . $columns . ' FROM ' . $arg1;
$rows = $this->getDi()->getMySQLConnection()->fetchAll($sql);
$this->assertEquals(count($rows), count($table->getHash()));
foreach ($rows as $index => $row) {
foreach ($row as $key => $value) {
$expectedValue = $table->getHash()[$index][$key];
if (strpos($expectedValue, 'REGEX=') > -1) {
$regexPattern = str_replace('REGEX=', '', $expectedValue);
$regexPattern = '#' . $regexPattern . '#';
$this->assertRegExp($regexPattern, $value);
} elseif (strpos($expectedValue, 'DATE=') > -1) {
$datePattern = str_replace('DATE=', '', $expectedValue);
if ($datePattern == 'TODAY') {
$this->assertEquals(date('Y-m-d'), (new DateTime($value))->format('Y-m-d'));
} elseif ($datePattern == 'OLD') {
$this->assertLessThan(date('Y-m-d'), (new DateTime($value))->format('Y-m-d'));
} elseif ($datePattern == 'FUTURE') {
$this->assertGreaterThan(date('Y-m-d'), (new DateTime($value))->format('Y-m-d'));
} else {
$regexPattern = '#' . $datePattern . '#';
$this->assertRegExp($regexPattern, $value);
}
} elseif (strpos($expectedValue, 'JSON=') > -1) {
$datePattern = str_replace('JSON=', '', $expectedValue);
$dateObject = json_decode($datePattern, true);
$valueObject = json_decode($value, true);
$this->assertTrue(empty(array_diff($dateObject, $valueObject)));
} else {
if (strpos($expectedValue, 'INTERVAL=') > -1) {
preg_match('|\#INTERVAL=(.*)\#|', $expectedValue, $matches);
if (count($matches) > 1) {
$date = new DateTime();
$intervalSpec = $matches[1];
$interval = new DateInterval($intervalSpec);
$date->add($interval);
$expectedValue = str_replace($matches[0], $date->format('d.m.Y'), $expectedValue);
}
}
$this->assertEquals($expectedValue, $value);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment