DI for ProfileController

This commit is contained in:
Phan An 2018-08-22 21:46:36 +02:00
parent 61bc21c303
commit 1be4678b25
3 changed files with 51 additions and 4 deletions

View file

@ -3,13 +3,20 @@
namespace App\Http\Controllers\API;
use App\Http\Requests\API\ProfileUpdateRequest;
use Hash;
use Illuminate\Contracts\Hashing\Hasher as Hash;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use RuntimeException;
class ProfileController extends Controller
{
private $hash;
public function __construct(Hash $hash)
{
$this->hash = $hash;
}
/**
* Get the current user's profile.
*
@ -36,7 +43,7 @@ class ProfileController extends Controller
$data = $request->only('name', 'email');
if ($request->password) {
$data['password'] = Hash::make($request->password);
$data['password'] = $this->hash->make($request->password);
}
return response()->json($request->user()->update($data));

View file

@ -32,6 +32,7 @@ class DownloadTest extends TestCase
public function a_single_song_can_be_downloaded()
{
$song = Song::first();
$this->downloadService
->shouldReceive('from')
->once()

View file

@ -3,18 +3,57 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Mockery\MockInterface;
class ProfileTest extends TestCase
{
use WithoutMiddleware;
/** @test */
public function user_can_update_his_profile()
/** @var MockInterface */
private $hash;
public function setUp()
{
parent::setUp();
$this->hash = $this->mockIocDependency(Hasher::class);
}
public function testUpdateProfileWithoutPassword()
{
$user = factory(User::class)->create();
$this->hash->shouldReceive('make')->never();
$this->putAsUser('api/me', ['name' => 'Foo', 'email' => 'bar@baz.com'], $user);
$this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']);
}
public function testUpdateProfileWithPassword()
{
/** @var User $user */
$user = factory(User::class)->create();
$this->hash
->shouldReceive('make')
->once()
->with('qux')
->andReturn('hashed');
$this->putAsUser('api/me', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
], $user);
$this->seeInDatabase('users', [
'id' => $user->id,
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'hashed',
]);
}
}