Best Phoronix-test-suite code snippet using pts_validation.generate_xsd_element_objects
pts_validation.php
Source:pts_validation.php
...297 return $types;298 }299 public static function xsd_to_cli_creator($xsd_file, &$new_object, $types = null)300 {301 $nodes = self::generate_xsd_element_objects($xsd_file, null, $types);302 self::xsd_nodes_to_cli_prompts($nodes, $new_object);303 }304 public static function xsd_nodes_to_cli_prompts($nodes, &$new_object)305 {306 foreach($nodes as $node)307 {308 $path = $node->get_path();309 if($node->get_documentation() == null)310 {311 continue;312 }313 if(in_array('UNCOMMON', $node->get_flags_array()))314 {315 continue;316 }317 echo pts_client::cli_just_bold($node->get_name());318 /*319 if($node->get_value() != null)320 {321 echo ': ' . pts_client::cli_colored_text($node->get_value(), 'cyan');322 }323 */324 echo PHP_EOL;325 $enums = array();326 $min_value = -1;327 $max_value = -1;328 $type_restrict = null;329 if($node->get_input_type_restrictions() != null)330 {331 $type = $node->get_input_type_restrictions();332 $type_restrict = $type->get_type();333 // echo 'xx' . $type->get_name() . ' ' . $type->get_type() . 'xx' . PHP_EOL;334 $enums = $type->get_enums();335 if(!empty($enums))336 {337 echo pts_client::cli_colored_text('Possible Values: ', 'gray', true) . implode(', ', $enums) . PHP_EOL;338 echo pts_client::cli_colored_text('Multiple Selections Allowed: ', 'gray', true) . ($type->multi_enum_select() ? 'YES' : 'NO') . PHP_EOL;339 }340 $min_value = $type->get_min_value();341 if($min_value > -1)342 {343 echo pts_client::cli_colored_text('Minimum Value: ', 'gray', true) . $min_value . PHP_EOL;344 }345 $max_value = $type->get_max_value();346 if($max_value > 0)347 {348 echo pts_client::cli_colored_text('Maximum Value: ', 'gray', true) . $max_value . PHP_EOL;349 }350 }351 /*if($node->get_api() != null)352 {353 echo pts_client::cli_colored_text('API: ', 'gray', true) . $node->get_api()[0] . '->' . $node->get_api()[1] . '()' . PHP_EOL;354 }*/355 if($node->get_documentation() != null)356 {357 echo $node->get_documentation() . PHP_EOL;358 }359 if($node->get_default_value() != null)360 {361 echo pts_client::cli_colored_text('Default Value: ', 'gray', true) . $node->get_default_value() . PHP_EOL;362 }363 $do_require = in_array('TEST_REQUIRES', $node->get_flags_array());364 if(!empty($enums))365 {366 $input = pts_user_io::prompt_text_menu('Select from the supported options', $enums, $type->multi_enum_select(), false, null);367 if(is_array($input))368 {369 $input = implode(',', $input);370 }371 }372 else373 {374 do375 {376 $input_passes = true;377 $input = pts_user_io::prompt_user_input($path, !($do_require && $node->get_default_value() == null), false);378 if($do_require && $min_value > 0 && strlen($input) < $min_value)379 {380 echo 'Minimum length of ' . $min_value . ' is required.';381 $input_passes = false;382 }383 if($do_require && $max_value > 0 && strlen($input) > $max_value)384 {385 echo 'Maximum length of ' . $max_value . ' is supported.';386 $input_passes = false;387 }388 if(!empty($input) && $type_restrict == 'INT' && !is_numeric($input))389 {390 echo 'Input must be a valid integer number.';391 $input_passes = false;392 }393 if(!empty($input) && $type_restrict == 'xs:decimal' && !is_numeric($input))394 {395 echo 'Input must be a valid number.';396 $input_passes = false;397 }398 }399 while(!$input_passes);400 if(empty($input) && $node->get_default_value() != null)401 {402 $input = $node->get_default_value();403 }404 }405 $new_object->addXmlNodeWNE($path, trim($input));406 echo PHP_EOL;407 }408 }409 public static function xsd_to_html_creator($xsd_file, $types = null)410 {411 $nodes = self::generate_xsd_element_objects($xsd_file, null, $types);412 return self::xsd_nodes_to_html_prompts($nodes);413 }414 public static function xsd_nodes_to_html_prompts($nodes)415 {416 $html = null;417 foreach($nodes as $node)418 {419 $path = $node->get_path();420 if($node->get_documentation() == null)421 {422 continue;423 }424 $uncommon = in_array('UNCOMMON', $node->get_flags_array());425 $html .= '<div style="" class="' . ($uncommon ? 'pts_phoromatic_create_test_option_area_uncommon' : 'pts_phoromatic_create_test_option_area') . '" id="' . str_replace('/', '', $path) . '">';426 $html .= '<h3>' . $node->get_name() . ($uncommon ? ' <sup> Uncommon Option; Hover To Expand</sup>' : '') . '</h3>' . PHP_EOL;427 $enums = array();428 $min_value = -1;429 $max_value = -1;430 $type_restrict = null;431 if($node->get_input_type_restrictions() != null)432 {433 $html .= '<p>';434 $type = $node->get_input_type_restrictions();435 $type_restrict = $type->get_type();436 $enums = $type->get_enums();437 $min_value = $type->get_min_value();438 if($min_value > 0)439 {440 $html .= '<strong>Minimum Value: </strong>' . $min_value;441 }442 $max_value = $type->get_max_value();443 if($max_value > 0)444 {445 $html .= '<strong>Maximum Value: </strong>' . $max_value;446 }447 $html .= '</p>';448 }449 if($node->get_documentation() != null)450 {451 $html .= '<p>' . str_replace($node->get_name(), '<em>' . $node->get_name() . '</em>', $node->get_documentation()) . '</p>';452 }453 $do_require = in_array('TEST_REQUIRES', $node->get_flags_array());454 $html .= '<p>';455 if(!empty($enums))456 {457 $html .= '<select name="' . $path . '" ' . ($type->multi_enum_select() ? ' multiple' : '') . ($do_require ? ' required' : '') . '>' . PHP_EOL;458 foreach($enums as $enum)459 {460 $html .= '<option value="' . $enum . '"' . ($node->get_default_value() == $enum ? 'selected="selected"' : null) . '>' . $enum . '</option>';461 }462 $html .= '</select>';463 }464 else465 {466 if($type_restrict == 'INT' || $type_restrict == 'xs:decimal')467 {468 $html .= '<input type="number" name="' . $path . '" value="' . $node->get_default_value() . '" min="1" ' . ($do_require ? ' required' : '') . ' />';469 }470 else471 {472 $html .= '<input type="text" name="' . $path . '" value="' . $node->get_default_value() . '" ' . ($do_require ? ' required' : '') . ' />';473 }474 }475 $html .= '</p>';476 $html .= '</div>';477 }478 return $html;479 }480 public static function xsd_to_var_array_generate_xml($xsd_file, $types, &$array_to_check, &$writer)481 {482 foreach(self::generate_xsd_element_objects($xsd_file, null, $types) as $node)483 {484 $do_require = in_array('TEST_REQUIRES', $node->get_flags_array());485 $path = $node->get_path();486 $value = isset($array_to_check[$path]) ? $array_to_check[$path] : null;487 if(empty($value))488 {489 $value = $node->get_default_value();490 }491 if(empty($value))492 {493 continue;494 }495 if($do_require && empty($value))496 {497 //return 'The ' . $path . ' value cannot be empty.';498 }499 $writer->addXmlNodeWNE($path, $value);500 }501 return true;502 }503 public static function xsd_to_rebuilt_xml($xsd_file, $types, &$test_profile, &$writer)504 {505 $test_profile->no_fallbacks_on_null = true;506 foreach(self::generate_xsd_element_objects($xsd_file, $test_profile, $types) as $node)507 {508 $do_require = in_array('TEST_REQUIRES', $node->get_flags_array());509 $value = $node->get_value();510 $path = $node->get_path();511 if($value == $node->get_default_value() && in_array('UNCOMMON', $node->get_flags_array()))512 {513 continue;514 }515 //if(empty($value))516 //{517 // $value = $node->get_default_value();518 //}519 if(empty($value) && $value !== '0')520 {521 continue;522 }523 //if($do_require && empty($value))524 //{525 //return 'The ' . $path . ' value cannot be empty.';526 //}527 $writer->addXmlNodeWNE($path, $value);528 }529 $test_profile->no_fallbacks_on_null = false;530 return true;531 }532 public static function string_to_sanitized_test_profile_base($input)533 {534 return pts_strings::keep_in_string(str_replace(' ', '-', strtolower($input)), pts_strings::CHAR_LETTER | pts_strings::CHAR_NUMERIC | pts_strings::CHAR_DASH);;535 }536 protected static function generate_xsd_element_objects($xsd_file, $obj = null, $types = null)537 {538 $doc = new DOMDocument();539 if(is_file($xsd_file))540 {541 $doc->loadXML(file_get_contents($xsd_file));542 }543 $xpath = new DOMXPath($doc);544 $xpath->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');545 $nodes = array();546 $ev = $xpath->evaluate('/xs:schema/xs:element');547 foreach($ev as $e)548 {549 self::xsd_elements_to_objects($nodes, $obj, $xpath, $e, $types, '');550 }551 return $nodes;552 }553 public static function xsd_elements_to_objects(&$append_to_array, $o, $xpath, $el, $types, $path)554 {555 static $unbounded;556 if($el->getElementsByTagName('*')->length > 0 && $el->getElementsByTagName('*')->item(0)->nodeName == 'xs:annotation' && $el->getElementsByTagName('*')->item(0)->getElementsByTagName('documentation')->length > 0)557 {558 $name = $el->getAttribute('name');559 $value = null;560 $get_api = null;561 $set_api = null;562 $default_value = null;563 $flags = null;564 $dynamic_list_multi = '';565 $nodes_to_match = array('set' => 'set_api', 'get' => 'get_api', 'default' => 'default_value', 'flags' => 'flags', 'dynamic_list_multi' => 'dynamic_list_multi');566 $cnodes = $el->getElementsByTagName('*');567 for($i = 0; $i < $cnodes->length; $i++)568 {569 if(isset($nodes_to_match[$cnodes->item($i)->nodeName]) && ${$nodes_to_match[$cnodes->item($i)->nodeName]} == null)570 {571 ${$nodes_to_match[$cnodes->item($i)->nodeName]} = $cnodes->item($i)->nodeValue;572 }573 }574 if($get_api != null && (is_callable(array($o, $get_api)) || (is_array($o) && isset($o[$get_api]))))575 {576 if(is_object($o))577 {578 $class = get_class($o);579 $val = call_user_func(array($o, $get_api));580 if(is_object($val))581 {582 $o = $val;583 $val = null;584 }585 }586 else if(is_array($o))587 {588 $class = null;589 $val = $o[$get_api];590 }591 if($el->getAttribute('maxOccurs') == 'unbounded')592 {593 $o = $val;594 $val = null;595 }596 else if(is_array($val))597 {598 $val = implode(', ', call_user_func(array($o, $get_api)));599 }600 else if($val === true)601 {602 $val = 'TRUE';603 }604 else if($val === false)605 {606 $val = 'FALSE';607 }608 if($val !== null)609 {610 $value = $val;611 }612 }613 $input_type_restrictions = new pts_input_type_restrictions();614 if($el->getAttribute('type') != null)615 {616 $type = $el->getAttribute('type');617 if(isset($types[$type]))618 {619 $types[$type]->set_required($el->getAttribute('minOccurs') > 0);620 $input_type_restrictions = $types[$type];621 }622 }623 if(is_array($unbounded))624 {625 foreach($unbounded as $ub_check)626 {627 if(strpos($path, $ub_check) !== false)628 {629 $flags .= ' UNBOUNDED';630 break;631 }632 }633 }634 $api = null;635 if(!empty($get_api) && !empty($class))636 {637 $api = array($class, $get_api);638 }639 $documentation = trim($el->getElementsByTagName('annotation')->item('0')->getElementsByTagName('documentation')->item(0)->nodeValue);640 if($input_type_restrictions->is_enums_empty() && !empty($dynamic_list_multi))641 {642 $dynamic_list_multi = explode('.', $dynamic_list_multi);643 if(count($dynamic_list_multi) == 2 && is_callable(array($dynamic_list_multi[0], $dynamic_list_multi[1])))644 {645 $dynamic_list_multi_enums = call_user_func(array($dynamic_list_multi[0], $dynamic_list_multi[1]));646 if(is_array($dynamic_list_multi_enums))647 {648 $input_type_restrictions->set_enums($dynamic_list_multi_enums);649 $input_type_restrictions->set_multi_enum_select(true);650 }651 }652 }653 $append_to_array[] = new pts_element_node($name, $value, $input_type_restrictions, $api, $documentation, $set_api, $default_value, $flags, $path . '/' . $name);654 }655 else656 {657 $name = $el->getAttribute('name');658 $new_el = new pts_element_node($name);659 $new_el->set_path($path . '/' . $name);660 $append_to_array[] = $new_el;661 }662 if($el->getAttribute('maxOccurs') == 'unbounded')663 {664 $unbounded[$path . '/' . $name] = $path . '/' . $name;665 }666 $els = $xpath->evaluate('xs:complexType/xs:sequence/xs:element', $el);667 if(is_array($o) && !empty($o))668 {669 $path .= (!empty($path) ? '/' : '') . $name;670 foreach($o as $j)671 {672 foreach($els as $e)673 {674 self:: xsd_elements_to_objects($append_to_array, $j, $xpath, $e, $types, $path);675 }676 }677 }678 else679 {680 $path .= (!empty($path) ? '/' : '') . $name;681 foreach($els as $e)682 {683 self:: xsd_elements_to_objects($append_to_array, $o, $xpath, $e, $types, $path);684 }685 }686 }687 public static function process_xsd_display_chart($xsd_file, $obj = null, $types = null)688 {689 $nodes = self::generate_xsd_element_objects($xsd_file, $obj, $types);690 self::xsd_display_cli_from_objects($nodes);691 }692 public static function xsd_display_cli_from_objects($nodes)693 {694 foreach($nodes as $node)695 {696 $path = $node->get_path();697 $depth = count(explode('/', $path)) - 1;698 if($node->get_documentation() == null)699 {700 echo str_repeat(' ', $depth) . pts_client::cli_colored_text($node->get_name(), 'yellow', true);701 }702 else703 echo str_repeat(' ', $depth) . pts_client::cli_just_bold($node->get_name());...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with generate_xsd_element_objects on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!