|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * NOTICE OF LICENSE |
| 4 | + * |
| 5 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 6 | + * that is bundled with this package in the file LICENSE.txt. |
| 7 | + * It is also available through the world-wide-web at this URL: |
| 8 | + * https://opensource.org/licenses/OSL-3.0 |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to contact@h-hennes.fr so we can send you a copy immediately. |
| 12 | + * |
| 13 | + * @author Hennes Hervé <contact@h-hennes.fr> |
| 14 | + * @copyright since 2016 Hennes Hervé |
| 15 | + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 16 | + * https://github.com/nenes25/prestashop_console |
| 17 | + * https://www.h-hennes.fr/blog/ |
| 18 | + */ |
| 19 | + |
| 20 | +use PrestashopConsole\Command\Admin\User\CreateCommand; |
| 21 | +use Symfony\Component\Console\Tester\CommandTester; |
| 22 | +use Symfony\Component\Console\Helper\HelperSet; |
| 23 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +class CreateAdminUserCommandTest extends TestCase |
| 27 | +{ |
| 28 | + /** @var CommandTester */ |
| 29 | + private $commandTester; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $command = new CreateCommand(); |
| 34 | + $command->setHelperSet(new HelperSet([new QuestionHelper()])); |
| 35 | + $this->commandTester = new CommandTester( |
| 36 | + $command |
| 37 | + ); |
| 38 | + parent::setUp(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param array $datas |
| 43 | + * @dataProvider getCases |
| 44 | + */ |
| 45 | + public function testExecute($datas): void |
| 46 | + { |
| 47 | + if (isset($datas['expect_exception'])) { |
| 48 | + $this->expectException($datas['expect_exception']); |
| 49 | + $this->expectExceptionMessage($datas['response_message']); |
| 50 | + $this->commandTester->execute($datas['params']); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Check command status code |
| 55 | + $this->assertEquals( |
| 56 | + $datas['response_code'], |
| 57 | + $this->commandTester->execute( |
| 58 | + $datas['params'] |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + // Check command message contains expected output |
| 63 | + $display = trim($this->commandTester->getDisplay()); |
| 64 | + $this->assertStringContainsString( |
| 65 | + $datas['response_message'], |
| 66 | + $display |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function getCases(): Generator |
| 71 | + { |
| 72 | + $emailOkRandomString = sprintf('unittestadmin-%s@yopmail.com', time() * mt_rand(1, 1000)); |
| 73 | + |
| 74 | + yield 'Case ok - Valid admin creation' => [ |
| 75 | + [ |
| 76 | + 'params' => [ |
| 77 | + '--email' => $emailOkRandomString, |
| 78 | + '--password' => 'prestashop123', |
| 79 | + '--firstname' => 'Admin', |
| 80 | + '--lastname' => 'Test', |
| 81 | + ], |
| 82 | + 'response_message' => 'Admin user created successfully!', |
| 83 | + 'response_code' => 0, |
| 84 | + ] |
| 85 | + ]; |
| 86 | + |
| 87 | + yield 'Case error - Invalid email' => [ |
| 88 | + [ |
| 89 | + 'params' => [ |
| 90 | + '--email' => 'invalid-email', |
| 91 | + '--password' => 'prestashop123', |
| 92 | + '--firstname' => 'Admin', |
| 93 | + '--lastname' => 'Test', |
| 94 | + ], |
| 95 | + 'response_message' => 'The email is empty or not valid', |
| 96 | + 'expect_exception' => RuntimeException::class, |
| 97 | + ] |
| 98 | + ]; |
| 99 | + |
| 100 | + yield 'Case error - Empty firstname validated at final step' => [ |
| 101 | + [ |
| 102 | + 'params' => [ |
| 103 | + '--email' => sprintf('admin-%s@yopmail.com', time() * mt_rand(1, 1000)), |
| 104 | + '--password' => 'prestashop123', |
| 105 | + '--firstname' => '', |
| 106 | + '--lastname' => 'Test', |
| 107 | + ], |
| 108 | + 'response_message' => 'Invalid firstname format', |
| 109 | + 'response_code' => 1, |
| 110 | + ] |
| 111 | + ]; |
| 112 | + |
| 113 | + yield 'Case error - Empty lastname validated at final step' => [ |
| 114 | + [ |
| 115 | + 'params' => [ |
| 116 | + '--email' => sprintf('admin-%s@yopmail.com', time() * mt_rand(1, 1000)), |
| 117 | + '--password' => 'prestashop123', |
| 118 | + '--firstname' => 'Admin', |
| 119 | + '--lastname' => '', |
| 120 | + ], |
| 121 | + 'response_message' => 'Invalid lastname format', |
| 122 | + 'response_code' => 1, |
| 123 | + ] |
| 124 | + ]; |
| 125 | + } |
| 126 | +} |
0 commit comments