Skip to content

Commit 535b3fb

Browse files
committed
Merge pull request #37 from chrispitt/add-some-tests
Add some tests
2 parents 6c934f7 + ca9a989 commit 535b3fb

File tree

9 files changed

+810
-83
lines changed

9 files changed

+810
-83
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[*]
2+
end_of_line = lf
3+
insert_final_newline = true
4+
indent_style = space
5+
indent_size = 2

app/Tricks/Filters/TrickOwnerFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function filter($route)
6868
*
6969
* @return int
7070
*/
71-
private function getUserId()
71+
protected function getUserId()
7272
{
7373
return $this->auth->user()->id;
7474
}
@@ -78,7 +78,7 @@ private function getUserId()
7878
* @param \Illuminate\Routing\Route $route
7979
* @return string
8080
*/
81-
private function getSlug($route)
81+
protected function getSlug($route)
8282
{
8383
return $route->getParameter('trick_slug');
8484
}
@@ -90,7 +90,7 @@ private function getSlug($route)
9090
* @param int $userId
9191
* @return bool
9292
*/
93-
private function isTrickOwnedByUser($slug, $userId)
93+
protected function isTrickOwnedByUser($slug, $userId)
9494
{
9595
return $this->tricks->isTrickOwnedByUser($slug, $userId);
9696
}

app/Tricks/Repositories/Eloquent/ProfileRepository.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ public function createFromGithubData(OAuthUser $details, User $user, $token)
4343
{
4444
$profile = $this->getNew();
4545

46-
$profile->uid = $details->uid;
47-
$profile->username = $details->nickname;
48-
$profile->name = $details->name;
49-
$profile->email = $details->email;
50-
$profile->first_name = $details->first_name;
51-
$profile->last_name = $details->last_name;
52-
$profile->location = $details->location;
53-
$profile->description = $details->description;
54-
$profile->image_url = $details->imageUrl;
55-
//$profile->urls = serialize($details->urls);
46+
$profile->uid = $details->uid;
47+
$profile->username = $details->nickname;
48+
$profile->name = $details->name;
49+
$profile->email = $details->email;
50+
$profile->first_name = $details->first_name;
51+
$profile->last_name = $details->last_name;
52+
$profile->location = $details->location;
53+
$profile->description = $details->description;
54+
$profile->image_url = $details->imageUrl;
5655
$profile->access_token = $token;
56+
$profile->user = $user;
5757

58-
$profile = $user->profile()->save($profile);
58+
$profile->save();
5959

6060
return $profile;
6161
}

