diff --git a/composer.json b/composer.json index 912b699..ec9ee18 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "behat/mink": "~1.5" + "behat/mink": "~1.5", + "behat/gherkin": "^4.5.1" }, "autoload": { "psr-4": {"DennisDigital\\Behat\\Gtm\\": "src"} diff --git a/src/Context/GtmContext.php b/src/Context/GtmContext.php index 7ccff97..22722f4 100644 --- a/src/Context/GtmContext.php +++ b/src/Context/GtmContext.php @@ -1,16 +1,20 @@ getHash(); + $values_array = $hash[0]; + $event_array = $this->getDatalayerEvent($event); + foreach ($values_array as $property => $value) { + $this->checkDatalayerProperty($event_array, $property, $value); + } + } + + /** + * Check datalayer property value. + * + * @param array $event_array + * Event array. + * @param string $property + * Property datalayer. + * @param string $expected_value + * Value datalayer. + * + * @throws \Exception + */ + public function checkDatalayerProperty(array $event_array, $property, $expected_value) { + if (!isset($event_array['event'])) { + throw new \Exception('Event not found.'); + } + $event_name = $event_array['event']; + + $value = $event_array; + $property_keys = explode(':', $property); + foreach ($property_keys as $key) { + if(!isset($value[$key])) { + throw new \Exception('Property ' . $property . ' not found on event ' . $event_name); + } + $value = $value[$key]; + } + + if (!$this->wildcardMatch($expected_value, $value)) { + throw new \Exception('Value ' . $expected_value . ' not found on event ' . $event_name . ', value of property ' . $property . ' is ' . $value); + } + } + + public function wildcardMatch($pattern, $subject) { + $pattern = strtr($pattern, [ + '*' => '.*?', // 0 or more (lazy) - asterisk (*) + '?' => '.', // 1 character - question mark (?) + ]); + return preg_match("/$pattern/", $subject); + } + + /** + * Obtain datalater event array from event name. + * + * @param string $event + * Event. + * + * @return mixed + * Event. + * + * @throws \Exception + */ + protected function getDatalayerEvent($event) { + $json_arr = $this->getDataLayerJson(); + + // Loop through the array and return the data layer event. + foreach ($json_arr as $json_item) { + if (isset($json_item['event']) && $json_item['event'] == $event) { + return $json_item; + } + } + throw new \Exception('Event ' . $event . ' not found.'); + } + + /** + * Get Google Tag Manager Data Layer value. + * + * @param string $key + * Datalayer key. * - * @param $key * @return mixed + * Datalayer value from key. + * * @throws \Exception */ protected function getDataLayerValue($key) { $json_arr = $this->getDataLayerJson(); - // Loop through the array and return the data layer value + // Loop through the array and return the data layer value. foreach ($json_arr as $json_item) { if (isset($json_item[$key])) { return $json_item[$key]; @@ -92,8 +183,8 @@ protected function getDataLayerJsonFromSource() { // Get the html. $html = $this->getSession()->getPage()->getContent(); - // Get the dataLayer json and json_decode it - preg_match('~dataLayer\s*=\s*(.*?);~' , $html, $match); + // Get the dataLayer json and json_decode it. + preg_match('~dataLayer\s*=\s*(.*?);~', $html, $match); if (!isset($match[0])) { throw new \Exception('dataLayer variable not found in source.'); } @@ -113,4 +204,5 @@ protected function getDataLayerJsonFromJS() { return $json_arr; } + }