7
7
use Cake \Error \PhpError ;
8
8
use CakeSentry \Error \SentryErrorLogger ;
9
9
use CakeSentry \Http \SentryClient ;
10
+ use Mockery ;
10
11
use PHPUnit \Framework \TestCase ;
11
- use ReflectionProperty ;
12
12
use RuntimeException ;
13
13
14
- final class SentryErrorLoggerTest extends TestCase
14
+ class SentryErrorLoggerTest extends TestCase
15
15
{
16
- private SentryErrorLogger $ subject ;
16
+ protected SentryErrorLogger $ logger ;
17
+
18
+ protected SentryClient $ client ;
17
19
18
20
/**
19
21
* @inheritDoc
@@ -23,13 +25,9 @@ public function setUp(): void
23
25
parent ::setUp ();
24
26
25
27
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);
33
31
}
34
32
35
33
/**
@@ -38,13 +36,8 @@ public function setUp(): void
38
36
public function testLogException ()
39
37
{
40
38
$ 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 ));
48
41
}
49
42
50
43
/**
@@ -53,25 +46,7 @@ public function testLogException()
53
46
public function testLogError ()
54
47
{
55
48
$ 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 ));
76
51
}
77
52
}
0 commit comments