app/tests/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public function getProtectedProperty($class, $property)
2727
{
2828
return Assert::readAttribute($class, $property);
2929
}
30+
31+
protected function incomplete($message = 'This test has not been implemented yet.')
32+
{
33+
$this->markTestIncomplete($message);
34+
}
3035
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
namespace Tricks\Filters;
4+
5+
use Mockery;
6+
use TestCase;
7+
8+
class TrickOwnerFilterTest
9+
extends TestCase
10+
{
11+
public function tearDown()
12+
{
13+
Mockery::close();
14+
}
15+
16+
/**
17+
* @group tricks/filters
18+
*/
19+
public function testConstructor()
20+
{
21+
$authManagerMock = Mockery::mock('Illuminate\Auth\AuthManager');
22+
23+
$redirectorMock = Mockery::mock('Illuminate\Routing\Redirector');
24+
25+
$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');
26+
27+
$trickOwnerFilter = new TrickOwnerFilter(
28+
$authManagerMock,
29+
$redirectorMock,
30+
$trickRepositoryMock
31+
);
32+
33+
$this->assertSame(
34+
$authManagerMock,
35+
$this->getProtectedProperty($trickOwnerFilter, 'auth')
36+
);
37+
38+
$this->assertSame(
39+
$redirectorMock,
40+
$this->getProtectedProperty($trickOwnerFilter, 'redirect')
41+
);
42+
43+
$this->assertSame(
44+
$trickRepositoryMock,
45+
$this->getProtectedProperty($trickOwnerFilter, 'tricks')
46+
);
47+
}
48+
49+
/**
50+
* @group tricks/filters
51+
*/
52+
public function testFilter()
53+
{
54+
$routeMock1 = Mockery::mock('Illuminate\Routing\Route');
55+
56+
$routeMock2 = Mockery::mock('Illuminate\Routing\Route');
57+
58+
$authManagerMock = Mockery::mock('Illuminate\Auth\AuthManager');
59+
60+
$redirectorMock = Mockery::mock('Illuminate\Routing\Redirector');
61+
62+
$redirectorMock
63+
->shouldReceive('route')
64+
->atLeast()->once()
65+
->with('browse.recent')
66+
->andReturn('mocked route');
67+
68+
$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');
69+
70+
$trickOwnerFilterMock = Mockery::mock('Tricks\Filters\TrickOwnerFilter', [
71+
$authManagerMock,
72+
$redirectorMock,
73+
$trickRepositoryMock
74+
])
75+
->shouldAllowMockingProtectedMethods()
76+
->makePartial();
77+
78+
$trickOwnerFilterMock
79+
->shouldReceive('getSlug')
80+
->atLeast()->once()
81+
->with($routeMock1)
82+
->andReturn('mocked route1 getSlug');
83+
84+
$trickOwnerFilterMock
85+
->shouldReceive('getSlug')
86+
->atLeast()->once()
87+
->with($routeMock2)
88+
->andReturn('mocked route2 getSlug');
89+
90+
$trickOwnerFilterMock
91+
->shouldReceive('getUserId')
92+
->atLeast()->once()
93+
->andReturn(1);
94+
95+
$trickOwnerFilterMock
96+
->shouldReceive('isTrickOwnedByUser')
97+
->atLeast()->once()
98+
->with('mocked route1 getSlug', 1)
99+
->andReturn(true);
100+
101+
$trickOwnerFilterMock
102+
->shouldReceive('isTrickOwnedByUser')
103+
->atLeast()->once()
104+
->with('mocked route2 getSlug', 1)
105+
->andReturn(false);
106+
107+
$this->assertNull(
108+
$trickOwnerFilterMock->filter($routeMock1)
109+
);
110+
111+
$this->assertEquals(
112+
'mocked route',
113+
$trickOwnerFilterMock->filter($routeMock2)
114+
);
115+
}
116+
117+
/**
118+
* @group tricks/filters
119+
*/
120+
public function testGetUserId()
121+
{
122+
$authManagerMock = Mockery::mock('Illuminate\Auth\AuthManager');
123+
124+
$authManagerMock
125+
->shouldReceive('user')
126+
->atLeast()->once()
127+
->andReturn($authManagerMock);
128+
129+
$authManagerMock->id = 1;
130+
131+
$redirectorMock = Mockery::mock('Illuminate\Routing\Redirector');
132+
133+
$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');
134+
135+
$trickOwnerFilterMock = Mockery::mock('Tricks\Filters\TrickOwnerFilter', [
136+
$authManagerMock,
137+
$redirectorMock,
138+
$trickRepositoryMock
139+
])
140+
->shouldAllowMockingProtectedMethods()
141+
->makePartial();
142+
143+
$this->assertEquals(
144+
1,
145+
$trickOwnerFilterMock->getUserId()
146+
);
147+
}
148+
149+
/**
150+
* @group tricks/filters
151+
*/
152+
public function testGetSlug()
153+
{
154+
$routeMock = Mockery::mock('Illuminate\Routing\Route');
155+
156+
$routeMock
157+
->shouldReceive('getParameter')
158+
->atLeast()->once()
159+
->with('trick_slug')
160+
->andReturn('mocked getParameter');
161+
162+
$trickOwnerFilterMock = Mockery::mock('Tricks\Filters\TrickOwnerFilter', [
163+
Mockery::mock('Illuminate\Auth\AuthManager'),
164+
Mockery::mock('Illuminate\Routing\Redirector'),
165+
Mockery::mock('Tricks\Repositories\TrickRepositoryInterface')
166+
])
167+
->shouldAllowMockingProtectedMethods()
168+
->makePartial();
169+
170+
$this->assertEquals(
171+
'mocked getParameter',
172+
$trickOwnerFilterMock->getSlug($routeMock)
173+
);
174+
}
175+
176+
/**
177+
* @group tricks/filters
178+
*/
179+
public function testIsTrickOwnedByUser()
180+
{
181+
$authManagerMock = Mockery::mock('Illuminate\Auth\AuthManager');
182+
183+
$redirectorMock = Mockery::mock('Illuminate\Routing\Redirector');
184+
185+
$trickRepositoryMock = Mockery::mock('Tricks\Repositories\TrickRepositoryInterface');
186+
187+
$trickRepositoryMock
188+
->shouldReceive('isTrickOwnedByUser')
189+
->atLeast()->once()
190+
->with('foo', 1)
191+
->andReturn('mocked isTrickOwnedByUser');
192+
193+
$trickOwnerFilterMock = Mockery::mock('Tricks\Filters\TrickOwnerFilter', [
194+
$authManagerMock,
195+
$redirectorMock,
196+
$trickRepositoryMock
197+
])
198+
->shouldAllowMockingProtectedMethods()
199+
->makePartial();
200+
201+
$this->assertEquals(
202+
'mocked isTrickOwnedByUser',
203+
$trickOwnerFilterMock->isTrickOwnedByUser('foo', 1)
204+
);
205+
}
206+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tricks\Repositories\Eloquent;
4+
5+
use Mockery;
6+
use TestCase;
7+
8+
class ConcreteRepository
9+
extends AbstractRepository
10+
{
11+
12+
}
13+
14+
class AbstractRepositoryTest
15+
extends TestCase
16+
{
17+
public function tearDown()
18+
{
19+
Mockery::close();
20+
}
21+
22+
/**
23+
* @group tricks/repositories
24+
*/
25+
public function testConstructor()
26+
{
27+
$modelMock = Mockery::mock('Illuminate\Database\Eloquent\Model')
28+
->makePartial();
29+
30+
$concreteRepository = new ConcreteRepository($modelMock);
31+
32+
$this->assertSame(
33+
$modelMock,
34+
$this->getProtectedProperty($concreteRepository, 'model')
35+
);
36+
}
37+
38+
/**
39+
* @group tricks/repositories
40+
*/
41+
public function testGetNew()
42+
{
43+
$data = [
44+
'foo' => 1,
45+
'bar' => 2,
46+
'baz' => 3
47+
];
48+
49+
$modelMock = Mockery::mock('Illuminate\Database\Eloquent\Model')
50+
->makePartial();
51+
52+
$modelMock
53+
->shouldReceive('newInstance')
54+
->atLeast()->once()
55+
->with($data)
56+
->andReturn('mocked newInstance');
57+
58+
$concreteRepository = new ConcreteRepository($modelMock);
59+
60+
$this->assertSame(
61+
'mocked newInstance',
62+
$concreteRepository->getNew($data)
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)