Jump to content

how to pass the date in preg_replace as regex and use that in test case for behat, php


Recommended Posts

I have to pass the date in one of the step as current date and compare that, but failing to achieve that test case is "And the URL "<>" contains the following:

"Measure name","Business description","Technical description","Data Sources",Reports,"Authored by","Last publish date"

"First Test Measure","business Lorem Ipsum is simply","technical Lorem Ipsum is",CHRISS,First Test Report,,

Here one of the parameter is "Last publish date" so I have to give it as it should be comes up as current date. I am trying to give the date [[now:j F Y], but it is not working.

Below is the function written for it, what change I have to do so it can have the date as well.

/** * Download a url and verify the contents. * * @throws \Exception * When the file contents don't match. * * @Then the URL :url contains the following: */ public function assertUrlContent(string $url, PyStringNode $content): void { $cookies = $this->getSession()->getDriver()->getWebDriverSession()->getCookie()[0]; $cookie = new SetCookie(); $cookie->setName($cookies['name']); $cookie->setValue($cookies['value']); $cookie->setDomain($cookies['domain']); $jar = new CookieJar(); $jar->setCookie($cookie); $client = new Client(); $res = $client->request('GET', $url, [ 'cookies' => $jar, 'verify' => FALSE, ]); $expected_string = preg_replace('/\r/', '', (string) $content); $actual_string = $res->getBody()->getContents(); if ($expected_string !== $actual_string) { print_r('Expected String: ' . PHP_EOL . $expected_string . PHP_EOL); print_r('Actual String: ' . PHP_EOL . $actual_string); throw new \Exception('Contents of download do not match'); } }

 

5 hours ago, Todar said:

I have to pass the date

What date?

5 hours ago, Todar said:

in one of the step

What step?

5 hours ago, Todar said:

as current date and compare that,

Compare to what?

5 hours ago, Todar said:

but failing to achieve that test case

What test case?

5 hours ago, Todar said:

is "And the URL "<>" contains the following:

...What?

5 hours ago, Todar said:

"Measure name","Business description","Technical description","Data Sources",Reports,"Authored by","Last publish date"

"First Test Measure","business Lorem Ipsum is simply","technical Lorem Ipsum is",CHRISS,First Test Report,,

So that's CSV?

5 hours ago, Todar said:

Here one of the parameter is "Last publish date" so I have to give it as it should be comes up as current date. I am trying to give the date [[now:j F Y], but it is not working.

What does "not working" mean? What does it does? What does it not do? What do you expect to see and what do you actually see?

5 hours ago, Todar said:

Below is the function written for it, what change I have to do so it can have the date as well.

Can you try posting the code again so that it's not all on one line?

 

This is the code given below

/** * Download a url and verify the contents.

* * @throws \Exception * When the file contents don't match.

* * @Then the URL :url contains the following: */

public function assertUrlContent(string $url, PyStringNode $content): void

{

$cookies = $this->getSession()->getDriver()->getWebDriverSession()->getCookie()[0];

$cookie = new SetCookie(); $cookie->setName($cookies['name']);

$cookie->setValue($cookies['value']);

$cookie->setDomain($cookies['domain']);

$jar = new CookieJar();

$jar->setCookie($cookie);

$client = new Client();

$res = $client->request('GET', $url, [ 'cookies' => $jar, 'verify' => FALSE, ]);

$expected_string = preg_replace('/\r/', '', (string) $content);

$actual_string = $res->getBody()->getContents();

if ($expected_string !== $actual_string) { print_r('Expected String: ' . PHP_EOL . $expected_string . PHP_EOL);

print_r('Actual String: ' . PHP_EOL . $actual_string);

throw new \Exception('Contents of download do not match');

}

}

 

in one case i have to compare the data of csv and on of the item is date that i have give on. test case is

And the URL "https://test2-portal.dev.local/data/measure-definitions.csv?title=&;report_nid=&;field_data_source_target_id=All" contains the following:

"""

"Measure name","Business description","Technical description","Data Sources",Reports,"Authored by", "Published date"

"First Test Measure","business Lorem Ipsum is simply","technical Lorem Ipsum is",CHRISS,,,

"Second Test Measure","business Lorem Ipsum is simply","technical Lorem Ipsum is",PHESS,,,

"""

 

and so my question what are ways to calculate the Published date as current date as expected dataand in which format I should enter it.

I want to what changes I need to make in $expected_string = preg_replace('/\r/', '', (string) $content);  so it calculate the current date in the expected string.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.