Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.49% covered (success)
96.49%
55 / 57
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_console_command_check_test
96.49% covered (success)
96.49%
55 / 57
83.33% covered (warning)
83.33%
5 / 6
8
0.00% covered (danger)
0.00%
0 / 1
 test_up_to_date
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_up_to_date_verbose
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_not_up_to_date
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_not_up_to_date_verbose
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 test_error
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
1.04
 get_command_tester
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14use Symfony\Component\Console\Application;
15use Symfony\Component\Console\Tester\CommandTester;
16use phpbb\console\command\update\check;
17
18require_once __DIR__ . '/../../../phpBB/includes/functions_admin.php';
19require_once __DIR__ . '/../../../phpBB/includes/functions.php';
20require_once __DIR__ . '/../../../phpBB/includes/utf/utf_tools.php';
21
22/**
23* @slow
24*/
25class phpbb_console_command_check_test extends phpbb_test_case
26{
27    protected $version_helper;
28
29    /** @var \phpbb\language\language */
30    protected $language;
31
32    public function test_up_to_date()
33    {
34        $command_tester = $this->get_command_tester('100000');
35        $status = $command_tester->execute(array('--no-ansi' => true));
36        $this->assertSame('', $command_tester->getDisplay());
37        $this->assertSame($status, 0);
38    }
39
40    public function test_up_to_date_verbose()
41    {
42        $command_tester = $this->get_command_tester('100000');
43        $status = $command_tester->execute(array('--no-ansi' => true, '--verbose' => true));
44        $this->assertStringContainsString($this->language->lang('UPDATE_NOT_NEEDED'), $command_tester->getDisplay());
45        $this->assertSame($status, 0);
46    }
47
48
49    public function test_not_up_to_date()
50    {
51        $command_tester = $this->get_command_tester('0');
52        $status = $command_tester->execute(array('--no-ansi' => true));
53        $this->assertStringContainsString($this->language->lang('UPDATE_NEEDED'), $command_tester->getDisplay());
54        $this->assertSame($status, 1);
55    }
56
57    public function test_not_up_to_date_verbose()
58    {
59        $command_tester = $this->get_command_tester('0');
60        $status = $command_tester->execute(array('--no-ansi' => true, '--verbose' => true));
61        $this->assertStringContainsString($this->language->lang('UPDATE_NEEDED'), $command_tester->getDisplay());
62        $this->assertStringContainsString($this->language->lang('UPDATES_AVAILABLE'), $command_tester->getDisplay());
63        $this->assertSame($status, 1);
64    }
65
66    public function test_error()
67    {
68        $this->expectException(\phpbb\exception\runtime_exception::class);
69
70        $command_tester = $this->get_command_tester('1');
71        $this->version_helper->set_file_location('acme.corp','foo', 'bar.json');
72
73        $status = $command_tester->execute(array('--no-ansi' => true));
74        $this->assertStringContainsString('VERSIONCHECK_FAIL', $command_tester->getDisplay());
75        $this->assertSame($status, 2);
76    }
77
78    public function get_command_tester($current_version)
79    {
80        global $user, $phpbb_root_path, $phpEx;
81
82        $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
83
84        $user = $this->createMock('\phpbb\user');
85        $user->method('lang')->will($this->returnArgument(0));
86
87        $cache = $this->getMockBuilder('\phpbb\cache\service')
88            ->disableOriginalConstructor()
89            ->getMock();
90
91        $config = new \phpbb\config\config(array('version' => $current_version));
92        $this->version_helper = $this->getMockBuilder('\phpbb\version_helper')
93            ->setConstructorArgs([$cache, $config, new \phpbb\file_downloader()])
94            ->onlyMethods(['get_suggested_updates'])
95            ->getMock();
96        $this->version_helper->method('get_suggested_updates')
97            ->willReturnCallback(function($force_update = false, $force_cache = false) use ($config)
98            {
99                if ($config['version'] === '100000')
100                {
101                    return [];
102                }
103                else if ($config['version'] === '0')
104                {
105                    return [
106                        [
107                            'current'        => '100000',
108                            'announcement'    => 'https://www.phpbb.com/downloads/',
109                            'eol'            => null,
110                            'security'        => false,
111                        ],
112                    ];
113                }
114                else
115                {
116                    throw new \phpbb\exception\runtime_exception('VERSIONCHECK_FAIL');
117                }
118            });
119
120        $container = new phpbb_mock_container_builder;
121        $container->set('version_helper', $this->version_helper);
122
123        $application = new Application();
124        $application->add(new check($user, $config, $container, $this->language));
125
126        $command = $application->find('update:check');
127        return new CommandTester($command);
128    }
129}