Skip to content

Commit fb3e098

Browse files
author
Bl00D4NGEL
committed
feat: remove dependency on mockery, replace swapMock with swap
1 parent 60cecdb commit fb3e098

File tree

4 files changed

+55
-101
lines changed

4 files changed

+55
-101
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"require-dev": {
2424
"phpstan/phpstan": "^1.9",
2525
"friendsofphp/php-cs-fixer": "^3.14",
26-
"mockery/mockery": "^1.5",
2726
"phpstan/phpstan-mockery": "^1.1",
28-
"phpunit/phpunit": "^10.0"
27+
"phpunit/phpunit": "^10.0",
28+
"mockery/mockery": "^1.6"
2929
},
3030
"autoload": {
3131
"psr-4": {

composer.lock

Lines changed: 29 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Facade.php

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace GeekCell\Facade;
66

7-
use Mockery;
87
use Psr\Container\ContainerExceptionInterface;
98
use Psr\Container\ContainerInterface;
109
use Psr\Container\NotFoundExceptionInterface;
@@ -24,7 +23,7 @@ abstract class Facade
2423
/**
2524
* @var array<object>
2625
*/
27-
protected static array $resolvedInstances = [];
26+
protected static array $swappedInstances = [];
2827

2928
/**
3029
* Return the underlying instance behind the facade.
@@ -39,8 +38,8 @@ abstract class Facade
3938
public static function getFacadeRoot(): object
4039
{
4140
$accessor = static::getFacadeAccessor();
42-
if (isset(static::$resolvedInstances[$accessor])) {
43-
return static::$resolvedInstances[$accessor];
41+
if (isset(static::$swappedInstances[$accessor])) {
42+
return static::$swappedInstances[$accessor];
4443
}
4544

4645
if (!isset(static::$container)) {
@@ -50,12 +49,10 @@ public static function getFacadeRoot(): object
5049
$instance = static::$container->get($accessor);
5150
if (!is_object($instance)) {
5251
throw new \UnexpectedValueException(
53-
sprintf('The entry for "%s" must return an object.', $accessor),
52+
sprintf('The entry for "%s" must return an object. Got: %s', $accessor, get_debug_type($instance)),
5453
);
5554
}
5655

57-
self::$resolvedInstances[$accessor] = $instance;
58-
5956
return $instance;
6057
}
6158

@@ -88,57 +85,22 @@ public static function __callStatic(string $method, array $args): mixed
8885
*/
8986
public static function clear(): void
9087
{
91-
static::$resolvedInstances = [];
88+
static::$swappedInstances = [];
9289
static::$container = null;
9390
}
9491

9592
/**
96-
* Return the class name for the underlying instance behind the facade.
97-
*
98-
* @return string
99-
*/
100-
public static function getMockableClass(): string
101-
{
102-
$instance = static::getFacadeRoot();
103-
return get_class($instance);
104-
}
105-
106-
/**
107-
* Return a Mockery mock instance of the underlying instance behind the facade.
108-
*
109-
* @return Mockery\MockInterface
110-
*
111-
* @throws NotFoundExceptionInterface
112-
* @throws ContainerExceptionInterface
113-
* @throws \LogicException
114-
* @throws \UnexpectedValueException
115-
*/
116-
public static function createMock(): Mockery\MockInterface
117-
{
118-
$classToMock = static::getMockableClass();
119-
return Mockery::mock($classToMock);
120-
}
121-
122-
/**
123-
* Return a Mockery mock instance, but also swap the underlying instance behind the facade
124-
* so consecutive calls to the facade will use the mock.
93+
* @template T of object
12594
*
126-
* This behavior can be reset by calling Facade::clear().
127-
*
128-
* @return Mockery\MockInterface
129-
*
130-
* @throws NotFoundExceptionInterface
131-
* @throws ContainerExceptionInterface
132-
* @throws \LogicException
133-
* @throws \UnexpectedValueException
95+
* @param T $newInstance
96+
* @return T
13497
*/
135-
public static function swapMock(): Mockery\MockInterface
98+
public static function swap($newInstance)
13699
{
137100
$accessor = static::getFacadeAccessor();
138-
$mock = static::createMock();
139-
static::$resolvedInstances[$accessor] = $mock;
101+
static::$swappedInstances[$accessor] = $newInstance;
140102

141-
return $mock;
103+
return $newInstance;
142104
}
143105

144106
/**

tests/Unit/FacadeTest.php

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,49 +117,30 @@ public function testCallStaticWithMissingMethod(): void
117117
ServiceFacade::invalid(); // @phpstan-ignore-line
118118
}
119119

120-
public function testGetMockableClass(): void
120+
public function testGetFacadeRootUsesSwappedValueWithoutContainer(): void
121121
{
122-
// Given
123-
ServiceFacade::setContainer($this->container);
124-
125-
// When
126-
$result = ServiceFacade::getMockableClass();
127-
128-
// Then
129-
$this->assertSame(Service::class, $result);
130-
}
131-
132-
public function testCreateMock(): void
133-
{
134-
// Given
135-
ServiceFacade::setContainer($this->container);
122+
$service = new Service();
123+
ServiceFacade::swap($service);
136124

137-
// When
138-
$mock = ServiceFacade::createMock();
125+
$root = ServiceFacade::getFacadeRoot();
139126

140-
// Then
141-
$this->assertInstanceOf(Mockery\MockInterface::class, $mock);
127+
$this->assertSame($root, $service);
142128
}
143129

144-
public function testSwapMock(): void
130+
public function testSwap(): void
145131
{
146132
// Given
147133
ServiceFacade::setContainer($this->container);
148134

149-
// When
150-
$mock = ServiceFacade::swapMock();
151-
$mock->shouldReceive('greeting')->andReturn('Hello Mock!');
152-
153-
// Then
154-
$this->assertInstanceOf(Mockery\MockInterface::class, $mock);
135+
$root = ServiceFacade::getFacadeRoot();
136+
$this->assertSame($this->service, $root);
155137

156-
$result = ServiceFacade::greeting('World'); // @phpstan-ignore-line
157-
$this->assertEquals('Hello Mock!', $result);
138+
$otherService = new Service();
139+
ServiceFacade::swap($otherService);
158140

159-
ServiceFacade::clear();
160-
ServiceFacade::setContainer($this->container);
161-
$result = ServiceFacade::greeting('World'); // @phpstan-ignore-line
162-
$this->assertEquals('Hello World!', $result);
141+
$otherRoot = ServiceFacade::getFacadeRoot();
142+
$this->assertSame($otherRoot, $otherService);
143+
$this->assertNotSame($root, $otherRoot);
163144
}
164145

165146
public function testSettingContainerClearsFacadeCache(): void

0 commit comments

Comments
 (0)