11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Test ;
13
13
14
+ use PHPUnit \Framework \Constraint \Constraint ;
14
15
use PHPUnit \Framework \Constraint \LogicalAnd ;
15
16
use PHPUnit \Framework \Constraint \LogicalNot ;
17
+ use PHPUnit \Framework \ExpectationFailedException ;
16
18
use Symfony \Component \BrowserKit \AbstractBrowser ;
17
19
use Symfony \Component \BrowserKit \Test \Constraint as BrowserKitConstraint ;
18
20
use Symfony \Component \HttpFoundation \Request ;
@@ -28,12 +30,12 @@ trait BrowserKitAssertionsTrait
28
30
{
29
31
public static function assertResponseIsSuccessful (string $ message = '' ): void
30
32
{
31
- self ::assertThat ( self :: getResponse (), new ResponseConstraint \ResponseIsSuccessful (), $ message );
33
+ self ::assertThatForResponse ( new ResponseConstraint \ResponseIsSuccessful (), $ message );
32
34
}
33
35
34
36
public static function assertResponseStatusCodeSame (int $ expectedCode , string $ message = '' ): void
35
37
{
36
- self ::assertThat ( self :: getResponse (), new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
38
+ self ::assertThatForResponse ( new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
37
39
}
38
40
39
41
public static function assertResponseRedirects (string $ expectedLocation = null , int $ expectedCode = null , string $ message = '' ): void
@@ -46,42 +48,42 @@ public static function assertResponseRedirects(string $expectedLocation = null,
46
48
$ constraint = LogicalAnd::fromConstraints ($ constraint , new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ));
47
49
}
48
50
49
- self ::assertThat ( self :: getResponse (), $ constraint , $ message );
51
+ self ::assertThatForResponse ( $ constraint , $ message );
50
52
}
51
53
52
54
public static function assertResponseHasHeader (string $ headerName , string $ message = '' ): void
53
55
{
54
- self ::assertThat ( self :: getResponse (), new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
56
+ self ::assertThatForResponse ( new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
55
57
}
56
58
57
59
public static function assertResponseNotHasHeader (string $ headerName , string $ message = '' ): void
58
60
{
59
- self ::assertThat ( self :: getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
61
+ self ::assertThatForResponse ( new LogicalNot(new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
60
62
}
61
63
62
64
public static function assertResponseHeaderSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
63
65
{
64
- self ::assertThat ( self :: getResponse (), new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
66
+ self ::assertThatForResponse ( new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
65
67
}
66
68
67
69
public static function assertResponseHeaderNotSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
68
70
{
69
- self ::assertThat ( self :: getResponse (), new LogicalNot (new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
71
+ self ::assertThatForResponse ( new LogicalNot (new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
70
72
}
71
73
72
74
public static function assertResponseHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
73
75
{
74
- self ::assertThat ( self :: getResponse (), new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
76
+ self ::assertThatForResponse ( new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
75
77
}
76
78
77
79
public static function assertResponseNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
78
80
{
79
- self ::assertThat ( self :: getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain )), $ message );
81
+ self ::assertThatForResponse ( new LogicalNot (new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain )), $ message );
80
82
}
81
83
82
84
public static function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
83
85
{
84
- self ::assertThat ( self :: getResponse (), LogicalAnd::fromConstraints (
86
+ self ::assertThatForResponse ( LogicalAnd::fromConstraints (
85
87
new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ),
86
88
new ResponseConstraint \ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain )
87
89
), $ message );
@@ -124,6 +126,21 @@ public static function assertRouteSame($expectedRoute, array $parameters = [], s
124
126
self ::assertThat (self ::getRequest (), $ constraint , $ message );
125
127
}
126
128
129
+ public static function assertThatForResponse (Constraint $ constraint , string $ message = '' ): void
130
+ {
131
+ try {
132
+ self ::assertThat (self ::getResponse (), $ constraint , $ message );
133
+ } catch (ExpectationFailedException $ exception ) {
134
+ if (($ serverExceptionMessage = self ::getResponse ()->headers ->get ('X-Debug-Exception ' ))
135
+ && ($ serverExceptionFile = self ::getResponse ()->headers ->get ('X-Debug-Exception-File ' ))) {
136
+ $ serverExceptionFile = explode (': ' , $ serverExceptionFile );
137
+ $ exception ->__construct ($ exception ->getMessage (), $ exception ->getComparisonFailure (), new \ErrorException (rawurldecode ($ serverExceptionMessage ), 0 , 1 , rawurldecode ($ serverExceptionFile [0 ]), $ serverExceptionFile [1 ]), $ exception ->getPrevious ());
138
+ }
139
+
140
+ throw $ exception ;
141
+ }
142
+ }
143
+
127
144
private static function getClient (AbstractBrowser $ newClient = null ): ?AbstractBrowser
128
145
{
129
146
static $ client ;
0 commit comments