Skip to content

Add support for Symfony 7 #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot refer to the kernel version. This is not really "nice" but for now, that works great.

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;
}
}
}


Loading