Fix tests to include expectation count and run user saving events

This commit is contained in:
Clark Winkelmann 2019-11-22 20:58:58 +01:00 committed by Daniël Klabbers
parent c2cd9174e4
commit c7634e48b6

View File

@ -11,6 +11,7 @@
namespace Flarum\Tests\unit\User; namespace Flarum\Tests\unit\User;
use Flarum\Tests\unit\TestCase;
use Flarum\User\AvatarUploader; use Flarum\User\AvatarUploader;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
@ -18,13 +19,9 @@ use Illuminate\Database\Eloquent\Model;
use Intervention\Image\ImageManagerStatic; use Intervention\Image\ImageManagerStatic;
use League\Flysystem\FilesystemInterface; use League\Flysystem\FilesystemInterface;
use Mockery as m; use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
class AvatarUploaderTest extends TestCase class AvatarUploaderTest extends TestCase
{ {
use MockeryPHPUnitIntegration;
private $dispatcher; private $dispatcher;
private $filesystem; private $filesystem;
private $uploader; private $uploader;
@ -41,8 +38,8 @@ class AvatarUploaderTest extends TestCase
public function test_removing_avatar_removes_file() public function test_removing_avatar_removes_file()
{ {
$this->filesystem->shouldReceive('has')->with('ABCDEFGHabcdefgh.png'); $this->filesystem->shouldReceive('has')->with('ABCDEFGHabcdefgh.png')->andReturn(true);
$this->filesystem->shouldReceive('delete')->with('ABCDEFGHabcdefgh.png')->andReturn(true); $this->filesystem->shouldReceive('delete')->with('ABCDEFGHabcdefgh.png')->once();
$user = new User(); $user = new User();
$user->changeAvatarPath('ABCDEFGHabcdefgh.png'); $user->changeAvatarPath('ABCDEFGHabcdefgh.png');
@ -51,6 +48,11 @@ class AvatarUploaderTest extends TestCase
$user->syncOriginal(); $user->syncOriginal();
$this->uploader->remove($user); $this->uploader->remove($user);
// Simulate saving
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
$callback($user);
}
$user->syncOriginal(); $user->syncOriginal();
$this->assertEquals(null, $user->getOriginal('avatar_url')); $this->assertEquals(null, $user->getOriginal('avatar_url'));
@ -58,7 +60,7 @@ class AvatarUploaderTest extends TestCase
public function test_removing_url_avatar_removes_no_file() public function test_removing_url_avatar_removes_no_file()
{ {
$this->filesystem->shouldReceive('has')->with('https://example.com/avatar.png')->andReturn(false); $this->filesystem->shouldReceive('has')->with('https://example.com/avatar.png')->andReturn(false)->once();
$this->filesystem->shouldNotReceive('delete'); $this->filesystem->shouldNotReceive('delete');
$user = new User(); $user = new User();
@ -66,6 +68,11 @@ class AvatarUploaderTest extends TestCase
$user->syncOriginal(); $user->syncOriginal();
$this->uploader->remove($user); $this->uploader->remove($user);
// Simulate saving
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
$callback($user);
}
$user->syncOriginal(); $user->syncOriginal();
$this->assertEquals(null, $user->getOriginal('avatar_url')); $this->assertEquals(null, $user->getOriginal('avatar_url'));
@ -73,15 +80,20 @@ class AvatarUploaderTest extends TestCase
public function test_changing_avatar_removes_file() public function test_changing_avatar_removes_file()
{ {
$this->filesystem->shouldReceive('put'); $this->filesystem->shouldReceive('put')->once();
$this->filesystem->shouldReceive('has')->with('ABCDEFGHabcdefgh.png')->andReturn(true); $this->filesystem->shouldReceive('has')->with('ABCDEFGHabcdefgh.png')->andReturn(true);
$this->filesystem->shouldReceive('delete')->with('ABCDEFGHabcdefgh.png'); $this->filesystem->shouldReceive('delete')->with('ABCDEFGHabcdefgh.png')->once();
$user = new User(); $user = new User();
$user->changeAvatarPath('ABCDEFGHabcdefgh.png'); $user->changeAvatarPath('ABCDEFGHabcdefgh.png');
$user->syncOriginal(); $user->syncOriginal();
$this->uploader->upload($user, ImageManagerStatic::canvas(50, 50)); $this->uploader->upload($user, ImageManagerStatic::canvas(50, 50));
// Simulate saving
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
$callback($user);
}
$user->syncOriginal(); $user->syncOriginal();
$this->assertNotEquals('ABCDEFGHabcdefgh.png', $user->getOriginal('avatar_url')); $this->assertNotEquals('ABCDEFGHabcdefgh.png', $user->getOriginal('avatar_url'));