Why Travis CI fails at testing my Laravel web route? - php

On my local machine all my phpunit tests passed successfully. But when the tests run on Travis CI, the one testing my Laravel web route fails.
Here is my route definition:
Route::get('/{any}', 'SinglePageController#index')->where('any', '.*')->name('landing');
My controller:
/**
* return the main view
* #return view
*/
public function index()
{
return view('landing');
}
(My Laravel view is named landing.blade.php)
And here is my test:
/**
* test return main web view
*
* #test
*/
public function testLandingViewIsReturned()
{
$response = $this->get(route('landing', ['any' => '']));
$response->assertSuccessful()
->assertViewIs('landing');
}
The Travis CI log:
1) Tests\Unit\RouteTest::testLandingViewIsReturned
Response status code [500] is not a successful status code.
Failed asserting that false is true.
Again, this test passes on my local dev env.
What's wrong with Travis?

Problem solved: my mix-manifest.json was missing during Travis build because I forgot to npm run dev in my .travis.yml configuration file.
The exception wasn't throw on my local env because I build assets every day.

Related

Vagrant & Symfony 3.3: Failed to remove directory

Trying to set up a Symfony 3.3 environment with Flex. Currently, when trying to require some packages like the following:
composer req annotations security orm template asset validator
Everything goes well, except on cache:clear I'm getting the following errors:
!! [Symfony\Component\Filesystem\Exception\IOException]
!! Failed to remove directory "/home/vagrant/Code/multi-user-gallery-blog/var/
!! cache/loca~/pools": rmdir(/home/vagrant/Code/multi-user-gallery-blog/var/ca
!! che/loca~/pools): Directory not empty.
I already tried to remove the folders manually, but those are generated automatically in the installation process, and then Symfony cannot remove them.
I'm running this on Vagrant Homestead.
Any idea on how can I solve this problem?
This is an issue with the way Vagrant/VirtualBox maps shared folders between the host and guest machines. In Symfony projects, the cache directory is typically placed within the project directory that gets synced from the host machine.
We can change the cache directory to use a location within the guest machine's filesystem to avoid these problems by adding an override method to the app/AppKernel.php class:
class AppKernel extends Kernel
{
...
public function getCacheDir()
{
if ($this->environment === 'local') {
return '/tmp/symfony/cache';
}
return parent::getCacheDir();
}
}
The example above demonstrates how we can set a custom cache directory for local development environments while retaining the default behavior for production. /tmp/symfony/cache is just an example. We can choose a location anywhere on the guest machine's filesystem that the application has permission to write to. Replace 'local' with the name of the environment the project uses for development.
See my comment to the accepted answer, here is the code:
public function getCacheDir()
{
if (in_array($this->environment, ['dev', 'test'])) {
return '/tmp/cache/' . $this->environment;
}
return parent::getCacheDir();
}
public function getLogDir()
{
if (in_array($this->environment, ['dev', 'test'])) {
return '/var/log/symfony/logs';
}
return parent::getLogDir();
}

ReflectionException: Laravel 5.4

Im getting this exception when running phpunit. I'm running the latest Laravel 5.4 with PHPUnit 5.7.23
ReflectionException: Class config does not exist
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:729
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:608
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:575
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:728
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:1172
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Support/ServiceProvider.php:57
/home/vagrant/Code/ProcessingHub-App/vendor/myvendor/core/src/app/Providers/CoreServiceProvider.php:50
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:574
/home/vagrant/Code/ProcessingHub-App/tests/CreatesApplication.php:18
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:89
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:66
/home/vagrant/Code/ProcessingHub-App/tests/TestCase.php:27
/home/vagrant/Code/ProcessingHub-App/tests/Unit/PassportTest.php:30
As you can see the exception comes from CoreServiceProvider this is one of my own ServiceProviders Nothing to weird happening there as you can see here
/**
* Register any package services.
*
* #return void
*/
public function register()
{
$this->registerEloquentFactoriesFrom(__DIR__.'/../../database/factories');
$this->mergeConfigFrom(
__DIR__.'/../../config/fields.php', 'fields'
);
}
contents of fields.php:
return [];
If i outcommend $this->mergeConfigFrom() it works like a charm, but the weird thing is i do this in multiple ServiceProviders and in these Classes it is not an issue.
I literally tried everything.
Running: composer dump-autoload
Running: php artisan optimize
Removing vendor and reinstalling everything
Debugging everywhere but no usefull info
Replacing test with basic testExample from laravel didn't work either
I already replaced my .env with an new simple .env didn't help.
My question is:
Does annyone know how i could fix this.
After I looked more into the \tests directory with a fresh Laravel installation i saw the thing i was staring at for like hours and hours.
The application was trying to register the ServiceProvider before the app was bootstraped. Sorry for the troubles (It was not my code).
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* #return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$app->register(\StepOrange\Core\Providers\CoreServiceProvider::class);
return $app;
}
}

