|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * UserFrosting Core Sprinkle (http://www.userfrosting.com) |
| 7 | + * |
| 8 | + * @link https://github.com/userfrosting/sprinkle-core |
| 9 | + * @copyright Copyright (c) 2013-2024 Alexander Weissman & Louis Charette |
| 10 | + * @license https://github.com/userfrosting/sprinkle-core/blob/master/LICENSE.md (MIT License) |
| 11 | + */ |
| 12 | + |
| 13 | +namespace UserFrosting\Sprinkle\Core\Tests\Integration\Controller; |
| 14 | + |
| 15 | +use Illuminate\Cache\Repository as Cache; |
| 16 | +use Mockery; |
| 17 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 18 | +use Psr\Http\Message\ResponseInterface as Response; |
| 19 | +use UserFrosting\I18n\DictionaryInterface; |
| 20 | +use UserFrosting\Sprinkle\Core\Tests\CoreTestCase; |
| 21 | + |
| 22 | +class DictionaryControllerTest extends CoreTestCase |
| 23 | +{ |
| 24 | + use MockeryPHPUnitIntegration; |
| 25 | + |
| 26 | + public function testGetDictionaryDataReturnsExpectedArray(): void |
| 27 | + { |
| 28 | + // Define mocks |
| 29 | + $dictionaryMock = Mockery::mock(DictionaryInterface::class); |
| 30 | + $cacheMock = Mockery::mock(Cache::class); |
| 31 | + |
| 32 | + $dictionaryMock->shouldReceive('getLocale->getIdentifier')->andReturn('en_US'); |
| 33 | + $dictionaryMock->shouldReceive('getLocale->getConfig')->andReturn(['config_key' => 'config_value']); |
| 34 | + $dictionaryMock->shouldReceive('getFlattenDictionary')->andReturn(['key' => 'value']); |
| 35 | + |
| 36 | + $cacheMock->shouldReceive('rememberForever')->andReturnUsing(function ($key, $callback) { |
| 37 | + return $callback(); |
| 38 | + }); |
| 39 | + |
| 40 | + // Set mocks in the CI |
| 41 | + $this->ci->set(DictionaryInterface::class, $dictionaryMock); |
| 42 | + $this->ci->set(Cache::class, $cacheMock); |
| 43 | + |
| 44 | + // Set expectations |
| 45 | + $expected = [ |
| 46 | + 'identifier' => 'en_US', |
| 47 | + 'config' => ['config_key' => 'config_value'], |
| 48 | + 'dictionary' => ['key' => 'value'], |
| 49 | + ]; |
| 50 | + |
| 51 | + // Create request with method and url and fetch response |
| 52 | + $request = $this->createJsonRequest('GET', '/api/dictionary'); |
| 53 | + $response = $this->handleRequest($request); |
| 54 | + |
| 55 | + // Assert response status & body |
| 56 | + $this->assertResponseStatus(200, $response); |
| 57 | + $this->assertJsonResponse($expected, $response); |
| 58 | + } |
| 59 | +} |
0 commit comments