|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SunAsterisk\Auth\Tests\Repositories; |
| 4 | + |
| 5 | +use SunAsterisk\Auth\Tests\TestCase; |
| 6 | +use SunAsterisk\Auth\Repositories\EloquentRepository; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Database\Eloquent\Builder; |
| 9 | +use Mockery; |
| 10 | + |
| 11 | +/** |
| 12 | + * @covers \SunAsterisk\Auth\Repositories\EloquentRepository |
| 13 | + */ |
| 14 | +final class EloquentRepositoryTest extends TestCase |
| 15 | +{ |
| 16 | + protected $model; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + $this->model = Mockery::mock(Model::class)->makePartial(); |
| 22 | + } |
| 23 | + |
| 24 | + protected function tearDown(): void |
| 25 | + { |
| 26 | + parent::tearDown(); |
| 27 | + Mockery::close(); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_provider_asks_model_to_find() |
| 31 | + { |
| 32 | + $eloquent = new EloquentRepository($this->model); |
| 33 | + $expected = (object) []; |
| 34 | + $this->model->shouldReceive('find')->with(1)->once()->andReturn($expected); |
| 35 | + $actual = $eloquent->find(1); |
| 36 | + |
| 37 | + $this->assertEquals($actual, $expected); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_create() |
| 41 | + { |
| 42 | + $eloquent = new EloquentRepository($this->model); |
| 43 | + $expected = (object) []; |
| 44 | + $this->model->shouldReceive('create')->with([])->once()->andReturn($expected); |
| 45 | + $actual = $eloquent->create([]); |
| 46 | + |
| 47 | + $this->assertEquals($actual, $expected); |
| 48 | + } |
| 49 | + |
| 50 | + public function test_update_by_id() |
| 51 | + { |
| 52 | + $eloquent = new EloquentRepository($this->model); |
| 53 | + $this->model->shouldReceive('whereId')->with(1)->once()->andReturn($this->model); |
| 54 | + $this->model->shouldReceive('update')->with([])->once()->andReturn(true); |
| 55 | + $actual = $eloquent->updateById(1, []); |
| 56 | + |
| 57 | + $this->assertTrue($actual); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_find_by_id() |
| 61 | + { |
| 62 | + $eloquent = new EloquentRepository($this->model); |
| 63 | + $expected = (object) []; |
| 64 | + $this->model->shouldReceive('find')->with(1)->once()->andReturn($expected); |
| 65 | + $actual = $eloquent->findById(1); |
| 66 | + |
| 67 | + $this->assertEquals($actual, $expected); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_find_by_attribute() |
| 71 | + { |
| 72 | + $eloquent = new EloquentRepository($this->model); |
| 73 | + $expected = (object) []; |
| 74 | + $this->model->shouldReceive('where')->with([])->once()->andReturn($this->model); |
| 75 | + $this->model->shouldReceive('first')->once()->andReturn($expected); |
| 76 | + $actual = $eloquent->findByAttribute([]); |
| 77 | + |
| 78 | + $this->assertEquals($actual, $expected); |
| 79 | + } |
| 80 | + |
| 81 | + public function test_find_by_credentials() |
| 82 | + { |
| 83 | + $eloquent = new EloquentRepository($this->model); |
| 84 | + $expected = (object) []; |
| 85 | + $this->model->shouldReceive('where')->with(Mockery::on(function ($closure) { |
| 86 | + $builder = Mockery::mock(Builder::class)->makePartial(); |
| 87 | + $builder->shouldReceive('orWhere')->with('key', 'value')->once()->andReturn($builder); |
| 88 | + $closure($builder); |
| 89 | + |
| 90 | + return true; |
| 91 | + }))->once()->andReturn($this->model); |
| 92 | + $this->model->shouldReceive('where')->with([])->once()->andReturn($this->model); |
| 93 | + $this->model->shouldReceive('first')->once()->andReturn($expected); |
| 94 | + $actual = $eloquent->findByCredentials(['key' => 'value'], []); |
| 95 | + |
| 96 | + $this->assertEquals($actual, $expected); |
| 97 | + } |
| 98 | +} |
0 commit comments