summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Migration/MigrateStatusFlagsTest.php
blob: d71f1a54c7a3cb8023d4fedc4bd3f200b651bdde (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Daniel Opitz <dev@copynpaste.de>
 * @copyright Daniel Opitz 2017
 */

namespace OCA\News\Tests\Unit\Migration;

use Doctrine\DBAL\Driver\Statement;
use OCA\News\Migration\MigrateStatusFlags;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use Test\TestCase;

class MigrateStatusFlagsTest extends TestCase
{

    /**
     * @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $db;
    /**
     * @var IConfig|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $config;
    /**
     * @var IOutput|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $output;

    protected function setUp(): void
    {
        $this->db = $this->createMock(IDBConnection::class);
        $this->config = $this->createMock(IConfig::class);
        $this->output = $this->createMock(IOutput::class);
    }

    public function testRun()
    {
        $statement = $this->createMock(Statement::class);
        $statement->expects($this->exactly(1))
            ->method('execute')
            ->with()
            ->willReturn(true);

        $this->config->expects($this->exactly(1))
            ->method('getAppValue')
            ->with('news', 'installed_version', '0.0.0')
            ->willReturn('11.0.5');

        $sql = 'UPDATE `*PREFIX*news_items` '
            . 'SET `unread` = ((`status` & 2) = 2), '
            . '`starred` = ((`status` & 4) = 4)';

        $this->db->expects($this->exactly(1))
            ->method('prepare')
            ->with($sql)
            ->willReturn($statement);

        $migration = new MigrateStatusFlags($this->db, $this->config);
        $migration->run($this->output);
    }

    public function testRunException()
    {
        $this->expectException('\Exception');
        $this->expectExceptionMessage('Could not migrate status');

        $statement = $this->createMock(Statement::class);
        $statement->expects($this->exactly(1))
            ->method('execute')
            ->with()
            ->willReturn(false);

        $this->config->expects($this->exactly(1))
            ->method('getAppValue')
            ->with('news', 'installed_version', '0.0.0')
            ->willReturn('11.0.5');

        $sql = 'UPDATE `*PREFIX*news_items` '
            . 'SET `unread` = ((`status` & 2) = 2), '
            . '`starred` = ((`status` & 4) = 4)';

        $this->db->expects($this->exactly(1))
            ->method('prepare')
            ->with($sql)
            ->willReturn($statement);

        $migration = new MigrateStatusFlags($this->db, $this->config);
        $migration->run($this->output);
    }

    public function testRunNewerVersion()
    {
        $this->config->expects($this->exactly(1))
            ->method('getAppValue')
            ->with('news', 'installed_version', '0.0.0')
            ->willReturn('11.1.0');
        $this->db->expects($this->exactly(0))
            ->method('prepare');

        $migration = new MigrateStatusFlags($this->db, $this->config);
        $migration->run($this->output);
    }
}