diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9735b3a..3907b68 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,6 +13,9 @@ jobs: matrix: php: ["8.0", "8.1"] symfony: ["^4.4", "^5.4", "^6.0"] + include: + - php: "8.3" + symfony: "^7.0" steps: diff --git a/composer.json b/composer.json index cdc4cd8..2c0d24e 100755 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^8.0", - "symfony/dependency-injection": "^4.3 || ^5.0 || ^6.0", + "symfony/dependency-injection": "^4.3 || ^5.0 || ^6.0 || ^7.0", "mockery/mockery": "^1.3" }, "require-dev": { @@ -25,5 +25,10 @@ }, "autoload": { "psr-4": { "PSS\\SymfonyMockerContainer\\": "src/" } + }, + "extra": { + "symfony": { + "require": "7.0.*" + } } } diff --git a/src/DependencyInjection/MockerContainer.php b/src/DependencyInjection/MockerContainer.php index 083643b..b9647ec 100644 --- a/src/DependencyInjection/MockerContainer.php +++ b/src/DependencyInjection/MockerContainer.php @@ -4,77 +4,134 @@ use Symfony\Component\DependencyInjection\Container; -class MockerContainer extends Container -{ - /** - * @var array $mockedServices - */ - static private $mockedServices = array(); - - /** - * Takes an id of the service as the first argument. - * Any other arguments are passed to the Mockery factory. - * - * @return \Mockery\Mock - */ - public function mock() +if (null === (new \ReflectionClass(Container::class))->getMethod('get')->getReturnType()) { + class MockerContainer extends Container { - $arguments = func_get_args(); - $id = array_shift($arguments); + /** + * @var array $mockedServices + */ + static private $mockedServices = array(); - if (!$this->has($id)) { - throw new \InvalidArgumentException(sprintf('Cannot mock a non-existent service: "%s"', $id)); + /** + * Takes an id of the service as the first argument. + * Any other arguments are passed to the Mockery factory. + * + * @return \Mockery\Mock + */ + public function mock() + { + $arguments = func_get_args(); + $id = array_shift($arguments); + + if (!$this->has($id)) { + throw new \InvalidArgumentException(sprintf('Cannot mock a non-existent service: "%s"', $id)); + } + + if (!array_key_exists($id, self::$mockedServices)) { + self::$mockedServices[$id] = call_user_func_array(array('Mockery', 'mock'), $arguments); + } + + return self::$mockedServices[$id]; } - if (!array_key_exists($id, self::$mockedServices)) { - self::$mockedServices[$id] = call_user_func_array(array('Mockery', 'mock'), $arguments); + /** + * @return null + */ + public function unmock($id) + { + unset(self::$mockedServices[$id]); } - return self::$mockedServices[$id]; - } + public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object + { + if (array_key_exists($id, self::$mockedServices)) { + return self::$mockedServices[$id]; + } - /** - * @return null - */ - public function unmock($id) - { - unset(self::$mockedServices[$id]); - } + return parent::get($id, $invalidBehavior); + } + + public function has($id): bool + { + if (array_key_exists($id, self::$mockedServices)) { + return true; + } + + return parent::has($id); + } - /** - * @param string $id - * @param integer $invalidBehavior - * - * @return object - */ - public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object + /** + * @return array + */ + public function getMockedServices() + { + return self::$mockedServices; + } + } +} else { + class MockerContainer extends Container { - if (array_key_exists($id, self::$mockedServices)) { + /** + * @var array $mockedServices + */ + static private $mockedServices = array(); + + /** + * Takes an id of the service as the first argument. + * Any other arguments are passed to the Mockery factory. + * + * @return \Mockery\Mock + */ + public function mock() + { + $arguments = func_get_args(); + $id = array_shift($arguments); + + if (!$this->has($id)) { + throw new \InvalidArgumentException(sprintf('Cannot mock a non-existent service: "%s"', $id)); + } + + if (!array_key_exists($id, self::$mockedServices)) { + self::$mockedServices[$id] = call_user_func_array(array('Mockery', 'mock'), $arguments); + } + return self::$mockedServices[$id]; } - return parent::get($id, $invalidBehavior); - } + /** + * @return null + */ + public function unmock($id) + { + unset(self::$mockedServices[$id]); + } - /** - * @param string $id - * - * @return boolean - */ - public function has($id): bool - { - if (array_key_exists($id, self::$mockedServices)) { - return true; + public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object + { + if (array_key_exists($id, self::$mockedServices)) { + return self::$mockedServices[$id]; + } + + return parent::get($id, $invalidBehavior); } - return parent::has($id); - } + public function has(string $id): bool + { + if (array_key_exists($id, self::$mockedServices)) { + return true; + } - /** - * @return array - */ - public function getMockedServices() - { - return self::$mockedServices; + return parent::has($id); + } + + /** + * @return array + */ + public function getMockedServices() + { + return self::$mockedServices; + } } } + +