Class 'Vendor\PackageName\ClassName' not found for newly created Laravel package

I've a problem running my newly created Laravel package which please check out https://github.com/Younesi/laravel-aparat
I can download it via Composer with no problem and it's auto-discovered via Laravel but when I try to use it, It gives me the following error of not finding class.
Class 'Younesi\LaravelAparat\Aparat' not found
My service Provider code is like:
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->app->bind('aparat', function ($app) {
return new Aparat;
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array('aparat');
}
Any help would be appreciated.
Looking at the package it's working fine, in composer.json of that package there is:
"autoload": {
"psr-4": {
"Younesi\\laravelAparat\\": "src"
}
},
Notice that laravel is not with capital letter here, so in your code you should import rather this way:
use Younesi\laravelAparat\Aparat;
instead of:
use Younesi\LaravelAparat\Aparat;
I also see that you are author of this package, so I would recommend using standard conversion (namespace starting with capital letter) instead of current namespace.
Looking further at package code, I also see that in service provider there is:
namespace Younesi\LaravelAparat;
namespace so it's nothing weird it won't work if you autoload it with lower-case letter and have namespace with upper-case letter
There were some cases with registration problem, cache issues, etc. Try one of these solutions:
register your provider (in main composer.json, then in config/app.php [provider & alias] ), then run composer dump-autoload
make sure you have initiated your package : go to the folder, then composer init
try php artisan config:cache or delete everything in bootstrap/cache/

Get list of routes of controller in Symfony?

I am able to get list of all routes for controller as mentioned on https://stackoverflow.com/a/15950365/3900206 by #Qoop (Thanks to him for sharing).
And, it shows different routes as configured when run in dev and prod environment. But, I only want to list routes that appears in prod environment in dev environment also.
How can I list only those routes which are configured for prod environment from any environment (dev or prod or test)?
Is it possible as we can list environment specific routes from console as:
php app/console router:debug --env=prod
Update (Improving question):
I am looking for a way to list out routes of production environment from controller in any environment (dev, test or prod)
I don't understand you have the command already, it can take --env option you can use it for dev/prod or test
php app/console router:debug --env=prod/test/dev
Edit2 :
You can create your own AppKernel with the environment that you choose because there is no way that i am aware off that you can set the environment on the current kernel which doesnt make sens.
instantiate a kernel prod for example and get the router service from its container.
for more info on instantiating new environment http://symfony.com/fr/doc/current/cookbook/configuration/environments.html
$kernel = new \AppKernel('prod', true);
$kernel->boot();
/** #var $router \Symfony\Component\Routing\Router */
$router = $kernel->getContainer()->get('router');
$router->setOption('debug', true);
/** #var $collection \Symfony\Component\Routing\RouteCollection */
$collection = $router->getRouteCollection();
$allRoutes = $collection->all();

Yii PHPUnit With Selenium

I'm having some trouble starting automated tests with Yii, PHPUnit and Selenium
I've set up Selenium and PHPUnit and when i run phpunit . I get this error:
Warning: include(PHPUnit_Extensions_Story_TestCase.php): failed to open stream: No such
file or directory in path_to/framework/YiiBase.php on line 427
Warning: include(): Failed opening 'PHPUnit_Extensions_Story_TestCase.php' for inclusion
(include_path='.:') in path_to/framework/YiiBase.php on line 427
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: The path to the driver executable
must be set by the phantomjs.binary.path capability/system property/PATH variable; for more
information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded
from http://phantomjs.org/download.html
I have Selenium RC running in background. I also have PHPunit folder inside tests folder, with Story and selenium extensions.
My code looks looks like this
define('TEST_BASE_URL','http://some_local_url/');
class WebTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
/**
* Sets up before each test method runs.
* This mainly sets the base URL for the test application.
*/
protected function setUp()
{
parent::setUp();
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowserUrl(TEST_BASE_URL);
}
}
class SiteTest extends WebTestCase
{
public function testIndex()
{
$this->open('');
$this->assertTextPresent('Welcome');
}
}
Turns out that Selenium server needs phantomjs (thank you captain obvious), to run correctly. I simply put the server .jar file in the same folder as phantomjs binary and the tests were running correctly afterwards.
It looks like you haven't configured the path to the framework in your test entry script (index-test.php by default) properly. You should also set your config file here. Without this your tests run with the same settings (databases, sessions) as your application.
This is the content of index-test.php for the demo blog : https://github.com/yiisoft/yii/blob/master/demos/blog/index-test.php
<?php
/**
* This is the bootstrap file for test application.
* This file should be removed when the application is deployed for production.
*/
// change the following paths if necessary
$yii=dirname(__FILE__).'/../../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/test.php';
// remove the following line when in production mode
// defined('YII_DEBUG') or define('YII_DEBUG',true);
require_once($yii);
Yii::createWebApplication($config)->run();

Categories