33namespace React \Tests \Http ;
44
55use Clue \React \Block ;
6- use React \Http \Browser ;
76use Psr \Http \Message \RequestInterface ;
7+ use React \Http \Browser ;
88use React \Promise \Promise ;
99use RingCentral \Psr7 \Uri ;
1010
@@ -21,7 +21,7 @@ public function setUpBrowser()
2121 {
2222 $ this ->loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
2323 $ this ->sender = $ this ->getMockBuilder ('React\Http\Io\Transaction ' )->disableOriginalConstructor ()->getMock ();
24- $ this ->browser = new Browser ($ this ->loop );
24+ $ this ->browser = new Browser (null , $ this ->loop );
2525
2626 $ ref = new \ReflectionProperty ($ this ->browser , 'transaction ' );
2727 $ ref ->setAccessible (true );
@@ -43,6 +43,114 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()
4343 $ this ->assertInstanceOf ('React\EventLoop\LoopInterface ' , $ loop );
4444 }
4545
46+ public function testConstructWithConnectorAssignsGivenConnector ()
47+ {
48+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
49+
50+ $ browser = new Browser ($ connector );
51+
52+ $ ref = new \ReflectionProperty ($ browser , 'transaction ' );
53+ $ ref ->setAccessible (true );
54+ $ transaction = $ ref ->getValue ($ browser );
55+
56+ $ ref = new \ReflectionProperty ($ transaction , 'sender ' );
57+ $ ref ->setAccessible (true );
58+ $ sender = $ ref ->getValue ($ transaction );
59+
60+ $ ref = new \ReflectionProperty ($ sender , 'http ' );
61+ $ ref ->setAccessible (true );
62+ $ client = $ ref ->getValue ($ sender );
63+
64+ $ ref = new \ReflectionProperty ($ client , 'connector ' );
65+ $ ref ->setAccessible (true );
66+ $ ret = $ ref ->getValue ($ client );
67+
68+ $ this ->assertSame ($ connector , $ ret );
69+ }
70+
71+ public function testConstructWithConnectorWithLegacySignatureAssignsGivenConnector ()
72+ {
73+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
74+
75+ $ browser = new Browser (null , $ connector );
76+
77+ $ ref = new \ReflectionProperty ($ browser , 'transaction ' );
78+ $ ref ->setAccessible (true );
79+ $ transaction = $ ref ->getValue ($ browser );
80+
81+ $ ref = new \ReflectionProperty ($ transaction , 'sender ' );
82+ $ ref ->setAccessible (true );
83+ $ sender = $ ref ->getValue ($ transaction );
84+
85+ $ ref = new \ReflectionProperty ($ sender , 'http ' );
86+ $ ref ->setAccessible (true );
87+ $ client = $ ref ->getValue ($ sender );
88+
89+ $ ref = new \ReflectionProperty ($ client , 'connector ' );
90+ $ ref ->setAccessible (true );
91+ $ ret = $ ref ->getValue ($ client );
92+
93+ $ this ->assertSame ($ connector , $ ret );
94+ }
95+
96+ public function testConstructWithLoopAssignsGivenLoop ()
97+ {
98+ $ browser = new Browser (null , $ this ->loop );
99+
100+ $ ref = new \ReflectionProperty ($ browser , 'transaction ' );
101+ $ ref ->setAccessible (true );
102+ $ transaction = $ ref ->getValue ($ browser );
103+
104+ $ ref = new \ReflectionProperty ($ transaction , 'loop ' );
105+ $ ref ->setAccessible (true );
106+ $ loop = $ ref ->getValue ($ transaction );
107+
108+ $ this ->assertSame ($ this ->loop , $ loop );
109+ }
110+
111+ public function testConstructWithLoopWithLegacySignatureAssignsGivenLoop ()
112+ {
113+ $ browser = new Browser ($ this ->loop );
114+
115+ $ ref = new \ReflectionProperty ($ browser , 'transaction ' );
116+ $ ref ->setAccessible (true );
117+ $ transaction = $ ref ->getValue ($ browser );
118+
119+ $ ref = new \ReflectionProperty ($ transaction , 'loop ' );
120+ $ ref ->setAccessible (true );
121+ $ loop = $ ref ->getValue ($ transaction );
122+
123+ $ this ->assertSame ($ this ->loop , $ loop );
124+ }
125+
126+ public function testConstructWithInvalidConnectorThrows ()
127+ {
128+ $ this ->setExpectedException ('InvalidArgumentException ' );
129+ new Browser ('foo ' );
130+ }
131+
132+ public function testConstructWithInvalidLoopThrows ()
133+ {
134+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
135+
136+ $ this ->setExpectedException ('InvalidArgumentException ' );
137+ new Browser ($ connector , 'foo ' );
138+ }
139+
140+ public function testConstructWithConnectorTwiceThrows ()
141+ {
142+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
143+
144+ $ this ->setExpectedException ('InvalidArgumentException ' );
145+ new Browser ($ connector , $ connector );
146+ }
147+
148+ public function testConstructWithLoopTwiceThrows ()
149+ {
150+ $ this ->setExpectedException ('InvalidArgumentException ' );
151+ new Browser ($ this ->loop , $ this ->loop );
152+ }
153+
46154 public function testGetSendsGetRequest ()
47155 {
48156 $ that = $ this ;
@@ -390,7 +498,7 @@ public function testCancelGetRequestShouldCancelUnderlyingSocketConnection()
390498 $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
391499 $ connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:80 ' )->willReturn ($ pending );
392500
393- $ this ->browser = new Browser ($ this ->loop , $ connector );
501+ $ this ->browser = new Browser ($ connector , $ this ->loop );
394502
395503 $ promise = $ this ->browser ->get ('http://example.com/ ' );
396504 $ promise ->cancel ();
0 commit comments