Skip to content

Commit 245ec4d

Browse files
authored
Merge pull request #126 from aik099/reduce-visibility
Reduce some "BrowserTestCase" class method visibility
2 parents 11e7abe + 85e986b commit 245ec4d

4 files changed

Lines changed: 76 additions & 49 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1212
- Allow using self-signed/invalid SSL certificates during testing on the SauceLabs by default.
1313
- Rewritten library object communication mechanism (the event dispatcher is no longer used). Update any custom session strategy/browser configuration implementations.
1414
- Reduce memory consumption by rewriting `SessionStrategyFactory` and `SessionStrategyManager` classes.
15+
- (Not a BC break) Some public methods of the `BrowserTestCase` class are protected now. Affected methods: `setRemoteCoverageScriptUrl`, `setBrowser`, `getBrowser`, `setSessionStrategy`, `getSessionStrategy`, `getCollectCodeCoverageInformation`, `getRemoteCodeCoverageInformation`.
16+
- (Not a BC break) Some protected properties of the `BrowserTestCase` class are private now. Affected properties: `sessionStrategyManager`, `remoteCoverageHelper`, `sessionStrategy`.
1517

1618
### Fixed
1719
- Don't set remote code coverage collection cookies, when the remote code coverage script URL isn't specified.

library/aik099/PHPUnit/BrowserTestCase.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ abstract class BrowserTestCase extends AbstractTestCase
7171
*
7272
* @var SessionStrategyManager
7373
*/
74-
protected $sessionStrategyManager;
74+
private $_sessionStrategyManager;
7575

7676
/**
7777
* Remote coverage helper.
7878
*
7979
* @var RemoteCoverageHelper
8080
*/
81-
protected $remoteCoverageHelper;
81+
private $_remoteCoverageHelper;
8282

8383
/**
8484
* Session strategy, used currently.
8585
*
8686
* @var ISessionStrategy
8787
*/
88-
protected $sessionStrategy;
88+
private $_sessionStrategy;
8989

