Acces to config variable when testing a package in laravel - laravel

I'm writing a test in Laravel and I want to unittest this piece of code:
if (file_exists(\Config::get('maintenance.dir.api'))) {
throw new ServiceUnavailableException('We are down for maintenance');
}
I'm using Illuminate\Foundation\Testing\TestCase and I can't access to the config variables from the main app in the test. In my package, I don't have any config. Can I mock the config folder or something like that?
Thank you!

You can 'mock' config variables by simply setting them:
config(['maintenance.dir.api' => 'mock value'])

Related

The following error originated from your test code, not from Cypress - process is not defined

I am getting this error when try to run the test in Cypress. Can someone help me how to resolve this, please?
This is my index.js
// Import commands.js using ES2015 syntax:
import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')
Cypress.on('uncaught:exception', (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false
})
The line where it errors
const env = process.env
is only valid in NodeJS, where process is a global object supplied by the Node runtime.
Cypress has both a Node process to which you can add plugins via the file cypress/plugins/index.js and a Browser process where you can add code to cypress/support/index.js.
The error comes from a package called ci-info, so it looks like you have imported it or something that uses it into cypress/support/index.js or cypress/support/commands.js, or directly into a test.
Please check all your imports.
I had to remove import "cypress" from my test. That fixed it. 9.7.0
This happened for me when I imported cypress within my test, removing that fixed the issue
This was the issue in my case , I removed it from my test's file. Now, Its working
const cypress = require("cypress");

In test runner cannot see tests in newly created folder - cypress

I created new folder 'apitests' in cypress project and created a JavaScript test file in it. It does not show up in the test runner.
I have used the default configuration in cypress.json as specified in Cypress documentation
"testFiles": "**/*.*",
I expected my new folder 'apitests' and JavaScript test file to show in the test runner. Here is the end result.
Here is the file structure.
You've put your apitests folder directly in cypress/, while Cypress by default looks in cypress/integration/ folder.
You can change that by using integrationFolder config option, but I'd personally just keep the spec files in cypress/integration as is the default.
I had to add this to cypress.config.js:
module.exports = defineConfig({
e2e: {
specPattern: [
'cypress/e2e/*.js',
'cypress/e2e/**/*.js'
]
}
});
I had it initially, but removed it, to hope that Cypress would automatically find all my tests. But that resulted in new tests not being added to the test-runner.
My Cypress-version: 11.x.x (and I updated to 12.0.2 as a debugging attempt).
The solution was found in the Cypress-documentation for config.
You've put your apitests folder directly in cypress/, while Cypress by default looks in cypress/integration/ folder.
You will get an idea about it

I am using composer embeded to test my code, when I add request function to interact other server.ReferenceError: require is not defined

I was writing composer test.In my origin codes, the logic.js include request.post which interact with other server. I add
var request = require('request') in the beginning to avoid error which is
"error 'request' is not defined".
in this way, I can translate the package into a .bna file, and work well.
But when I try to write some unit test with 'embeded', the error came up with
ReferenceError: require is not defined.
I add the 'require' package in my package.json file.
this is because that 'eslint' is complaining it doesn't know what to do with request when you run npm test etc.
We shoud add the comment like
/* global getAssetRegistry getFactory emit request */
follow the example:
https://github.com/hyperledger/composer-sample-networks/blob/master/packages/basic-sample-network/lib/sample.js#L15

How to make config file in Golang elegantly?

I'm a newbie with Golang.
I want to write a program to manage my Redis instances so that I can create a Redis connection with specific config file. But I don't know how to create the config file for Redis instances elegantly.
I found "text/template" before, is that a good idea?
It depends on the file format you want to support for those configs.
One library able to read most of those format (from a simple ini file to a JSON one) would be spf13/viper:
Viper is a complete configuration solution for go applications. It has been designed to work within an application to handle all types of configuration. It supports
setting defaults
reading from yaml, toml and json config files
reading from environment variables
reading from remote config systems (Etcd or Consul)
reading from command line flags
setting explicit values
Redis configuration files have a simple text format. You can generate a configuration file using the fmt package:
fmt.Fprintf(w, "pidfile %s\n", pidFile)
fmt.Fprintf(w, "port %d\n", port)
where w is io.Writer for the output.
The text/template package is also a viable option. Given the template
pidfile {{.PidFile}}
port {{.Port}}
you can execute it with
t.Execute(w, map[string]interface{}{
"PidFile": pidFile,
"Port": port,
})
If you want to make a config file for development, testing, staging and production, I would recommend to use the // +build possibility offered by Go.
Set up your Go program
You create 4 files in a config packages as followed :
src/program/config
|
|--config_dev.go
|--config_test.go
|--config_staging.go
|--config_prod.go
In the config files
Then in each file, you define the tag used to use that file during the go build (or run, ...) process.
It means for instance in config_dev.go :
// +build dev
package config
// Development ready configuration const based on the build tags.
const (
MYSETTINGS = "dev blablabla"
ISREADY = false
)
In the config_test.go, that would look like :
// +build test
package config
// Development ready configuration const based on the build tags.
const (
MYSETTINGS = "test blablabla"
ISREADY = true
)
Note the // +build dev and // +build test.
That are the tags you are going to use during build to tell which config file you want to use.
In any other Go file, you just have to call config.ISREADY without importing anything else that "config" in your file.
Build
Then to build your app, you just have to run :
go build -tags dev to build with the development config
or
go build -tags test to build with the testing config.
As redis config file has very simple structure I'd suggest you to look at encoding/csv package with Reader.Comma delimiter set just to blank space. It allow you to both read, parse and write configuration easily. Seems to me "slaveof {{.Host}} {{.Port}}" as template looks not very handy. But it's sure correct approach. Just matter of taste.
I would suggest to use some configuration library. I like Viper for is completeness.

