Fixed the environment for unit tests to work.
First unit test is working. Also all database migrations get executed automatically when running the unit tests.develop
							parent
							
								
									53d3066230
								
							
						
					
					
						commit
						b8a8fbdc4b
					
				| @ -1,19 +1,26 @@ | ||||
| # This file defines all environment variables that the application needs. | ||||
| # DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE. | ||||
| # Use ".env.local" for local overrides during development. | ||||
| # Use real environment variables when deploying to production. | ||||
| # https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration | ||||
| 
 | ||||
| ###> symfony/framework-bundle ### | ||||
| APP_ENV=dev | ||||
| APP_SECRET=558a40bd107371e7f7f25b4adf2d0da9 | ||||
| APP_SECRET=879a6adeceeccbdc835a19f7e3aad7e8 | ||||
| #TRUSTED_PROXIES=127.0.0.1,127.0.0.2 | ||||
| #TRUSTED_HOSTS=localhost,example.com | ||||
| #TRUSTED_HOSTS='^localhost|example\.com$' | ||||
| ###< symfony/framework-bundle ### | ||||
| 
 | ||||
| ###> doctrine/doctrine-bundle ### | ||||
| # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url | ||||
| # For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" | ||||
| # Configure your db driver and server_version in config/packages/doctrine.yaml | ||||
| #DATABASE_URL=pgsql://db_user:db_password@127.0.0.1:5432/db_name | ||||
| DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db | ||||
| ###< doctrine/doctrine-bundle ### | ||||
| 
 | ||||
| ###> symfony/swiftmailer-bundle ### | ||||
| # For Gmail as a transport, use: "gmail://username:password@localhost" | ||||
| # For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" | ||||
| # Delivery is disabled by default via "null://localhost" | ||||
| MAILER_URL=null://localhost | ||||
| ###< symfony/swiftmailer-bundle ### | ||||
|  | ||||
| @ -0,0 +1,24 @@ | ||||
| # This file is a "template" of which env vars need to be defined for your application | ||||
| # Copy this file to .env file for development, create environment variables when deploying to production | ||||
| # https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration | ||||
| 
 | ||||
| ###> symfony/framework-bundle ### | ||||
| APP_ENV=dev | ||||
| APP_SECRET=9d74944d92d8155f1c870695def94464 | ||||
| #TRUSTED_PROXIES=127.0.0.1,127.0.0.2 | ||||
| #TRUSTED_HOSTS=localhost,example.com | ||||
| ###< symfony/framework-bundle ### | ||||
| 
 | ||||
| ###> doctrine/doctrine-bundle ### | ||||
| # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url | ||||
| # For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" | ||||
| # Configure your db driver and server_version in config/packages/doctrine.yaml | ||||
| DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name | ||||
| ###< doctrine/doctrine-bundle ### | ||||
| 
 | ||||
| ###> symfony/swiftmailer-bundle ### | ||||
| # For Gmail as a transport, use: "gmail://username:password@localhost" | ||||
| # For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" | ||||
| # Delivery is disabled by default via "null://localhost" | ||||
| MAILER_URL=null://localhost | ||||
| ###< symfony/swiftmailer-bundle ### | ||||
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								| @ -0,0 +1,4 @@ | ||||
| # See https://symfony.com/doc/current/email/dev_environment.html | ||||
| swiftmailer: | ||||
|     # send all emails to a specific address | ||||
|     #delivery_addresses: ['me@example.com'] | ||||
| @ -0,0 +1,3 @@ | ||||
| swiftmailer: | ||||
|     url: '%env(MAILER_URL)%' | ||||
|     spool: { type: 'memory' } | ||||
| @ -0,0 +1,2 @@ | ||||
| swiftmailer: | ||||
|     disable_delivery: true | ||||
| @ -0,0 +1,3 @@ | ||||
| framework: | ||||
|     validation: | ||||
|         email_validation_mode: html5 | ||||
| @ -0,0 +1,51 @@ | ||||
| <?php | ||||
| 
 | ||||
| use Symfony\Component\Dotenv\Dotenv; | ||||
| 
 | ||||
| require dirname(__DIR__).'/vendor/autoload.php'; | ||||
| 
 | ||||
| if (!array_key_exists('APP_ENV', $_SERVER)) { | ||||
|     $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] ?? null; | ||||
| } | ||||
| 
 | ||||
