Skip to content

Commit

Permalink
Merge pull request #22 from loic425/symfony-7
Browse files Browse the repository at this point in the history
Add support for Symfony 7
  • Loading branch information
l3l0 authored Sep 9, 2024
2 parents b4c78de + 484f2ba commit 84b0f32
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
],
"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": {
"phpunit/phpunit": "^8.4"
},
"autoload": {
"psr-4": { "PSS\\SymfonyMockerContainer\\": "src/" }
},
"extra": {
"symfony": {
"require": "7.0.*"
}
}
}
169 changes: 113 additions & 56 deletions src/DependencyInjection/MockerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}


0 comments on commit 84b0f32

Please sign in to comment.