Skip to content

Commit

Permalink
And account for bad experiment names too
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Nov 10, 2015
1 parent c672779 commit 91e8c63
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SeatGeek/Sixpack/Session/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SeatGeek\Sixpack\Session;

use SeatGeek\Sixpack\Response;
use SeatGeek\Sixpack\Session\Exception\InvalidExperimentNameException;
use SeatGeek\Sixpack\Session\Exception\InvalidForcedAlternativeException;
use \InvalidArgumentException;

Expand Down Expand Up @@ -209,7 +210,7 @@ protected function getIpAddress()
protected function sendRequest($endpoint, $params = array())
{
if (isset($params["experiment"]) && !preg_match('#^[a-z0-9][a-z0-9\-_ ]*$#i', $params["experiment"])) {
throw new \Exception("Invalid Experiment Name: " . $params["experiment"]);
throw new InvalidExperimentNameException($params["experiment"]);
}

$params = array_merge(array(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace SeatGeek\Sixpack\Session\Exception;

use \Exception;

/**
* Used when an experiement name is deemed invalid
*/
class InvalidExperimentNameException extends Exception
{
/**
* The sprintf pattern for when an exception is thrown
*
* @var string
*/
protected $messageTemplate = "The experiement name \"%s\" is invalid";

/**
* Constructor
*
* @param string|array If passed, it's the experiment name
* @param int $code The exception code
* @param \Exception $previous The previous exception
*/
public function __construct($message = null, $code = 0, Exception $previous = null)
{
if ($message) {
$message = sprintf($this->messageTemplate, $message);
}
return parent::__construct($message, $code, $previous);
}
}
18 changes: 18 additions & 0 deletions tests/Session/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,22 @@ public function testParticipateInvalidForcedAlternative()

$base->participate('experiment', ['one', 'two']);
}

/**
* Verify what happens with a bad experiment name
*
* @expectedException \SeatGeek\Sixpack\Session\Exception\InvalidExperimentNameException
* @expectedExceptionMessage The experiement name "experiments; the final frontier" is invalid
*/
public function testParticipateInvalidExperimentName()
{
$_GET['sixpack-force-experiment'] = 'not configured';

$base = $this->getMockBuilder('SeatGeek\Sixpack\Session\Base')
->disableOriginalConstructor()
->setMethods(['nothing'])
->getMock();

$base->participate('experiments; the final frontier', ['one', 'two']);
}
}

0 comments on commit 91e8c63

Please sign in to comment.