i have following class want test:
<?php namespace freya\component\pagebuilder\fieldhandler; use freya\component\pagebuilder\fieldhandler\ifieldhandler; /** * used set of non empty, false or null fields. * * core purpose use class determine a) advanced custom fields installed * , b) set of fields child page. * * cavete here class requires have child pages have custom fields * want display on each of pages. getting other page information such content, title, featured image * , other meta boxes left ot end developer. * * @see freya\component\pagebuilder\fieldhandler\ifieldhandler */ class fieldhandler implements ifieldhandler { /** * {@inheritdoc} * * @return bool */ public function checkforafc() { return function_exists("register_field_group"); } /** * {@inheritdoc} * * @param $childpageid - id of child page may or may not have custom fields. * @return mixed - null or array */ public function getfields($childpageid) { $fieldsforthispage = get_fields($childpageid); if (is_array($fieldsforthispage)) { foreach ($fieldsforthispage $key => $value) { if ($value === "" || $value === false || $value === null) { unset($fieldsforthispage[$key]); } } return $fieldsforthispage; } return null; } } i can test of 1 thing want stub get_fields() function return type of array used how ever rest of function uses it, in case looping through it.
the part don't know how in php stub function that's being called , return x.
so how stub get_fields?
you can use trick here unqualified function name get_fields(). since don't use fully qualified function name \get_fields() php first try find function in current namespace , fall global function name.
for definition of qualified , unqualified, see: http://php.net/manual/en/language.namespaces.basics.php (it's similar absolute , relative filenames)
so have define function in namespace of class, test case, this:
namespace freya\component\pagebuilder\fieldhandler; function get_fields() { return ['x']; } class fieldhandlertest extends \phpunit_test_case { ... } additional notes:
- you can same core functions, described here: http://www.schmengler-se.de/en/2011/03/php-mocking-built-in-functions-like-time-in-unit-tests/
- this trick works functions, not classes. classes in global namespace must referenced leading backslash.
Comments
Post a Comment