Passed
Push — master ( b8d1dd...8de7b7 )
by Carlos
04:38
created

EstadoDocumentoTest::testDataInstalled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2021 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScripts\Test\Core\Model;
21
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
use FacturaScripts\Core\Model\EstadoDocumento;
24
use FacturaScripts\Test\Core\LogErrorsTrait;
25
use PHPUnit\Framework\TestCase;
26
27
final class EstadoDocumentoTest extends TestCase
28
{
29
    use LogErrorsTrait;
30
31
    public function testDataInstalled()
32
    {
33
        $status = new EstadoDocumento();
34
        $this->assertNotEmpty($status->all(), 'estado-documento-data-not-installed-from-csv');
35
    }
36
37
    public function testCreateNewStatus()
38
    {
39
        $status = new EstadoDocumento();
40
        $status->nombre = 'Test';
41
        $status->tipodoc = 'PresupuestoProveedor';
42
        $this->assertTrue($status->save(), 'estado-documento-cant-save');
43
        $this->assertNotNull($status->primaryColumnValue(), 'estado-documento-pk-not-stored');
44
        $this->assertTrue($status->exists(), 'estado-documento-cant-persist');
45
        $this->assertTrue($status->delete(), 'estado-documento-cant-delete');
46
    }
47
48
    public function testCreateDefaultStatus()
49
    {
50
        // get the initial default count
51
        $status = new EstadoDocumento();
52
        $where = [new DataBaseWhere('predeterminado', true)];
53
        $defaultsCount = $status->count($where);
54
55
        // create a new default status
56
        $name = 'Test default';
57
        $type = 'PresupuestoProveedor';
58
        $status->nombre = $name;
59
        $status->predeterminado = true;
60
        $status->tipodoc = $type;
61
        $this->assertTrue($status->save(), 'estado-documento-cant-save');
62
63
        // find the default on the database
64
        $where2 = [
65
            new DataBaseWhere('predeterminado', true),
66
            new DataBaseWhere('tipodoc', $type)
67
        ];
68
        $this->assertEquals(1, $status->count($where2), 'estado-documento-more-than-one-default');
69
        foreach ($status->all($where2) as $sta) {
70
            $this->assertEquals($status->idestado, $sta->idestado, 'estado-documento-not-the-right-default');
71
        }
72
73
        // check the defaults count did not change
74
        $this->assertEquals($defaultsCount, $status->count($where), 'estado-documento-defaults-count-changed');
75
76
        // remove the default status
77
        $this->assertTrue($status->delete(), 'estado-documento-cant-delete');
78
79
        // check the defaults count did not change
80
        $this->assertEquals($defaultsCount, $status->count($where), 'estado-documento-defaults-count-changed-2');
81
    }
82
83
    public function testCreateLockedStatus()
84
    {
85
        $status = new EstadoDocumento();
86
        $status->bloquear = true;
87
        $status->nombre = 'Test';
88
        $status->tipodoc = 'PresupuestoProveedor';
89
        $this->assertTrue($status->save(), 'estado-documento-cant-save');
90
        $this->assertFalse($status->delete(), 'estado-documento-lock-can-delete');
91
92
        // change properties
93
        $status->editable = false;
94
        $this->assertFalse($status->save(), 'estado-documento-lock-cant-save');
95
96
        // unlock
97
        $status->bloquear = false;
98
        $this->assertTrue($status->save(), 'estado-documento-cant-unlock');
99
100
        // delete
101
        $this->assertTrue($status->delete(), 'estado-documento-cant-delete');
102
    }
103
104
    public function testStatusCanNotHaveGenerationAndEditable()
105
    {
106
        $status = new EstadoDocumento();
107
        $status->editable = true;
108
        $status->generadoc = 'PedidoProveedor';
109
        $status->nombre = 'Generate';
110
        $status->tipodoc = 'PresupuestoCliente';
111
        $this->assertTrue($status->save(), 'estado-documento-cant-save');
112
        $this->assertFalse($status->editable, 'estado-documento-must-be-not-editable');
113
    }
114
115
    public function testCanNotCreateInvoicesWithGeneration()
116
    {
117
        $status = new EstadoDocumento();
118
        $status->generadoc = 'PedidoCliente';
119
        $status->nombre = 'Generate';
120
        $status->tipodoc = 'FacturaCliente';
121
        $this->assertFalse($status->save(), 'invalid-estado-documento-for-sales-invoice-can-save');
122
123
        $status->generadoc = 'PedidoProveedor';
124
        $status->tipodoc = 'FacturaProveedor';
125
        $this->assertFalse($status->save(), 'invalid-estado-documento-for-purchase-invoice-can-save');
126
    }
127
128
    protected function tearDown()
129
    {
130
        $this->logErrors();
131
    }
132
}
133