Skip to content
This repository has been archived by the owner on Aug 12, 2019. It is now read-only.

Commit

Permalink
tests: refactored to use @dataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Jan 4, 2015
1 parent e048d4d commit a9a503d
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions tests/cases/LinkFactoryTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,62 @@ require __DIR__ . '/../bootstrap.php';
class LinkFactoryTest extends Tester\TestCase
{

public function testLink()
/**
* @dataProvider provideLinkData
*/
public function testLink($dest, $destParams, $requestPresenter, $requestParams, $finalUrl)
{
$this->assertLink(
'Foo:bar', array('a' => 'b'),
'Foo', array('a' => 'b', 'action' => 'bar'),
'/basepath/foo/bar?a=b'
);
$refUrl = new Url('http://example.com/basepath/');

$this->assertLink(
'//Foo:bar#anchor', array('a' => 'b'),
'Foo', array('a' => 'b', 'action' => 'bar'),
'http://example.com/basepath/foo/bar?a=b#anchor'
);
$appRequestMatcher = Mockery::on(function (Request $appRequest) use ($requestPresenter, $requestParams) {
Assert::same($requestPresenter, $appRequest->getPresenterName());
Assert::same($requestParams, $appRequest->getParameters());
Assert::same('GET', $appRequest->getMethod());
Assert::same(array(), $appRequest->getPost());
Assert::same(array(), $appRequest->getFiles());
return TRUE;
});

$router = Mockery::mock('Nette\Application\IRouter')
->shouldReceive('constructUrl')->with($appRequestMatcher, $refUrl)
->andReturnUsing(array(new Route('<presenter>/<action>'), 'constructUrl'))
->getMock();

$request = Mockery::mock('Nette\Http\IRequest')
->shouldReceive('getUrl')->withNoArgs()
->andReturn($refUrl)
->getMock();

$this->assertLink(
'Admin:Dashboard:default', array(),
'Admin:Dashboard', array('action' => 'default'),
'/basepath/admin.dashboard/default'
$factory = new LinkFactory($router, $request);
Assert::same($finalUrl, $factory->link($dest, $destParams));
}


public function provideLinkData()
{
return array(
array(
'Foo:bar', array('a' => 'b'),
'Foo', array('a' => 'b', 'action' => 'bar'),
'/basepath/foo/bar?a=b'
),
array(
'//Foo:bar#anchor', array('a' => 'b'),
'Foo', array('a' => 'b', 'action' => 'bar'),
'http://example.com/basepath/foo/bar?a=b#anchor'
),
array(
'Admin:Dashboard:default', array(),
'Admin:Dashboard', array('action' => 'default'),
'/basepath/admin.dashboard/default'
),
);
}


public function testInvalidLink()
{
$url = new Url('http://example.com/basepath/');
$refUrl = new Url('http://example.com/basepath/');

$router = Mockery::mock('Nette\Application\IRouter')
->shouldReceive('constructUrl')
Expand All @@ -52,7 +83,7 @@ class LinkFactoryTest extends Tester\TestCase

$request = Mockery::mock('Nette\Http\IRequest')
->shouldReceive('getUrl')
->andReturn($url)
->andReturn($refUrl)
->getMock();

Assert::exception(
Expand All @@ -65,37 +96,6 @@ class LinkFactoryTest extends Tester\TestCase
);
}


public function assertLink($dest, $destParams, $requestPresenter, $requestParams, $finalUrl)
{
$url = new Url('http://example.com/basepath/');

$realRouter = new Route('<presenter>/<action>');

$router = Mockery::mock('Nette\Application\IRouter')
->shouldReceive('constructUrl')->with(
Mockery::on(function (Request $appRequest) use ($requestPresenter, $requestParams) {
Assert::same($requestPresenter, $appRequest->getPresenterName());
Assert::same($requestParams, $appRequest->getParameters());
Assert::same('GET', $appRequest->getMethod());
Assert::same(array(), $appRequest->getPost());
Assert::same(array(), $appRequest->getFiles());
return TRUE;
}),
$url
)
->andReturnUsing(array($realRouter, 'constructUrl'))
->getMock();

$request = Mockery::mock('Nette\Http\IRequest')
->shouldReceive('getUrl')
->andReturn($url)
->getMock();

$factory = new LinkFactory($router, $request);
Assert::same($finalUrl, $factory->link($dest, $destParams));
}

}

$test = new LinkFactoryTest;
Expand Down

0 comments on commit a9a503d

Please sign in to comment.