Changing cache_dir and log_dir with Symfony2

Because of deployment constraints, I would like to have the log and cache directories used by my Symfony2 application somewhere under /var/... in my file system. For this reason, I am looking for a way to configure Symfony and to override the default location for these two directories.
I have seen the kernel.cache_dir and kernel.log_dir and read the class Kernel.php. From what I have seen, I don't think that it is possible to change the dir locations by configuration and I would have to patch the Kernel.php class.
Is that true, or is there a way to achieve what I want without modifying the framework code?
Add the following methods to app/AppKernel.php (AppKernel extends Kernel) making them return your preferred paths:
public function getCacheDir()
{
return $this->rootDir . '/my_cache/' . $this->environment;
}
public function getLogDir()
{
return $this->rootDir . '/my_logs';
}
I was happy to find your post, but I was a little bit confused of the unhelping answers.
I got the same problem and found out that the logs are depending on the config parameter
kernel.logs_dir.
So I just added it to my config.yml parameters:
kernel.logs_dir: /var/log/symfonyLogs
I hope it will helpfull for you even, if its a late answer.
i think the easiest way is to link the folder to another place. We have made this on the prod server but when you develop local perhaps on windows its a bit complicated to set the symlinks.
ln -s /var/cache/ /var/www/project/app/cache
something like this.
I would like to offer an alternative and that is to set environment variables to change these directories. This way it's easier to set depending on the stage. (testing, production or development)
export SYMFONY__KERNEL__CACHE_DIR "/your/directory/cache"
export SYMFONY__KERNEL__LOGS_DIR "/your/directory/logs"
Environment variables can also be set in the virtual host with SetEnv.
When reading kernel parameters symfony will look for all the $_SERVER variables that start with SYMFONY__, strip the first part and convert all the double underscores into a .
Source code
See line 568 to 608
In symfony you can override the cache (and logs) directory by extending the method in AppKernel.
// app/appKernel.php
class AppKernel extends Kernel
{
// ...
public function getCacheDir()
{
return $this->rootDir.'/'.$this->environment.'/cache';
}
}
Check out http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html#override-cache-dir
I used the configuration solution from Dragnic but I put the paths in the parameters.yml file because this file is ignored by git. in other words, it's not synchronized from my PC to the git repository so there is no impact in the prod environment.
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
[...]
kernel.cache_dir: "T:/project/cache"
kernel.logs_dir: "T:/project/logs"
Configuration: Windows7, WAMP 2.4 and Symfony 2.3.20.
But you have to know that:
Overwriting the kernel.cache_dir parameter from your config file is a very bad idea, and not a supported way to change the cache folder in Symfony.
It breaks things because you would now have different cache folders for the kernel Kernel::getCacheDir() and for the parameter.
Source: https://github.com/symfony/AsseticBundle/issues/370
So you should use it only in dev environment and if you don't want to change the content of the app/AppKernel.php file, otherwise see the other answers.
No accepted answer, and a really old question, but IĀ found it with google, so I post here a more recent way to change the cache directory, and the logs directory, (source here)
remember, short syntax for arrays require php 5.4
you can select the env to modify, and manage different cache and logs directories if you want
public function getCacheDir()
{
if (in_array($this->environment, ['prod', 'test'])) {
return '/tmp/cache/' . $this->environment;
}
return parent::getCacheDir();
}
public function getLogDir()
{
if (in_array($this->environment, ['prod', 'test'])) {
return '/var/log/symfony/logs';
}
return parent::getLogDir();
}

Resources