Skip to content

Commit

Permalink
Start of unit test structure, minor refactor of autoloading
Browse files Browse the repository at this point in the history
  • Loading branch information
dstockto committed Jan 23, 2013
1 parent 0c934a7 commit 4060cee
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.idea/
.idea/.name .idea/.name
.idea/codeStyleSettings.xml .idea/codeStyleSettings.xml
.idea/encodings.xml .idea/encodings.xml
Expand Down
25 changes: 25 additions & 0 deletions Service/Autoload.php
@@ -0,0 +1,25 @@
<?php
namespace Joindin\Service;

class Autoload
{
/**
* Autoloader for joind.in classes
*
* @param string $class Class name to load
*
* @return void
*/
public static function autoload($class)
{
if (in_array('Joindin', explode('\\', $class))) {
// Convert namespace to directory separators

$path = str_replace('\\', '/', $class);

// Trim off Joindin
$path = substr($path, 8);
require_once(__DIR__ . '/../'.$path.'.php');
}
}
}
77 changes: 77 additions & 0 deletions tests/Model/Collection/EventTest.php
@@ -0,0 +1,77 @@
<?php
namespace test\Model\API\Collection;

class EventTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests that variables are properly set when retrieve is called with no parameters
*
* @return void
*
* @test
*/
public function defaultRetrievalParametersAreSet()
{
$mockEvent = $this->getMock('Joindin\Model\Collection\Event', array('apiGet'));
$mockEvent->expects($this->once())
->method('apiGet')
->with('http://api.joind.in/v2.1/events?resultsperpage=10&page=1')
->will($this->returnValue(json_encode(array('events' => array(), 'meta' => array()))));

$mockEvent->retrieve();
}

/**
* Ensures that setting a limit will make the URL correctly
*
* @return void
*
* @test
*/
public function retrievalWithLimitSetsParamsCorrectly()
{
$mockEvent = $this->getMock('Joindin\Model\Collection\Event', array('apiGet'));
$mockEvent->expects($this->once())
->method('apiGet')
->with('http://api.joind.in/v2.1/events?resultsperpage=75&page=1')
->will($this->returnValue(json_encode(array('events' => array(), 'meta' => array()))));

$mockEvent->retrieve(75);
}

/**
* Ensures that asking for a different page sets params correctly
*
* @return void
*
* @test
*/
public function retrievalWithPageValueSetsParamsCorrectly()
{
$mockEvent = $this->getMock('Joindin\Model\Collection\Event', array('apiGet'));
$mockEvent->expects($this->once())
->method('apiGet')
->with('http://api.joind.in/v2.1/events?resultsperpage=32&page=6')
->will($this->returnValue(json_encode(array('events' => array(), 'meta' => array()))));

$mockEvent->retrieve(32, 6);
}

/**
* Ensures that setting all 3 params sets everything correctly
*
* @return void
*
* @test
*/
public function retrievalWithFilterSetsAllParamsCorrectly()
{
$mockEvent = $this->getMock('Joindin\Model\Collection\Event', array('apiGet'));
$mockEvent->expects($this->once())
->method('apiGet')
->with('http://api.joind.in/v2.1/events?resultsperpage=16&page=3&filter=samoflange')
->will($this->returnValue(json_encode(array('events' => array(), 'meta' => array()))));

$mockEvent->retrieve(16, 3, 'samoflange');
}
}
5 changes: 5 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,5 @@
<?php
require_once __DIR__ . '/../Service/Autoload.php';

spl_autoload_register('Joindin\Service\Autoload::autoload');

11 changes: 11 additions & 0 deletions tests/phpunit.xml
@@ -0,0 +1,11 @@
<phpunit bootstrap="./bootstrap.php">
<testsuite name="Joind.in responsive Test Suite">
<directory>.</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhiteList="true">
<directory suffix=".php">.</directory>
</whitelist>
</filter>
</phpunit>
13 changes: 3 additions & 10 deletions web/index.php
@@ -1,7 +1,9 @@
<?php <?php
namespace Joindin; namespace Joindin;


spl_autoload_register('Joindin\autoload'); require_once '../Service/Autoload.php';

spl_autoload_register('Joindin\Service\autoload::autoload');


session_cache_limiter(false); session_cache_limiter(false);
session_start(); session_start();
Expand Down Expand Up @@ -31,12 +33,3 @@


// execute application // execute application
$app->run(); $app->run();

function autoload($class)
{
if (in_array('Joindin', explode('\\', $class))) {
$path = str_replace('\\', '/', $class);
$path = substr($path, 8);
require_once('../'.$path.'.php');
}
}

0 comments on commit 4060cee

Please sign in to comment.