Skip to content

Commit

Permalink
Improving remote code coverage collection code
Browse files Browse the repository at this point in the history
Related to #13
  • Loading branch information
Alexander Obuhovich committed Feb 23, 2014
1 parent 80c1ca5 commit f1d448f
Show file tree
Hide file tree
Showing 24 changed files with 686 additions and 408 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,27 @@ Browser tests are executed on different machine, then one, where code coverage i
Remote server is web-server, where website used in tests is located.

1. Install [Xdebug](http://xdebug.org/) PHP extension on web-server
2. Copy `library/aik099/PHPUnit/Common/phpunit_coverage.php` into web-server's DocumentRoot directory.
3. In web-server's `php.ini` configuration file (or `.htaccess` file), configure `library/aik099/PHPUnit/Common/prepend.php` and `library/aik099/PHPUnit/Common/append.php` as the `auto_prepend_file` and `auto_append_file` setting values, respectively.
2. Copy `library/aik099/PHPUnit/RemoteCoverage/RemoteCoverageTool.php` into web-server's DocumentRoot directory.
3. Include following code before your application bootstraps:

```php
require_once 'RemoteCoverageTool.php';
\aik099\PHPUnit\RemoteCoverage\RemoteCoverageTool::init();
```

### On Test Machine
This is machine, where PHPUnit tests are being executed.

1. In test case class that extends `BrowserTestCase` class, add `protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';` to specify the URL for the `phpunit_coverage.php` script (`host` should be replaced with web server's url).
By default the `baseUrl` setting from browser configuration is used as the `remote code coverage information url`. However if a need exists to set alternative url on per-test basis, then place following code in the `setUp` method of the test case class, that extends `BrowserTestCase` class:

```php
$this->setRemoteCoverageScriptUrl('http://host/'); // `host` should be replaced with web server's url
```

### How This Works
1. each test sets a special cookie on website under test
2. when cookie is present, then `prepend.php` script collects coverage information and `append.php` stores it on disk
3. once test finishes it queries `phpunit_coverage.php` script on remote server, which in turn returns collected coverage information
2. when cookie is present, then `RemoteCoverageTool.php` script collects coverage information and stores it on disk
3. once test finishes, then `http://host/?rct_mode=output` url is accessed on remote server, which in turn returns collected coverage information
4. remote coverage information is then joined with coverage information collected locally on test machine

## Browser Configuration in Details
Expand Down Expand Up @@ -259,4 +268,4 @@ There are also corresponding `set` and `get` methods for each of mentioned above
```bash
$ curl http://getcomposer.org/installer | php
$ php composer.phar install
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use aik099\PHPUnit\BrowserTestCase;
use aik099\PHPUnit\Event\TestEndedEvent;
use aik099\PHPUnit\Event\TestEvent;
use aik099\PHPUnit\IEventDispatcherAware;
use aik099\PHPUnit\Session\ISessionStrategyFactory;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -117,6 +118,7 @@ public function __construct()
public static function getSubscribedEvents()
{
return array(
BrowserTestCase::TEST_SETUP_EVENT => array('onTestSetup', 100),
BrowserTestCase::TEST_ENDED_EVENT => array('onTestEnded', 100),
);
}
Expand All @@ -143,6 +145,7 @@ public function setEventDispatcher(EventDispatcherInterface $event_dispatcher)
public function attachToTestCase(BrowserTestCase $test_case)
{
$this->_testCase = $test_case;
$this->_testCase->setRemoteCoverageScriptUrl($this->getBaseUrl());
$this->_eventDispatcher->addSubscriber($this);

return $this;
Expand Down Expand Up @@ -519,6 +522,18 @@ protected static function arrayMergeRecursive($array1, $array2)
return $array1;
}

/**
* Hook, called from "BrowserTestCase::setUp" method.
*
* @param TestEvent $event Test event.
*
* @return void
*/
public function onTestSetup(TestEvent $event)
{

}

/**
* Hook, called from "BrowserTestCase::run" method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ public function __construct(IBrowserConfigurationFactory $browser_configuration_
parent::__construct();
}

/**
* Returns an array of event names this subscriber wants to listen to.
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
$events = parent::getSubscribedEvents();
$events[BrowserTestCase::TEST_SETUP_EVENT] = array('onTestSetup', 100);

return $events;
}

/**
* Initializes a browser with given configuration.
*
Expand Down Expand Up @@ -178,6 +165,8 @@ public function getDesiredCapabilities()
*/
public function onTestSetup(TestEvent $event)
{
parent::onTestSetup($event);

$desired_capabilities = $this->getDesiredCapabilities();
$desired_capabilities[self::NAME_CAPABILITY] = $this->getJobName($event->getTestCase());

Expand Down
47 changes: 41 additions & 6 deletions library/aik099/PHPUnit/BrowserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use aik099\PHPUnit\BrowserConfiguration\IBrowserConfigurationFactory;
use aik099\PHPUnit\Common\RemoteCoverage;
use aik099\PHPUnit\RemoteCoverage\RemoteCoverageHelper;
use aik099\PHPUnit\RemoteCoverage\RemoteCoverageTool;
use aik099\PHPUnit\Event\TestEndedEvent;
use aik099\PHPUnit\Event\TestEvent;
use aik099\PHPUnit\Event\TestFailedEvent;
Expand Down Expand Up @@ -66,7 +67,7 @@ abstract class BrowserTestCase extends \PHPUnit_Framework_TestCase implements IE
*
* @var string Override to provide code coverage data from the server
*/
protected $coverageScriptUrl;
private $_remoteCoverageScriptUrl;

/**
* Current browser configuration.
Expand All @@ -89,6 +90,13 @@ abstract class BrowserTestCase extends \PHPUnit_Framework_TestCase implements IE
*/
protected $sessionStrategyManager;

/**
* Remote coverage helper.
*
* @var RemoteCoverageHelper
*/
protected $remoteCoverageHelper;

/**
* Session strategy, used currently.
*
Expand Down Expand Up @@ -141,6 +149,30 @@ public function setSessionStrategyManager(SessionStrategyManager $session_strate
return $this;
}

/**
* Sets remote coverage helper.
*
* @param RemoteCoverageHelper $remote_coverage_helper Remote coverage helper.
*
* @return void
*/
public function setRemoteCoverageHelper(RemoteCoverageHelper $remote_coverage_helper)
{
$this->remoteCoverageHelper = $remote_coverage_helper;
}

/**
* Sets base url for remote coverage information collection.
*
* @param string $url URL.
*
* @return void
*/
public function setRemoteCoverageScriptUrl($url)
{
$this->_remoteCoverageScriptUrl = $url;
}

/**
* Set session meta-info for "Sauce Labs".
*
Expand Down Expand Up @@ -332,8 +364,8 @@ protected function runTest()
$this->_testId = get_class($this) . '__' . $this->getName();

$session = $this->getSession();
$session->setCookie('PHPUNIT_SELENIUM_TEST_ID', null);
$session->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->_testId);
$session->setCookie(RemoteCoverageTool::TEST_ID_VARIABLE, null);
$session->setCookie(RemoteCoverageTool::TEST_ID_VARIABLE, $this->_testId);
}

return parent::runTest();
Expand All @@ -358,12 +390,15 @@ public function onTestSuiteEnded()
* Returns remote code coverage information.
*
* @return array
* @throws \RuntimeException When no remote coverage script URL set.
*/
public function getRemoteCodeCoverageInformation()
{
$remote_coverage = new RemoteCoverage($this->coverageScriptUrl, $this->_testId);
if ( $this->_remoteCoverageScriptUrl == '' ) {
throw new \RuntimeException('Remote coverage script url not set');
}

return $remote_coverage->get();
return $this->remoteCoverageHelper->get($this->_remoteCoverageScriptUrl, $this->_testId);
}

/**
Expand Down
81 changes: 0 additions & 81 deletions library/aik099/PHPUnit/Common/append.php

This file was deleted.

100 changes: 0 additions & 100 deletions library/aik099/PHPUnit/Common/phpunit_coverage.php

This file was deleted.

Loading

0 comments on commit f1d448f

Please sign in to comment.