Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.44% covered (success)
98.44%
63 / 64
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_console_user_add_test
98.44% covered (success)
98.44%
63 / 64
75.00% covered (warning)
75.00%
3 / 4
8
0.00% covered (danger)
0.00%
0 / 1
 get_command_tester
97.14% covered (success)
97.14%
34 / 35
0.00% covered (danger)
0.00%
0 / 1
5
 test_add_no_dialog
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 test_add_dialog
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 test_add_no_dialog_invalid
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
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\user\add;
17use Symfony\Component\Console\Input\InputInterface;
18use Symfony\Component\Console\Output\OutputInterface;
19use Symfony\Component\Console\Question\Question;
20
21require_once __DIR__ . '/base.php';
22
23class phpbb_console_user_add_test extends phpbb_console_user_base
24{
25    public function get_command_tester($question_answers = [])
26    {
27        $application = new Application();
28        $application->add(new add(
29            $this->user,
30            $this->db,
31            $this->config,
32            $this->language,
33            $this->email,
34            $this->passwords_manager,
35            $this->phpbb_root_path,
36            $this->php_ext
37        ));
38
39        $command = $application->find('user:add');
40
41        if (!empty($question_answers))
42        {
43            $ask = function(InputInterface $input, OutputInterface $output, Question $question) use ($question_answers)
44            {
45                $text = $question->getQuestion();
46
47                // handle a question
48                foreach ($question_answers as $expected_question => $answer)
49                {
50                    if (strpos($text, $expected_question) !== false)
51                    {
52                        $response = $answer;
53                    }
54                }
55
56                if (!isset($response))
57                {
58                    throw new \RuntimeException('Was asked for input on an unhandled question: ' . $text);
59                }
60
61                $output->writeln(print_r($response, true));
62                return $response;
63            };
64            $helper = $this->getMockBuilder('\Symfony\Component\Console\Helper\QuestionHelper')
65                ->onlyMethods(['ask'])
66                ->disableOriginalConstructor()
67                ->getMock();
68            $helper->expects($this->any())
69                ->method('ask')
70                ->will($this->returnCallback($ask));
71            $this->question = $helper;
72            $command->getHelperSet()->set($helper, 'question');
73        }
74        else
75        {
76            $this->question = $command->getHelper('question');
77        }
78
79        return new CommandTester($command);
80    }
81
82    public function test_add_no_dialog()
83    {
84        $command_tester = $this->get_command_tester();
85
86        $this->assertEquals(2, $this->get_user_id('Admin'));
87
88        $command_tester->execute(array(
89            '--username'    => 'foo',
90            '--password'    => 'bar',
91            '--email'        => 'foo@test.com'
92        ));
93
94        $this->assertNotEquals(null, $this->get_user_id('foo'));
95        $this->assertStringContainsString('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay());
96    }
97
98    public function test_add_dialog()
99    {
100        $command_tester = $this->get_command_tester([
101            'USERNAME'        => 'bar',
102            'PASSWORD'        => 'password',
103            'EMAIL_ADDRESS'    => 'bar@test.com',
104        ]);
105
106        $this->assertEquals(2, $this->get_user_id('Admin'));
107
108        $command_tester->setInputs(['bar', 'password', 'password', 'bar@test.com']);
109
110        $command_tester->execute([]);
111
112        $this->assertNotEquals(null, $this->get_user_id('bar'));
113        $this->assertStringContainsString('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay());
114
115    }
116
117    public function test_add_no_dialog_invalid()
118    {
119        $command_tester = $this->get_command_tester();
120
121        $this->assertEquals(3, $this->get_user_id('Test'));
122
123        $command_tester->execute(array(
124            '--username'    => 'Test',
125            '--password'    => '1',
126            '--email'        => 'foo'
127        ));
128
129        $this->assertStringContainsString('USERNAME_TAKEN', $command_tester->getDisplay());
130        $this->assertStringContainsString('TOO_SHORT', $command_tester->getDisplay());
131        $this->assertStringContainsString('EMAIL_INVALID', $command_tester->getDisplay());
132    }
133}