|
| 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 | +} |
0 commit comments