|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpmock\integration; |
| 4 | + |
| 5 | +use malkusch\phpmock\ParameterBuilder; |
| 6 | + |
| 7 | +/** |
| 8 | + * Defines a MockDelegateFunction. |
| 9 | + * |
| 10 | + * @author Markus Malkusch <markus@malkusch.de> |
| 11 | + * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations |
| 12 | + * @license http://www.wtfpl.net/txt/copying/ WTFPL |
| 13 | + * @internal |
| 14 | + */ |
| 15 | +class MockDelegateFunctionBuilder |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * The delegation method name. |
| 20 | + */ |
| 21 | + const METHOD = "delegate"; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var int The instance counter. |
| 25 | + */ |
| 26 | + private static $counter = 0; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string The namespace of the build class. |
| 30 | + */ |
| 31 | + private $namespace; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Text_Template The MockDelegateFunction template. |
| 35 | + */ |
| 36 | + private $template; |
| 37 | + |
| 38 | + /** |
| 39 | + * Instantiation. |
| 40 | + */ |
| 41 | + public function __construct() |
| 42 | + { |
| 43 | + $this->template = new \Text_Template(__DIR__ . "/MockDelegateFunction.tpl"); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Builds a MockDelegateFunction for a function. |
| 48 | + * |
| 49 | + * @param string|null $functionName The mocked function. |
| 50 | + * |
| 51 | + * @SuppressWarnings(PHPMD) |
| 52 | + */ |
| 53 | + public function build($functionName = null) |
| 54 | + { |
| 55 | + self::$counter++; |
| 56 | + |
| 57 | + $this->namespace = __NAMESPACE__ . self::$counter; |
| 58 | + |
| 59 | + $parameterBuilder = new ParameterBuilder(); |
| 60 | + $parameterBuilder->build($functionName); |
| 61 | + |
| 62 | + $data = [ |
| 63 | + "namespace" => $this->namespace, |
| 64 | + "signatureParameters" => $parameterBuilder->getSignatureParameters(), |
| 65 | + ]; |
| 66 | + $this->template->setVar($data, false); |
| 67 | + $definition = $this->template->render(); |
| 68 | + |
| 69 | + eval($definition); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the fully qualified class name |
| 74 | + * |
| 75 | + * @return string The class name. |
| 76 | + */ |
| 77 | + public function getFullyQualifiedClassName() |
| 78 | + { |
| 79 | + return "$this->namespace\\MockDelegateFunction"; |
| 80 | + } |
| 81 | +} |
0 commit comments