Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: php spark migrate on testing environment doesn't work #3309

Closed
lefuentes opened this issue Jul 14, 2020 · 6 comments
Closed

Bug: php spark migrate on testing environment doesn't work #3309

lefuentes opened this issue Jul 14, 2020 · 6 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@lefuentes
Copy link
Contributor

Describe the bug
When I change environment variable CI_ENVIRONMENT to testing, the command php spark migrate doesn't work. I want to migrate all migrations files on app/Database/Migrations to test my app.
It returns that there is an error on routes.

image

CodeIgniter 4 version
CodeIgniter CLI Tool - Version 4.0.3

Context

  • OS: Archlinux
  • PHP version : 7.4.6
@lefuentes lefuentes added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 14, 2020
@paulbalandan
Copy link
Member

This is confirmed. Tested on codeigniter4/framework v4.0.3 and latest codeigniter4/codeigniter4. Even running php spark yields Error 404.

@paulbalandan
Copy link
Member

Did some digging and found the offending method in CodeIgniter\CodeIgniter:

        protected function getRequestObject()
	{
		if ($this->request instanceof Request)
		{
			return;
		}

		if (is_cli() && ENVIRONMENT !== 'testing')
		{
			// @codeCoverageIgnoreStart
			$this->request = Services::clirequest($this->config);
			// @codeCoverageIgnoreEnd
		}
		else
		{
			$this->request = Services::request($this->config);
			// guess at protocol if needed
			$this->request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1');
		}
	}

Currently, on CLI if ENVIRONMENT is not testing the HTTPVerb used by RouteCollection is cli. Otherwise, it will be set to get. Since the migration routes are anchored on the cli verb, these are not found when on testing. I don't know the logic behind if (is_cli() && ENVIRONMENT !== 'testing') so I cannot make a PR to fix this if this is really a bug. I have found other methods relying on ENVIRONMENT !== 'testing'.

Maybe @lonnieezell , @MGatner or @michalsn can shed some light on this.

@MGatner
Copy link
Member

MGatner commented Jul 15, 2020

I'm not entirely sure but my guess is that ENVIRONMENT set to testing is intended for Unit Tests and Continuous Integration, not running the application manually to test it. If you need an environment to "try out" I would recommend setting development as that is pre-configured for such use; alternatively you could define your own, "staging" or something.

@lefuentes
Copy link
Contributor Author

@MGatner I understand your point. But I need migrate into testing database to run unit test and integration test. How can I do that without php spark migrate?
I tried it with php spark migrate -g tests and ENVIRONMENT = development but didn't work.

@MGatner
Copy link
Member

MGatner commented Jul 15, 2020

Typically you would do this programmatically, either by specifying the migration namespaces on the Database Test Case (https://codeigniter4.github.io/CodeIgniter4/testing/database.html) or by using MigrationRunner directly in your fixture.

I recommend checking out the CodeIgniter Playground if you are new to testing in the framework. It has some example test cases, tests, and docs (https://github.com/codeigniter4projects/playground/blob/develop/docs/testing.md) that should be helpful.

@lonnieezell
Copy link
Member

Yes, biggest piece of advice here is don't use the environment "testing". That's the mode set when running phpunit and has special conditions built into the framework at various places to assist with that. For example, when running automated tests it's always running from the CLI and there are a number of times when you want to simulate an HTTP request so that check was placed there.

I believe @MGatner gave the appropriate steps here so am closing it. If you still run into problems under environments other than testing feel free to re-open with additional details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

4 participants