Commit e6be9a747c5eb7dd78c99ec9f67de80604321604
Committed by
GitHub
Exists in
2.8
and in
7 other branches
Merge pull request #472 from portabilis/fix-http-tests
Melhora uso de HTTP Tests
Showing
11 changed files
with
126 additions
and
131 deletions
Show diff stats
.env.example
.travis.yml
app/Http/Controllers/LegacyController.php
... | ... | @@ -5,6 +5,7 @@ namespace App\Http\Controllers; |
5 | 5 | use Exception; |
6 | 6 | use Throwable; |
7 | 7 | use Illuminate\Contracts\Debug\ExceptionHandler; |
8 | +use Illuminate\Http\Request; | |
8 | 9 | use Illuminate\Http\Response; |
9 | 10 | use Symfony\Component\HttpKernel\Exception\HttpException; |
10 | 11 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
... | ... | @@ -12,6 +13,21 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
12 | 13 | class LegacyController extends Controller |
13 | 14 | { |
14 | 15 | /** |
16 | + * @var Request | |
17 | + */ | |
18 | + private $request; | |
19 | + | |
20 | + /** | |
21 | + * LegacyController constructor. | |
22 | + * | |
23 | + * @param Request $request | |
24 | + */ | |
25 | + public function __construct(Request $request) | |
26 | + { | |
27 | + $this->request = $request; | |
28 | + } | |
29 | + | |
30 | + /** | |
15 | 31 | * Return i-Educar legacy code path. |
16 | 32 | * |
17 | 33 | * @return string |
... | ... | @@ -172,12 +188,15 @@ class LegacyController extends Controller |
172 | 188 | * @param string $filename |
173 | 189 | * |
174 | 190 | * @return Response |
191 | + * | |
192 | + * @throws Exception | |
175 | 193 | */ |
176 | 194 | private function requireFileFromLegacy($filename) |
177 | 195 | { |
178 | 196 | ob_start(); |
179 | 197 | |
180 | 198 | $this->startLegacySession(); |
199 | + $this->overrideGlobals(); | |
181 | 200 | $this->configureErrorsAndExceptions(); |
182 | 201 | $this->loadLegacyBootstrapFile(); |
183 | 202 | $this->loadLegacyFile($filename); |
... | ... | @@ -198,7 +217,22 @@ class LegacyController extends Controller |
198 | 217 | */ |
199 | 218 | private function startLegacySession() |
200 | 219 | { |
201 | - session_start(); | |
220 | + @session_start(); | |
221 | + } | |
222 | + | |
223 | + /** | |
224 | + * Override PHP Globals if not exists. | |
225 | + * | |
226 | + * @return void | |
227 | + */ | |
228 | + private function overrideGlobals() | |
229 | + { | |
230 | + $_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_URI'] ?? $this->request->getRequestUri(); | |
231 | + | |
232 | + $_GET = empty($_GET) ? $this->request->query->all() : $_GET; | |
233 | + $_POST = empty($_POST) ? $this->request->request->all() : $_POST; | |
234 | + $_FILES = empty($_FILES) ? $this->request->files->all() : $_FILES; | |
235 | + $_COOKIE = empty($_COOKIE) ? $this->request->cookies->all() : $_COOKIE; | |
202 | 236 | } |
203 | 237 | |
204 | 238 | /** |
... | ... | @@ -207,6 +241,8 @@ class LegacyController extends Controller |
207 | 241 | * @param string $uri |
208 | 242 | * |
209 | 243 | * @return Response |
244 | + * | |
245 | + * @throws Exception | |
210 | 246 | */ |
211 | 247 | public function intranet($uri) |
212 | 248 | { |
... | ... | @@ -217,6 +253,8 @@ class LegacyController extends Controller |
217 | 253 | * Load module route file and generate a response. |
218 | 254 | * |
219 | 255 | * @return Response |
256 | + * | |
257 | + * @throws Exception | |
220 | 258 | */ |
221 | 259 | public function module() |
222 | 260 | { |
... | ... | @@ -229,6 +267,8 @@ class LegacyController extends Controller |
229 | 267 | * @param string $uri |
230 | 268 | * |
231 | 269 | * @return Response |
270 | + * | |
271 | + * @throws Exception | |
232 | 272 | */ |
233 | 273 | public function modules($uri) |
234 | 274 | { | ... | ... |
bootstrap/testing.php
ieducar/configuration/ieducar.ini.example
... | ... | @@ -112,8 +112,8 @@ app.recaptcha.options.lang = pt |
112 | 112 | app.recaptcha.options.theme = white |
113 | 113 | |
114 | 114 | ; Define chaves de acesso à API do i-Educar |
115 | -apis.access_key = | |
116 | -apis.secret_key = | |
115 | +apis.access_key = ${API_ACCESS_KEY} | |
116 | +apis.secret_key = ${API_SECRET_KEY} | |
117 | 117 | apis.educacao_token_header = |
118 | 118 | apis.educacao_token_key = |
119 | 119 | ... | ... |
ieducar/includes/bootstrap.php
1 | 1 | <?php |
2 | 2 | |
3 | +use Illuminate\Foundation\Application; | |
4 | +use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables; | |
5 | + | |
6 | +$app = require_once __DIR__ . '/../../bootstrap/app.php'; | |
7 | + | |
8 | +if ($app instanceof Application) { | |
9 | + (new LoadEnvironmentVariables())->bootstrap($app); | |
10 | +} | |
11 | + | |
3 | 12 | if (!defined('DS')) { |
4 | 13 | define('DS', DIRECTORY_SEPARATOR); |
5 | 14 | } | ... | ... |
ieducar/module/index.php
1 | 1 | <?php |
2 | 2 | |
3 | -use iEducar\Modules\ErrorTracking\TrackerFactory; | |
3 | +// Objeto de requisição | |
4 | +$request = new CoreExt_Controller_Request(); | |
4 | 5 | |
5 | -require_once __DIR__ . '/../vendor/autoload.php'; | |
6 | -require_once 'includes/bootstrap.php'; | |
7 | -require_once 'include/clsBanco.inc.php'; | |
8 | -require_once 'App/Model/IedFinder.php'; | |
9 | -require_once 'CoreExt/View/Helper/UrlHelper.php'; | |
10 | -require_once 'CoreExt/Controller/Request.php'; | |
11 | -require_once 'CoreExt/Controller/Front.php'; | |
12 | -require_once 'CoreExt/DataMapper.php'; | |
6 | +// Helper de URL. Auxilia para criar uma URL no formato http://www.example.org/module | |
7 | +$url = CoreExt_View_Helper_UrlHelper::getInstance(); | |
8 | +$url = $url->url($request->get('REQUEST_URI'), ['components' => CoreExt_View_Helper_UrlHelper::URL_HOST]); | |
13 | 9 | |
14 | -try { | |
15 | - // Objeto de requisição | |
16 | - $request = new CoreExt_Controller_Request(); | |
10 | +// Configura o baseurl da request | |
11 | +$request->setBaseurl(sprintf('%s/module', $url)); | |
17 | 12 | |
18 | - // Helper de URL. Auxilia para criar uma URL no formato http://www.example.org/module | |
19 | - $url = CoreExt_View_Helper_UrlHelper::getInstance(); | |
20 | - $url = $url->url($request->get('REQUEST_URI'), ['components' => CoreExt_View_Helper_UrlHelper::URL_HOST]); | |
13 | +// Configura o DataMapper para usar uma instância de clsBanco com fetch de resultados | |
14 | +// usando o tipo FETCH_ASSOC | |
15 | +CoreExt_DataMapper::setDefaultDbAdapter(new clsBanco(['fetchMode' => clsBanco::FETCH_ASSOC])); | |
21 | 16 | |
22 | - // Configura o baseurl da request | |
23 | - $request->setBaseurl(sprintf('%s/module', $url)); | |
17 | +// Inicia o Front Controller | |
18 | +$frontController = CoreExt_Controller_Front::getInstance(); | |
19 | +$frontController->setRequest($request); | |
24 | 20 | |
25 | - // Configura o DataMapper para usar uma instância de clsBanco com fetch de resultados | |
26 | - // usando o tipo FETCH_ASSOC | |
27 | - CoreExt_DataMapper::setDefaultDbAdapter(new clsBanco(['fetchMode' => clsBanco::FETCH_ASSOC])); | |
21 | +// Configura o caminho aonde os módulos estão instalados | |
22 | +$frontController->setOptions( | |
23 | + ['basepath' => PROJECT_ROOT . DS . 'modules'] | |
24 | +); | |
28 | 25 | |
29 | - // Inicia o Front Controller | |
30 | - $frontController = CoreExt_Controller_Front::getInstance(); | |
31 | - $frontController->setRequest($request); | |
26 | +$frontController->dispatch(); | |
32 | 27 | |
33 | - // Configura o caminho aonde os módulos estão instalados | |
34 | - $frontController->setOptions( | |
35 | - ['basepath' => PROJECT_ROOT . DS . 'modules'] | |
36 | - ); | |
37 | - | |
38 | - $frontController->dispatch(); | |
39 | - | |
40 | - // Resultado | |
41 | - print $frontController->getViewContents(); | |
42 | - | |
43 | -} catch (Exception $e) { | |
44 | - if (config('app.debug')) { | |
45 | - throw $e; | |
46 | - } | |
47 | - | |
48 | - $lastError = error_get_last(); | |
49 | - | |
50 | - @session_start(); | |
51 | - $_SESSION['last_error_message'] = $e->getMessage(); | |
52 | - $_SESSION['last_php_error_message'] = $lastError['message']; | |
53 | - $_SESSION['last_php_error_line'] = $lastError['line']; | |
54 | - $_SESSION['last_php_error_file'] = $lastError['file']; | |
55 | - @session_write_close(); | |
56 | - | |
57 | - if ($GLOBALS['coreExt']['Config']->modules->error->track) { | |
58 | - $tracker = TrackerFactory::getTracker($GLOBALS['coreExt']['Config']->modules->error->tracker_name); | |
59 | - $tracker->notify($e); | |
60 | - } | |
61 | - | |
62 | - error_log('Erro inesperado (pego em /module/index.php): ' . $e->getMessage()); | |
63 | - | |
64 | - die('<script>document.location.href = \'/module/Error/unexpected\';</script>'); | |
65 | -} | |
28 | +// Resultado | |
29 | +print $frontController->getViewContents(); | ... | ... |
phpunit.xml
tests/SuiteTestCase/ApiTestCase.php
... | ... | @@ -2,64 +2,54 @@ |
2 | 2 | |
3 | 3 | namespace Tests\SuiteTestCase; |
4 | 4 | |
5 | -use GuzzleHttp\Client; | |
5 | +use Illuminate\Support\Facades\DB; | |
6 | +use Tests\TestCase; | |
6 | 7 | |
7 | 8 | class ApiTestCase extends TestCase |
8 | 9 | { |
9 | 10 | /** |
10 | - * @var Client | |
11 | + * Return access key for API use. | |
12 | + * | |
13 | + * @return string | |
11 | 14 | */ |
12 | - private $http; | |
13 | - | |
14 | - public function setUp(): void | |
15 | - { | |
16 | - parent::setUp(); | |
17 | - $this->http = new Client(); | |
18 | - } | |
19 | - | |
20 | - public function doAuthenticatedRequest($resource, $params, $method = 'GET') | |
21 | - { | |
22 | - $params['access_key'] = $this->getApiKey(); | |
23 | - $params['secret_key'] = $this->getApiSecret(); | |
24 | - | |
25 | - return $this->doRequest($resource, $params, $method); | |
26 | - } | |
27 | - | |
28 | - public function doRequest($resource, $params, $method = 'GET') | |
29 | - { | |
30 | - if (!in_array($method, ['GET', 'POST'])) { | |
31 | - throw new \Exception('Método não implementado'); | |
32 | - } | |
33 | - | |
34 | - $params['resource'] = $resource; | |
35 | - $params['oper'] = 'get'; | |
36 | - | |
37 | - $response = $this->http->request( | |
38 | - 'GET', | |
39 | - $this->getApiUri(), | |
40 | - [\GuzzleHttp\RequestOptions::QUERY => $params] | |
41 | - ); | |
42 | - | |
43 | - return $response->getBody()->getContents(); | |
44 | - } | |
45 | - | |
46 | - private function getApiUri() | |
15 | + protected function getApiKey() | |
47 | 16 | { |
48 | - return getenv('API_URI'); | |
17 | + return env('API_ACCESS_KEY'); | |
49 | 18 | } |
50 | 19 | |
51 | - private function getApiKey() | |
20 | + /** | |
21 | + * Return secret key for API use. | |
22 | + * | |
23 | + * @return string | |
24 | + */ | |
25 | + protected function getApiSecret() | |
52 | 26 | { |
53 | - return getenv('API_ACCESS_KEY'); | |
27 | + return env('API_SECRET_KEY'); | |
54 | 28 | } |
55 | 29 | |
56 | - private function getApiSecret() | |
30 | + /** | |
31 | + * Return JSON filename. | |
32 | + * | |
33 | + * @param string $filename | |
34 | + * | |
35 | + * @return string | |
36 | + */ | |
37 | + public function getJsonFile($filename) | |
57 | 38 | { |
58 | - return getenv('API_SECRET_KEY'); | |
39 | + return __DIR__ . '/../Unit/assets/' . $filename; | |
59 | 40 | } |
60 | 41 | |
61 | - public function getJsonFile($fileName) | |
42 | + /** | |
43 | + * Load a SQL dump file. | |
44 | + * | |
45 | + * @param string $filename | |
46 | + * | |
47 | + * @return void | |
48 | + */ | |
49 | + public function loadDump($filename) | |
62 | 50 | { |
63 | - return __DIR__ . '/../Unit/assets/' . $fileName; | |
51 | + DB::unprepared('SET session_replication_role = replica;'); | |
52 | + DB::unprepared(file_get_contents(__DIR__ . '/../Unit/dumps/' . $filename)); | |
53 | + DB::unprepared('SET session_replication_role = DEFAULT;'); | |
64 | 54 | } |
65 | 55 | } | ... | ... |
tests/Unit/Api/RegraTest.php
1 | 1 | <?php |
2 | 2 | |
3 | -namespace Tests\unit\Api; | |
3 | +namespace Tests\Unit\Api; | |
4 | 4 | |
5 | -use PHPUnit\DbUnit\DataSet\DefaultDataSet; | |
6 | 5 | use Tests\SuiteTestCase\ApiTestCase; |
7 | 6 | |
8 | 7 | class RegraTest extends ApiTestCase |
9 | 8 | { |
10 | - public function getDataSet() | |
11 | - { | |
12 | - $this->setupDump('regraavaliacao.sql'); | |
13 | - return new DefaultDataSet(); | |
14 | - } | |
15 | - | |
16 | 9 | public function testRegression() |
17 | 10 | { |
18 | - $this->markTestSkipped(); | |
11 | + $query = [ | |
12 | + 'access_key' => $this->getApiKey(), | |
13 | + 'secret_key' => $this->getApiSecret(), | |
14 | + 'resource' => 'regras', | |
15 | + 'oper' => 'get', | |
16 | + 'instituicao_id' => 1, | |
17 | + 'ano' => '2018', | |
18 | + ]; | |
19 | + | |
20 | + $this->loadDump('regraavaliacao.sql'); | |
19 | 21 | |
20 | - $responseBody = $this->doAuthenticatedRequest('regras', ['instituicao_id' => 1, 'ano' => '2018']); | |
22 | + $response = $this->getJson('/module/Api/Regra?' . http_build_query($query)); | |
21 | 23 | |
22 | - $this->assertJsonStringEqualsJsonFile($this->getJsonFile('regra_json_valid.json'), $responseBody); | |
24 | + $this->assertJsonStringEqualsJsonFile( | |
25 | + $this->getJsonFile('regra_json_valid.json'), $response->getContent() | |
26 | + ); | |
23 | 27 | } |
24 | 28 | } | ... | ... |
tests/Unit/CoreExt/SessionTest.php
... | ... | @@ -111,9 +111,6 @@ class CoreExt_SessionTest extends TestCase |
111 | 111 | $this->assertEquals(1, count($this->_session)); |
112 | 112 | } |
113 | 113 | |
114 | - /** | |
115 | - * @backupGlobals enabled | |
116 | - */ | |
117 | 114 | public function testOverload() |
118 | 115 | { |
119 | 116 | $this->assertNull($this->_session->foo, '->foo is not null'); |
... | ... | @@ -150,4 +147,4 @@ class CoreExt_SessionTest extends TestCase |
150 | 147 | $this->assertEquals($this->_session->$key, $val, sprintf('$session->%s != %s', $key, $val)); |
151 | 148 | } |
152 | 149 | } |
153 | -} | |
154 | 150 | \ No newline at end of file |
151 | +} | ... | ... |