Skip to content

Commit

Permalink
Added phpunit, mockery + a ton of tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Mar 12, 2014
1 parent 9011230 commit 14f1c30
Show file tree
Hide file tree
Showing 13 changed files with 675 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,6 +1,7 @@
/bootstrap/compiled.php
/vendor
/report
composer.phar
composer.lock
.DS_Store
Thumbs.db
Thumbs.db
9 changes: 9 additions & 0 deletions app/Tricks/User.php
Expand Up @@ -30,6 +30,15 @@ class User extends Model implements UserInterface, RemindableInterface
*/
protected $hidden = [ 'password' ];

/**
* Users should not be admin by default
*
* @var array
*/
protected $attributes = [
'is_admin' => false
];

/**
* Query the user's social profile.
*
Expand Down
17 changes: 0 additions & 17 deletions app/tests/ExampleTest.php

This file was deleted.

28 changes: 28 additions & 0 deletions app/tests/Tricks/CategoryTest.php
@@ -0,0 +1,28 @@
<?php

namespace Tricks;

use Mockery;
use TestCase;

class CategoryTest
extends TestCase
{
public function tearDown()
{
Mockery::close();
}

public function testTricks()
{
$mock = Mockery::mock('Tricks\Category[belongsToMany]');

$mock
->shouldReceive('belongsToMany')
->atLeast()->once()
->with('Tricks\Trick')
->andReturn('mocked');

$this->assertEquals('mocked', $mock->tricks());
}
}
156 changes: 156 additions & 0 deletions app/tests/Tricks/Events/ViewTrickHandlerTest.php
@@ -0,0 +1,156 @@
<?php

namespace Tricks\Events;

use Mockery;
use PHPUnit_Framework_Assert as Assert;
use TestCase;

class ViewTrickHandlerTest
extends TestCase
{
public function tearDown()
{
Mockery::close();
}

public function testConstructor()
{
$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');

$storeMock = Mockery::mock('Illuminate\Session\Store');

$viewTrickHandler = new ViewTrickHandler(
$trickRepositoryMock,
$storeMock
);

$this->assertEquals(
$trickRepositoryMock,
Assert::readAttribute($viewTrickHandler, 'tricks')
);

$this->assertEquals(
$storeMock,
Assert::readAttribute($viewTrickHandler, 'session')
);
}

public function testHandle()
{
$trickMock = Mockery::mock('Tricks\Trick');

$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');

$trickRepositoryMock
->shouldReceive('incrementViews')
->atLeast()->once()
->with($trickMock)
->andReturn('mocked');

$storeMock = Mockery::mock('Illuminate\Session\Store');

$viewTrickHandlerMock = Mockery::mock('Tricks\Events\ViewTrickHandler[hasViewedTrick,storeViewedTrick]', [
$trickRepositoryMock,
$storeMock
])->shouldAllowMockingProtectedMethods();

$viewTrickHandlerMock
->shouldReceive('hasViewedTrick')
->atLeast()->once()
->andReturn(false);

$viewTrickHandlerMock
->shouldReceive('storeViewedTrick')
->atLeast()->once()
->with('mocked');

$viewTrickHandlerMock->handle($trickMock);
}

public function testHasViewedTrick()
{
$viewTrickHandlerMock = Mockery::mock('Tricks\Events\ViewTrickHandler[getViewedTricks]', [
Mockery::mock('Tricks\Repositories\TrickRepositoryInterface'),
Mockery::mock('Illuminate\Session\Store')
])
->makePartial()
->shouldAllowMockingProtectedMethods();

$time = time();

$tricks = [
"1" => $time - 50,
"2" => $time - 150,
"3" => $time
];

$viewTrickHandlerMock
->shouldReceive('getViewedTricks')
->atLeast()->once()
->andReturn($tricks);

$trickMock = Mockery::mock('Tricks\Trick')->makePartial();

$trickMock->id = 2;

$this->assertTrue($viewTrickHandlerMock->hasViewedTrick($trickMock));
}

public function testGetViewedTricks()
{
$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');

$storeMock = Mockery::mock('Illuminate\Session\Store');

$storeMock
->shouldReceive('get')
->atLeast()->once()
->with('viewed_tricks', [])
->andReturn('mocked');

$viewTrickHandlerMock = Mockery::mock('Tricks\Events\ViewTrickHandler[getViewedTricks]', [
$trickRepositoryMock,
$storeMock
])->shouldAllowMockingProtectedMethods();

$viewTrickHandlerMock
->shouldReceive('getViewedTricks')
->atLeast()->once()
->passthru();

$this->assertEquals('mocked', $viewTrickHandlerMock->getViewedTricks());
}

public function testStoreViewedTrick()
{
$id = 2;

$trickMock = Mockery::mock('Tricks\Trick')->makePartial();

$trickMock->id = $id;

$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');

$storeMock = Mockery::mock('Illuminate\Session\Store');

$storeMock
->shouldReceive('put')
->atLeast()->once()
->with('viewed_tricks.' . $id, time())
->andReturn('mocked');

$viewTrickHandlerMock = Mockery::mock('Tricks\Events\ViewTrickHandler[storeViewedTrick]', [
$trickRepositoryMock,
$storeMock
])->shouldAllowMockingProtectedMethods();

$viewTrickHandlerMock
->shouldReceive('storeViewedTrick')
->atLeast()->once()
->with($trickMock)
->passthru();

$viewTrickHandlerMock->storeViewedTrick($trickMock);
}
}

0 comments on commit 14f1c30

Please sign in to comment.