| if ('prod' !== $_SERVER['APP_ENV']) { | ||||
|     if (!class_exists(Dotenv::class)) { | ||||
|         throw new RuntimeException('The "APP_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.'); | ||||
|     } | ||||
| 
 | ||||
|     $path = dirname(__DIR__).'/.env'; | ||||
|     $dotenv = new Dotenv(); | ||||
| 
 | ||||
|     if (method_exists($dotenv, 'loadEnv')) { | ||||
|         $dotenv->loadEnv($path); | ||||
|     } else { | ||||
|         // fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added) | ||||
| 
 | ||||
|         if (file_exists($path) || !file_exists($p = "$path.dist")) { | ||||
|             $dotenv->load($path); | ||||
|         } else { | ||||
|             $dotenv->load($p); | ||||
|         } | ||||
| 
 | ||||
|         if (null === $env = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) { | ||||
|             $dotenv->populate(array('APP_ENV' => $env = 'dev')); | ||||
|         } | ||||
| 
 | ||||
|         if ('test' !== $env && file_exists($p = "$path.local")) { | ||||
|             $dotenv->load($p); | ||||
|             $env = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? $env; | ||||
|         } | ||||
| 
 | ||||
|         if (file_exists($p = "$path.$env")) { | ||||
|             $dotenv->load($p); | ||||
|         } | ||||
| 
 | ||||
|         if (file_exists($p = "$path.$env.local")) { | ||||
|             $dotenv->load($p); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?: $_ENV['APP_ENV'] ?: 'dev'; | ||||
| $_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV']; | ||||
| $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0'; | ||||
| @ -0,0 +1,35 @@ | ||||
| <?php declare(strict_types=1); | ||||
| 
 | ||||
| namespace DoctrineMigrations; | ||||
| 
 | ||||
| use Doctrine\DBAL\Schema\Schema; | ||||
| use Doctrine\Migrations\AbstractMigration; | ||||
| 
 | ||||
| /** | ||||
|  * Auto-generated Migration: Please modify to your needs! | ||||
|  */ | ||||
| final class Version20181118195745 extends AbstractMigration | ||||
| { | ||||
|     public function up(Schema $schema) : void | ||||
|     { | ||||
|         // this up() migration is auto-generated, please modify it to your needs | ||||
|         $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.'); | ||||
| 
 | ||||
|         $this->addSql('CREATE TABLE hero (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id INTEGER NOT NULL, name VARCHAR(32) NOT NULL)'); | ||||
|         $this->addSql('CREATE UNIQUE INDEX UNIQ_51CE6E865E237E06 ON hero (name)'); | ||||
|         $this->addSql('CREATE INDEX IDX_51CE6E86A76ED395 ON hero (user_id)'); | ||||
|         $this->addSql('CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username VARCHAR(32) NOT NULL, password VARCHAR(255) NOT NULL, api_token VARCHAR(255) DEFAULT NULL, roles CLOB NOT NULL --(DC2Type:json) | ||||
|         )'); | ||||
|         $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON user (username)'); | ||||
|         $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D6497BA2F5EB ON user (api_token)'); | ||||
|     } | ||||
| 
 | ||||
|     public function down(Schema $schema) : void | ||||
|     { | ||||
|         // this down() migration is auto-generated, please modify it to your needs | ||||
|         $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.'); | ||||
| 
 | ||||
|         $this->addSql('DROP TABLE hero'); | ||||
|         $this->addSql('DROP TABLE user'); | ||||
|     } | ||||
| } | ||||
| @ -1,6 +1,6 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Entity\Repository; | ||||
| namespace App\Repository; | ||||
| 
 | ||||
| use App\Entity\Hero; | ||||
| use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||||
| @ -1,6 +1,6 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Entity\Repository; | ||||
| namespace App\Repository; | ||||
| 
 | ||||
| use App\Entity\User; | ||||
| use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||||
| @ -0,0 +1,31 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Tests\Controller; | ||||
| 
 | ||||
| use App\DataFixtures\UserFixtures; | ||||
| 
 | ||||
| class UserControllerTest extends RestTestBase | ||||
| { | ||||
|     /** | ||||
|      * This test verifies that requesting | ||||
|      */ | ||||
|     public function testRetrieveUser() | ||||
|     { | ||||
|         $this->createRequestBuilder() | ||||
|             ->setMethod('GET') | ||||
|             ->setUri('/api/users') | ||||
|             ->setAcceptType('application/json') | ||||
|             ->addServerParameter('HTTP_X-AUTH-TOKEN', UserFixtures::ADMIN_USER_TOKEN) | ||||
|             ->request(); | ||||
| 
 | ||||
|         $response = $this->client->getResponse(); | ||||
| 
 | ||||
|         $this->assertTrue( | ||||
|             $response->headers->contains('Content-Type', 'application/json'), | ||||
|             'the "Content-Type" header is "' . $response->headers->get('Content-Type') . '"' // optional message shown on failure | ||||
|         ); | ||||
| 
 | ||||
|         $this->assertEquals(200, $response->getStatusCode(), 'Status code was ' . $response->getStatusCode() . ' but expected 200: ' . $response->getContent()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue
	
	 Hecht
						Hecht