Skip to content

Commit

Permalink
removed cookiePrefix; added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heartsentwined committed Sep 6, 2012
1 parent 11817ba commit 693f83b
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 28 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php
php:
- "5.3"
- "5.4"
before_script: composer install
script: phpunit --configuration test/phpunit.xml
notifications:
email:
on_success: always
on_failure: always
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Copy `config/browser.local.php.dist` into `(app root)/config/autoload/browser.lo
The `browser` alias can be changed to anything you like.

- `cookieDir`: directory for storing cookies. Make sure you create this directory, and that it is writable by `www-data` (or whatever your PHP scripts run as).
- `cookiePrefix`: prefix to all cookie files
- `cookieLife`: lifetime for cookie files (minute)
- `connectTimeout`: max time to wait when connecting (second)
- `options`: wrapper for `\Zend\Http\Client::setOptions()`
Expand Down
1 change: 0 additions & 1 deletion config/browser.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ return array(
'browser' => array(
'parameters' => array(
'cookieDir' => 'data/tmp/cookie',
'cookiePrefix' => 'browserCookie-',
'cookieLife' => 1440,
'connectTimeout' => 120,
'options' => array(),
Expand Down
29 changes: 3 additions & 26 deletions src/Heartsentwined/Browser/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class Factory
{
protected $cookieDir;
protected $cookiePrefix;
protected $cookieLife;
protected $connectTimeout;
protected $options;
Expand Down Expand Up @@ -41,29 +40,6 @@ public function getCookieDir()
return $this->cookieDir;
}

/**
* prefix to all cookie files
*
* @param string $cookiePrefix
* @return $this
*/
public function setCookiePrefix($cookiePrefix)
{
ArgValidator::assert($cookiePrefix, 'string');
$this->cookiePrefix = $cookiePrefix;
return $this;
}

/**
* getCookiePrefix
*
* @return string
*/
public function getCookiePrefix()
{
return $this->cookiePrefix;
}

/**
* lifetime for cookie files
*
Expand Down Expand Up @@ -185,6 +161,8 @@ public function getWd()
*/
public function newInstance()
{
ArgValidator::assert($this->getCookieDir(), array('string', 'min' => 1));

$browser = new Browser;

if ($options = $this->getOptions() && count($options)) {
Expand All @@ -205,8 +183,7 @@ public function newInstance()
$this->setWd(getcwd());

//random cookie file
$cookieFile = $this->getCookieDir() . '/' . $this->getCookiePrefix()
. md5(rand().microtime(true));
$cookieFile = $this->getCookieDir() . '/' . md5(rand().microtime(true));
$fh = fopen($cookieFile, 'x+');
fclose($fh);

Expand Down
55 changes: 55 additions & 0 deletions test/Heartsentwined/Test/Browser/BrowserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Heartsentwined\Test\Browser;

use Heartsentwined\Browser\Factory;
use Heartsentwined\Browser\Exception;

class BrowserTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
mkdir('tmp');
$factory = new Factory;
$factory
->setCookieDir('tmp')
->setCookieLife(2)
->setConnectTimeout(120);
$this->browser = $factory->newInstance();
}

public function tearDown()
{
$handle = opendir('tmp');
while (($file = readdir($handle)) !== false) {
unlink("tmp/$file");
}
closedir($handle);
rmdir('tmp');
}

public function testGet()
{
$body = $this->browser->get('http://google.com');
$this->assertNotEmpty($body);
}

public function testIndirectGet()
{
$this->browser->setUri('http://google.com');
$body = $this->browser->get();
$this->assertNotEmpty($body);
}

public function testPost()
{
$body = $this->browser->post('http://google.com', array());
$this->assertNotEmpty($body);
}

public function testIndirectPost()
{
$this->browser->setUri('http://google.com');
$body = $this->browser->post('', array());
$this->assertNotEmpty($body);
}
}
67 changes: 67 additions & 0 deletions test/Heartsentwined/Test/Browser/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace Heartsentwined\Test\Browser;

use Heartsentwined\Browser\Factory;

class FactoryTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->factory = new Factory;
}

public function testCookie()
{
mkdir('tmp');
$this->factory
->setCookieDir('tmp')
->newInstance();

$fileCount = 0;
$handle = opendir('tmp');
while (($file = readdir($handle)) !== false) {
unlink("tmp/$file");
$fileCount++;
}
closedir($handle);
rmdir('tmp');

$this->assertSame(1, $fileCount);
}

public function testRemoveOldCookie()
{
mkdir('tmp');

$fh = fopen('tmp/deleted', 'x+');
touch('tmp/deleted', time()-10*60);
fclose($fh);

$fh = fopen('tmp/remaining', 'x+');
touch('tmp/remaining', time()-5*60);
fclose($fh);

$this->factory
->setCookieDir('tmp')
->setCookieLife(10);
unset($this->factory); //trigger destructor

$deletedCount = 0;
$remainingCount = 0;
$handle = opendir('tmp');
while (($file = readdir($handle)) !== false) {
if (strpos($file, 'deleted') !== false) {
$deletedCount++;
}
if (strpos($file, 'remaining') !== false) {
$remainingCount++;
}
unlink("tmp/$file");
}
closedir($handle);
rmdir('tmp');

$this->assertSame(0, $deletedCount);
$this->assertSame(1, $remainingCount);
}
}
3 changes: 3 additions & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$loader->add('Heartsentwined\Test', __DIR__);
7 changes: 7 additions & 0 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuites>
<testsuite>
<directory>./</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 693f83b

Please sign in to comment.