summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/BaseModelTest.php
blob: 963ea64aa13cf2165a3903a3a9fcdf8019acd595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace Engelsystem\Test\Unit\Models;

use Engelsystem\Test\Unit\Models\Stub\BaseModelImplementation;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class BaseModelTest extends TestCase
{
    /**
     * @covers \Engelsystem\Models\BaseModel::create
     */
    public function testCreate()
    {
        $model = new BaseModelImplementation();
        $newModel = $model->create(['foo' => 'bar']);

        $this->assertNotEquals($model, $newModel);
        $this->assertEquals('bar', $newModel->foo);
        $this->assertEquals(1, $newModel->saveCount);
    }

    /**
     * @covers \Engelsystem\Models\BaseModel::find
     */
    public function testFind()
    {
        /** @var QueryBuilder|MockObject $queryBuilder */
        $queryBuilder = $this->createMock(QueryBuilder::class);
        BaseModelImplementation::$queryBuilder = $queryBuilder;

        $anotherModel = new BaseModelImplementation();

        $queryBuilder->expects($this->once())
            ->method('find')
            ->with(1337, ['foo', 'bar'])
            ->willReturn($anotherModel);

        $model = new BaseModelImplementation();
        $newModel = $model->find(1337, ['foo', 'bar']);

        $this->assertEquals($anotherModel, $newModel);
    }

    /**
     * @covers \Engelsystem\Models\BaseModel::findOrNew
     */
    public function testFindOrNew()
    {
        /** @var QueryBuilder|MockObject $queryBuilder */
        $queryBuilder = $this->createMock(QueryBuilder::class);
        BaseModelImplementation::$queryBuilder = $queryBuilder;

        $anotherModel = new BaseModelImplementation();

        $queryBuilder->expects($this->once())
            ->method('findOrNew')
            ->with(31337, ['lorem', 'ipsum'])
            ->willReturn($anotherModel);

        $model = new BaseModelImplementation();
        $newModel = $model->findOrNew(31337, ['lorem', 'ipsum']);

        $this->assertEquals($anotherModel, $newModel);
    }
}