PasswordHasherFactoryTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Auth;
  17. use Cake\Auth\PasswordHasherFactory;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for PasswordHasherFactory
  21. */
  22. class PasswordHasherFactoryTest extends TestCase
  23. {
  24. /**
  25. * test passwordhasher instance building
  26. */
  27. public function testBuild(): void
  28. {
  29. $hasher = PasswordHasherFactory::build('Default');
  30. $this->assertInstanceof('Cake\Auth\DefaultPasswordHasher', $hasher);
  31. $hasher = PasswordHasherFactory::build([
  32. 'className' => 'Default',
  33. 'hashOptions' => ['foo' => 'bar'],
  34. ]);
  35. $this->assertInstanceof('Cake\Auth\DefaultPasswordHasher', $hasher);
  36. $this->assertEquals(['foo' => 'bar'], $hasher->getConfig('hashOptions'));
  37. $this->loadPlugins(['TestPlugin']);
  38. $hasher = PasswordHasherFactory::build('TestPlugin.Legacy');
  39. $this->assertInstanceof('TestPlugin\Auth\LegacyPasswordHasher', $hasher);
  40. $this->clearPlugins();
  41. }
  42. /**
  43. * test build() throws exception for nonexistent hasher
  44. */
  45. public function testBuildException(): void
  46. {
  47. $this->expectException(\RuntimeException::class);
  48. $this->expectExceptionMessage('Password hasher class "FooBar" was not found.');
  49. $hasher = PasswordHasherFactory::build('FooBar');
  50. }
  51. }