Permalink
Browse files

Added phpunit, mockery + a ton of tests.

  • Loading branch information...
1 parent 9011230 commit 14f1c3032a71d6efc605523c1b953efc888db269 @assertchris assertchris committed Mar 12, 2014
View
@@ -1,6 +1,7 @@
/bootstrap/compiled.php
/vendor
+/report
composer.phar
composer.lock
.DS_Store
-Thumbs.db
+Thumbs.db
View
@@ -31,6 +31,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.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
@@ -1,17 +0,0 @@
-<?php
-
-class ExampleTest extends TestCase {
-
- /**
- * A basic functional test example.
- *
- * @return void
- */
- public function testBasicExample()
- {
- $crawler = $this->client->request('GET', '/');
-
- $this->assertTrue($this->client->getResponse()->isOk());
- }
-
-}
@@ -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());
+ }
+}
@@ -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);
+ }
+}
Oops, something went wrong.

0 comments on commit 14f1c30

Please sign in to comment.