Skip to content

Commit adfec8e

Browse files
committed
convert PHPUnit Mocks to Mockery
1 parent 4681feb commit adfec8e

File tree

7 files changed

+38
-67
lines changed

7 files changed

+38
-67
lines changed

.phive/phars.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
3-
<phar name="phpstan" version="1.11.11" installed="1.11.11" location="./tools/phpstan" copy="false"/>
4-
<phar name="psalm" version="5.25.0" installed="5.25.0" location="./tools/psalm" copy="false"/>
3+
<phar name="phpstan" version="1.12.3" installed="1.12.3" location="./tools/phpstan" copy="false"/>
4+
<phar name="psalm" version="5.26.1" installed="5.26.1" location="./tools/psalm" copy="false"/>
55
</phive>

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"require-dev": {
1111
"cakephp/cakephp-codesniffer": "^5.0",
12+
"mockery/mockery": "^1.6",
1213
"phpunit/phpunit": "^10.5.5 || ^11.1.3"
1314
},
1415
"license": "MIT",
@@ -17,6 +18,11 @@
1718
"CakeSentry\\": "src/"
1819
}
1920
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"CakeSentry\\Test\\": "tests/"
24+
}
25+
},
2026
"scripts": {
2127
"test": "phpunit",
2228
"cs-check": "phpcs --colors -p",

tests/TestCase/CakeSentryInitTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Cake\TestSuite\TestCase;
88
use CakeSentry\CakeSentryInit;
99

10-
final class CakeSentryInitTest extends TestCase
10+
class CakeSentryInitTest extends TestCase
1111
{
1212
/**
1313
* @inheritDoc

tests/TestCase/Database/CakeSentryLogTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPUnit\Framework\Attributes\DataProvider;
1111
use Psr\Log\LogLevel;
1212

13-
final class CakeSentryLogTest extends TestCase
13+
class CakeSentryLogTest extends TestCase
1414
{
1515
protected CakeSentryLog $logger;
1616

tests/TestCase/Error/SentryErrorLoggerTest.php

+12-37
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use Cake\Error\PhpError;
88
use CakeSentry\Error\SentryErrorLogger;
99
use CakeSentry\Http\SentryClient;
10+
use Mockery;
1011
use PHPUnit\Framework\TestCase;
11-
use ReflectionProperty;
1212
use RuntimeException;
1313

14-
final class SentryErrorLoggerTest extends TestCase
14+
class SentryErrorLoggerTest extends TestCase
1515
{
16-
private SentryErrorLogger $subject;
16+
protected SentryErrorLogger $logger;
17+
18+
protected SentryClient $client;
1719

1820
/**
1921
* @inheritDoc
@@ -23,13 +25,9 @@ public function setUp(): void
2325
parent::setUp();
2426

2527
Configure::write('Sentry.dsn', 'https://[email protected]/yourproject/1');
26-
$subject = new SentryErrorLogger([]);
27-
28-
$clientMock = $this->createMock(SentryClient::class);
29-
$this->subject = $subject;
30-
31-
$clientProp = $this->getClientProp();
32-
$clientProp->setValue($this->subject, $clientMock);
28+
$logger = new SentryErrorLogger([]);
29+
$this->logger = $logger;
30+
$this->client = Mockery::mock(SentryClient::class);
3331
}
3432

3533
/**
@@ -38,13 +36,8 @@ public function setUp(): void
3836
public function testLogException()
3937
{
4038
$excpetion = new RuntimeException('some error');
41-
42-
$client = $this->getClientProp()->getValue($this->subject);
43-
$client->expects($this->once())
44-
->method('captureException')
45-
->with($excpetion);
46-
47-
$this->subject->logException($excpetion);
39+
$this->client->shouldReceive('captureException')->with($excpetion, null);
40+
$this->assertNull($this->logger->logException($excpetion));
4841
}
4942

5043
/**
@@ -53,25 +46,7 @@ public function testLogException()
5346
public function testLogError()
5447
{
5548
$phpError = new PhpError(E_USER_WARNING, 'some error');
56-
57-
$client = $this->getClientProp()->getValue($this->subject);
58-
$client->expects($this->once())
59-
->method('captureError')
60-
->with($phpError);
61-
62-
$this->subject->logError($phpError);
63-
}
64-
65-
/**
66-
* Helper access subject::$client(reflection)
67-
*
68-
* @return ReflectionProperty Client reflection
69-
*/
70-
private function getClientProp()
71-
{
72-
$clientProp = new ReflectionProperty($this->subject, 'client');
73-
$clientProp->setAccessible(true);
74-
75-
return $clientProp;
49+
$this->client->shouldReceive('captureError')->with($phpError, null);
50+
$this->assertNull($this->logger->logError($phpError));
7651
}
7752
}

tests/TestCase/Http/ClientTest.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use CakeSentry\CakeSentryInit;
1212
use CakeSentry\Http\SentryClient;
1313
use Exception;
14+
use Mockery;
1415
use RuntimeException;
1516
use Sentry\ClientBuilder;
1617
use Sentry\ClientInterface;
@@ -20,7 +21,7 @@
2021
use Sentry\Options;
2122
use Sentry\State\Hub;
2223

23-
final class ClientTest extends TestCase
24+
class ClientTest extends TestCase
2425
{
2526
/**
2627
* @inheritDoc
@@ -59,6 +60,7 @@ public function testSetupClientSetOptions(): void
5960
{
6061
Configure::write('Sentry.server_name', 'test-server');
6162

63+
CakeSentryInit::init();
6264
$subject = new SentryClient();
6365
$options = $subject->getHub()->getClient()->getOptions();
6466

@@ -117,16 +119,13 @@ function () use (&$called) {
117119
public function testCaptureException(): void
118120
{
119121
$subject = new SentryClient();
120-
$sentryClientP = $this->createConfiguredMock(ClientInterface::class, [
121-
'captureException' => null,
122-
]);
123-
$subject->getHub()->bindClient($sentryClientP);
122+
$client = Mockery::mock(ClientInterface::class);
123+
$client->shouldReceive('captureException')->once()->andReturn(null);
124+
$subject->getHub()->bindClient($client);
124125

125126
$exception = new RuntimeException('something wrong.');
126127
$subject->captureException($exception);
127-
128-
$result = $sentryClientP->captureException($exception);
129-
$this->assertSame(null, $result);
128+
$this->assertTrue(true);
130129
}
131130

132131
/**
@@ -227,10 +226,9 @@ public function testCaptureErrorWithAdditionalData(): void
227226
public function testCaptureDispatchBeforeExceptionCapture(): void
228227
{
229228
$subject = new SentryClient();
230-
$sentryClientP = $this->createConfiguredMock(ClientInterface::class, [
231-
'captureException' => null,
232-
]);
233-
$subject->getHub()->bindClient($sentryClientP);
229+
$client = Mockery::mock(ClientInterface::class);
230+
$client->shouldReceive('captureException')->andReturn(null);
231+
$subject->getHub()->bindClient($client);
234232

235233
$called = false;
236234
EventManager::instance()->on(

tests/TestCase/Middleware/CakeSentryMiddlewareTest.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
use Cake\TestSuite\TestCase;
1010
use CakeSentry\Database\Log\CakeSentryLog;
1111
use CakeSentry\Middleware\CakeSentryQueryMiddleware;
12+
use Mockery;
1213
use Psr\Http\Server\RequestHandlerInterface;
1314

14-
final class CakeSentryMiddlewareTest extends TestCase
15+
class CakeSentryMiddlewareTest extends TestCase
1516
{
1617
public function testQueryLoggingEnabled(): void
1718
{
@@ -25,10 +26,10 @@ public function testQueryLoggingEnabled(): void
2526
'body' => '<html><title>test</title><body><p>some text</p></body>',
2627
]);
2728

28-
$handler = $this->handler();
29-
$handler->expects($this->once())
30-
->method('handle')
31-
->willReturn($response);
29+
$handler = Mockery::mock(RequestHandlerInterface::class);
30+
$handler->shouldReceive('handle')
31+
->once()
32+
->andReturn($response);
3233

3334
$middleware = new CakeSentryQueryMiddleware();
3435
$response = $middleware->process($request, $handler);
@@ -41,13 +42,4 @@ public function testQueryLoggingEnabled(): void
4142
$this->assertSame(CakeSentryLog::class, get_class($driver->getLogger()));
4243
}
4344
}
44-
45-
protected function handler()
46-
{
47-
$handler = $this->getMockBuilder(RequestHandlerInterface::class)
48-
->onlyMethods(['handle'])
49-
->getMock();
50-
51-
return $handler;
52-
}
5345
}

0 commit comments

Comments
 (0)