9090
/**
9191
* Test ID.
@@ -115,7 +115,7 @@ public function setBrowserConfigurationFactory(IBrowserConfigurationFactory $bro
115115
*/
116116
public function setSessionStrategyManager(SessionStrategyManager $session_strategy_manager)
117117
{
118-
$this->sessionStrategyManager = $session_strategy_manager;
118+
$this->_sessionStrategyManager = $session_strategy_manager;
119119

120120
return $this;
121121
}
@@ -129,7 +129,7 @@ public function setSessionStrategyManager(SessionStrategyManager $session_strate
129129
*/
130130
public function setRemoteCoverageHelper(RemoteCoverageHelper $remote_coverage_helper)
131131
{
132-
$this->remoteCoverageHelper = $remote_coverage_helper;
132+
$this->_remoteCoverageHelper = $remote_coverage_helper;
133133
}
134134

135135
/**
@@ -139,7 +139,7 @@ public function setRemoteCoverageHelper(RemoteCoverageHelper $remote_coverage_he
139139
*
140140
* @return void
141141
*/
142-
public function setRemoteCoverageScriptUrl($url)
142+
protected function setRemoteCoverageScriptUrl($url)
143143
{
144144
$this->_remoteCoverageScriptUrl = $url;
145145
}
@@ -164,14 +164,16 @@ protected function setUpTest()
164164
*
165165
* @return self
166166
*/
167-
public function setBrowser(BrowserConfiguration $browser)
167+
protected function setBrowser(BrowserConfiguration $browser)
168168
{
169169
$this->_browser = $browser;
170170

171171
// Configure session strategy.
172-
return $this->setSessionStrategy(
173-
$this->sessionStrategyManager->getSessionStrategy($browser, $this)
172+
$this->setSessionStrategy(
173+
$this->_sessionStrategyManager->getSessionStrategy($browser, $this)
174174
);
175+
176+
return $this;
175177
}
176178

177179
/**
@@ -180,7 +182,7 @@ public function setBrowser(BrowserConfiguration $browser)
180182
* @return BrowserConfiguration
181183
* @throws \RuntimeException When browser configuration isn't defined.
182184
*/
183-
public function getBrowser()
185+
protected function getBrowser()
184186
{
185187
if ( !is_object($this->_browser) ) {
186188
throw new \RuntimeException('Browser configuration not defined');
@@ -198,7 +200,9 @@ public function getBrowser()
198200
*/
199201
public function setBrowserFromConfiguration(array $browser_config)
200202
{
201-
return $this->setBrowser($this->createBrowserConfiguration($browser_config));
203+
return $this->setBrowser(
204+
$this->createBrowserConfiguration($browser_config)
205+
);
202206
}
203207

204208
/**
@@ -220,9 +224,9 @@ protected function createBrowserConfiguration(array $browser_config)
220224
*
221225
* @return self
222226
*/
223-
public function setSessionStrategy(ISessionStrategy $session_strategy = null)
227+
protected function setSessionStrategy(ISessionStrategy $session_strategy = null)
224228
{
225-
$this->sessionStrategy = $session_strategy;
229+
$this->_sessionStrategy = $session_strategy;
226230

227231
return $this;
228232
}
@@ -233,14 +237,14 @@ public function setSessionStrategy(ISessionStrategy $session_strategy = null)
233237
* @return ISessionStrategy
234238
* @see setSessionStrategy()
235239
*/
236-
public function getSessionStrategy()
240+
protected function getSessionStrategy()
237241
{
238-
if ( $this->sessionStrategy ) {
239-
return $this->sessionStrategy;
242+
if ( $this->_sessionStrategy !== null ) {
243+
return $this->_sessionStrategy;
240244
}
241245

242246
// Default session strategy (not session itself) shared across all test cases.
243-
return $this->sessionStrategyManager->getDefaultSessionStrategy();
247+
return $this->_sessionStrategyManager->getDefaultSessionStrategy();
244248
}
245249

246250
/**
@@ -290,8 +294,8 @@ protected function tearDownTest()
290294
$this->_browser->onTestEnded($this, $result);
291295
}
292296

293-
if ( $this->sessionStrategy !== null ) {
294-
$this->sessionStrategy->onTestEnded($this);
297+
if ( $this->_sessionStrategy !== null ) {
298+
$this->_sessionStrategy->onTestEnded($this);
295299
}
296300
}
297301

@@ -301,7 +305,7 @@ protected function tearDownTest()
301305
* @return boolean
302306
* @throws \RuntimeException When used before test is started.
303307
*/
304-
public function getCollectCodeCoverageInformation()
308+
protected function getCollectCodeCoverageInformation()
305309
{
306310
if ( !$this->_remoteCoverageScriptUrl ) {
307311
return false;
@@ -341,8 +345,8 @@ protected function runTest()
341345
*/
342346
public function onTestSuiteEnded()
343347
{
344-
if ( $this->sessionStrategy !== null ) {
345-
$this->sessionStrategy->onTestSuiteEnded($this);
348+
if ( $this->_sessionStrategy !== null ) {
349+
$this->_sessionStrategy->onTestSuiteEnded($this);
346350
}
347351

348352
return $this;
@@ -353,13 +357,13 @@ public function onTestSuiteEnded()
353357
*
354358
* @return array|RawCodeCoverageData
355359
*/
356-
public function getRemoteCodeCoverageInformation()
360+
protected function getRemoteCodeCoverageInformation()
357361
{
358362
if ( $this->_remoteCoverageScriptUrl ) {
359-
return $this->remoteCoverageHelper->get($this->_remoteCoverageScriptUrl, $this->_testId);
363+
return $this->_remoteCoverageHelper->get($this->_remoteCoverageScriptUrl, $this->_testId);
360364
}
361365

362-
return $this->remoteCoverageHelper->getEmpty();
366+
return $this->_remoteCoverageHelper->getEmpty();
363367
}
364368

365369
/**
@@ -381,8 +385,8 @@ public static function suite($class_name)
381385
*/
382386
protected function onNotSuccessfulTestCompat($e)
383387
{
384-
if ( $this->sessionStrategy !== null ) {
385-
$this->sessionStrategy->onTestFailed($this, $e);
388+
if ( $this->_sessionStrategy !== null ) {
389+
$this->_sessionStrategy->onTestFailed($this, $e);
386390
}
387391
}
388392

tests/aik099/PHPUnit/BrowserTestCaseTest.php

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
use aik099\PHPUnit\MinkDriver\IMinkDriverFactory;
3434
use Behat\Mink\Session;
3535
use ConsoleHelpers\CodeCoverageCompat\Driver\Driver;
36+
use Behat\Mink\Exception\DriverException;
3637

37-
class BrowserTestCaseTest extends AbstractTestCase
38+
class BrowserTestCaseTest extends BrowserTestCase
3839
{
3940

4041
use ExpectException;
42+
use TVerifyTestExpectations;
4143

4244
const BROWSER_CLASS = BrowserConfiguration::class;
4345

@@ -76,9 +78,14 @@ public function testSetSessionStrategyManager()
7678
$manager = m::mock(self::MANAGER_CLASS);
7779

7880
$test_case = new WithoutBrowserConfig('test name');
79-
$test_case->setSessionStrategyManager($manager);
8081

81-
$property = new \ReflectionProperty($test_case, 'sessionStrategyManager');
82+
$this->assertSame(
83+
$test_case,
84+
$test_case->setSessionStrategyManager($manager),
85+
'The fluid interface doesn\'t work.'
86+
);
87+
88+
$property = new \ReflectionProperty(BrowserTestCase::class, '_sessionStrategyManager');
8289
$property->setAccessible(true);
8390

8491
$this->assertSame($manager, $property->getValue($test_case));
@@ -94,7 +101,7 @@ public function testSetBrowserCorrect()
94101
/** @var ISessionStrategy $session_strategy */
95102
$session_strategy = m::mock(self::SESSION_STRATEGY_INTERFACE);
96103

97-
$test_case = $this->getFixture($session_strategy);
104+
$test_case = $this->getFixture(true, $session_strategy);
98105

99106
$browser = new BrowserConfiguration($this->createDriverFactoryRegistry());
100107

@@ -118,6 +125,7 @@ protected function createDriverFactoryRegistry()
118125
$registry
119126
->shouldReceive('get')
120127
->with('selenium2')
128+
->once()
121129
->andReturn($driver_factory);
122130

123131
return $registry;
@@ -143,9 +151,10 @@ public function testGetBrowserNotSpecified()
143151
*/
144152
public function testSetBrowserFromConfigurationDefault()
145153
{
146-
$test_case = $this->getFixture();
154+
$test_case = $this->getFixture(true);
155+
$test_case->setBrowserConfigurationFactory($this->browserConfigurationFactory);
147156

148-
$browser = $this->getBrowser(0);
157+
$browser = $this->getBrowserMock(0);
149158
$browser_config = array('browserName' => 'safari');
150159

151160
$this->browserConfigurationFactory
@@ -200,9 +209,9 @@ public function testGetSessionStrategySharing()
200209
*
201210
* @return void
202211
*/
203-
public function testGetSession()
212+
public function testGetSessionWithAutoCreate()
204213
{
205-
$browser = $this->getBrowser(0);
214+
$browser = $this->getBrowserMock(0);
206215

207216
$expected_session1 = m::mock(Session::class);
208217
$expected_session2 = m::mock(Session::class);
@@ -211,7 +220,7 @@ public function testGetSession()
211220
$session_strategy = m::mock(self::SESSION_STRATEGY_INTERFACE);
212221
$session_strategy->shouldReceive('session')->with($browser)->andReturn($expected_session1, $expected_session2);
213222

214-
$test_case = $this->getFixture($session_strategy);
223+
$test_case = $this->getFixture(true, $session_strategy);
215224
$test_case->setBrowser($browser);
216225
$test_case->setTestResultObject(new TestResult());
217226

@@ -224,20 +233,27 @@ public function testGetSession()
224233
$this->assertSame($session1, $session2);
225234
}
226235

236+
public function testGetSessionWithoutAutoCreate()
237+
{
238+
$test_case = $this->getFixture(false);
239+
240+
$this->assertNull($test_case->getSession(false), 'The session was created upon request.');
241+
}
242+
227243
/**
228244
* Test description.
229245
*
230246
* @return void
231247
*/
232248
public function testGetSessionDriverError()
233249
{
234-
$browser = $this->getBrowser(1);
250+
$browser = $this->getBrowserMock(1);
235251

236252
/** @var ISessionStrategy $session_strategy */
237253
$session_strategy = m::mock(self::SESSION_STRATEGY_INTERFACE);
238-
$session_strategy->shouldReceive('session')->andThrow('\Behat\Mink\Exception\DriverException');
254+
$session_strategy->shouldReceive('session')->andThrow(DriverException::class);
239255

240-
$test_case = $this->getFixture($session_strategy);
256+
$test_case = $this->getFixture(true, $session_strategy);
241257
$test_case->setBrowser($browser);
242258

243259
// On PHPUnit 5.x usage of expectException/expectExceptionMessage results in this test being marked as skipped.
@@ -264,7 +280,7 @@ public function testGetSessionDriverError()
264280
*
265281
* @return BrowserConfiguration
266282
*/
267-
protected function getBrowser($times)
283+
protected function getBrowserMock($times)
268284
{
269285
$browser = m::mock(self::BROWSER_CLASS);
270286
$browser->shouldReceive('getHost')->times($times)->andReturn('{hostname}');
@@ -280,7 +296,7 @@ protected function getBrowser($times)
280296
*/
281297
public function testGetCollectCodeCoverageInformationSuccess($remote_coverage_script_url)
282298
{
283-
$test_case = $this->getFixture();
299+
$test_case = $this->getFixture(false);
284300

285301
$test_result = new TestResult();
286302

@@ -613,7 +629,7 @@ public function testOnTestFailed()
613629
/** @var ISessionStrategy $session_strategy */
614630
$session_strategy = m::mock(self::SESSION_STRATEGY_INTERFACE);
615631

616-
$test_case = $this->getFixture($session_strategy);
632+
$test_case = $this->getFixture(false, $session_strategy);
617633
$test_case->setSessionStrategy($session_strategy);
618634

619635
$exception = new \Exception('MSG_TEST');
@@ -632,7 +648,7 @@ public function testOnTestFailed()
632648
*/
633649
public function testGetBrowserAliases()
634650
{
635-
$test_case = $this->getFixture();
651+
$test_case = $this->getFixture(false);
636652

637653
$this->assertEmpty($test_case->getBrowserAliases(), 'Browser configuration aliases are empty by default');
638654
}
@@ -647,12 +663,12 @@ protected function prepareForRun()
647663
/** @var ISessionStrategy $session_strategy */
648664
$session_strategy = m::mock(self::SESSION_STRATEGY_INTERFACE);
649665

650-
$test_case = $this->getFixture($session_strategy);
666+
$test_case = $this->getFixture(true, $session_strategy);
651667
$test_case->setName('testSuccess');
652668

653669
$session_strategy->shouldReceive('onTestEnded')->with($test_case)->once();
654670

655-
$browser = $this->getBrowser(0);
671+
$browser = $this->getBrowserMock(0);
656672
$browser->shouldReceive('onTestSetup')->with($test_case)->once();
657673
$browser->shouldReceive('onTestEnded')->with($test_case, m::type(TestResult::class))->once();
658674

@@ -664,22 +680,24 @@ protected function prepareForRun()
664680
/**
665681
* Returns test case fixture.
666682
*
683+
* @param boolean $return_strategy Session strategy manager would be asked for a strategy.
667684
* @param ISessionStrategy|null $session_strategy Session strategy.
668685
*
669686
* @return WithoutBrowserConfig
670687
*/
671-
protected function getFixture(ISessionStrategy $session_strategy = null)
688+
protected function getFixture($return_strategy, ISessionStrategy $session_strategy = null)
672689
{
673690
if ( !isset($session_strategy) ) {
674691
$session_strategy = m::mock(self::SESSION_STRATEGY_INTERFACE);
675692
}
676693

677694
/** @var SessionStrategyManager $manager */
678695
$manager = m::mock(self::MANAGER_CLASS);
679-
$manager->shouldReceive('getSessionStrategy')->andReturn($session_strategy);
696+
$manager->shouldReceive('getSessionStrategy')
697+
->times($return_strategy ? 1 : 0)
698+
->andReturn($session_strategy);
680699

681700
$test_case = new WithoutBrowserConfig('test name');
682-
$test_case->setBrowserConfigurationFactory($this->browserConfigurationFactory);
683701
$test_case->setSessionStrategyManager($manager);
684702

685703
return $test_case;

0 commit comments

Comments
 (0)