Unable to mock Cache::put() facade in Laravel - laravel

I'm trying to mock the Cache::put() facade. But it gives me an error. I have tried different ways but couldn't figure it out.
public function testGetAllFromDatabase()
{
$industry = new Industry();
Cache::shouldReceive('has')
->once()
->with('industries.all')
->andReturn(false);
Cache::shouldReceive('put')
->with('industries.all', '', 0)
->andReturn(true);
$this->industryMock
->shouldReceive('all')
->once()
->andReturn(array_reverse($this->industries));
$this->app->instance(Industry::class, $this->industryMock);
$industryRepository = new IndustryRepository();
$all = $industryRepository->all();
dd($all);
$this->assertContains( $this->industries[2], $all);
}
But when I execute it the following error is occurring.
$ vendor/bin/phpunit
PHPUnit 7.2.7 by Sebastian Bergmann and contributors.
...E 4 / 4 (100%)
Time: 3.76 seconds, Memory: 12.00MB
There was 1 error:
1) Tests\Unit\RepositoriesTests\IndustryRepositoryTest::testGetAllFromDatabase
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_1_Illuminate_Cache_CacheManager::put('industries.all', object(Illuminate\Database\Eloquent\Collection), '1440'). Either the method was unexpected or its arguments matched no expected argument list for this method
Objects: ( array (
'Illuminate\\Database\\Eloquent\\Collection' =>
array (
'class' => 'Illuminate\\Database\\Eloquent\\Collection',
'properties' =>
array (
),
),
))
F:\development\consulting.local\src\vendor\mockery\mockery\library\Mockery\ExpectationDirector.php:92
F:\development\consulting.local\src\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:223
F:\development\consulting.local\src\app\Repositories\IndustryRepository.php:30
F:\development\consulting.local\src\tests\Unit\RepositoriesTests\IndustryRepositoryTest.php:81
I have tried many ways but couldn't get it to fix. Thank you.

Since it may help others, Laravel's facade includes helper functions that allow swapping then with a Mockery test double
This means that when you use a shouldReceive you can chain it with any Mockery expectation for example in this case if you don't care about some parameters you can use:
Cache::shouldReceive('put')
->with('industries.all', \Mockery::any(), \Mockery::any())
->andReturn(true);

In case it helps others, it's good to point out that you may not want to mock Cache, but instead actually use the real Cache.
That's because it's a Cache for testing only:
When running tests via vendor/bin/phpunit, Laravel [....] automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.
https://laravel.com/docs/8.x/testing#environment
Note that unlike the OP's test, you may need to follow Laravel's guidance about your test class extending their TestCase to get the behavior, e.g.
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase

Related

Laravel Dusk Test passes when ran individually but fails when ran as part of a suite

I am trying two run two tests on a single Test Class in Laravel dusk. It passes when ran individually but fails when ran as part of a suite.
Here, my first test passes, but second test is failing.
Exception looks like:
There was 1 error:
1) Tests\Browser\Front\JobApplyTest::jobseeker_can_apply_to_a_job
Illuminate\Contracts\Container\BindingResolutionException: Target class [env] does not exist.
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:879
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:851
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:836
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:1423
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:571
/home/ellite/code/labs/jagirhouse/app/Core/Providers/TelescopeServiceProvider.php:26
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:279
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:61
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:323
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:281
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:330
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:475
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Watchers/QueryWatcher.php:48
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:404
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:249
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:887
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:728
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:683
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:502
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:109
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:364
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:227
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
/home/ellite/code/labs/jagirhouse/database/migrations/2014_10_12_000000_create_users_table.php:46
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:394
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:403
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:202
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:167
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:112
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:85
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:585
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:68
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:28
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:55
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:1005
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:299
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:171
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:186
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:263
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:260
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:413
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:66
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:19
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:126
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/home/ellite/code/labs/jagirhouse/vendor/laravel/dusk/src/TestCase.php:23
Caused by
ReflectionException: Class "env" does not exist
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:877
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:851
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:836
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:1423
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:571
/home/ellite/code/labs/jagirhouse/app/Core/Providers/TelescopeServiceProvider.php:26
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:279
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:61
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:323
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:281
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:330
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:475
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Watchers/QueryWatcher.php:48
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:404
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:249
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:887
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:728
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:683
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:502
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:109
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:364
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:227
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
/home/ellite/code/labs/jagirhouse/database/migrations/2014_10_12_000000_create_users_table.php:46
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:394
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:403
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:202
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:167
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:112
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:85
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:585
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:68
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:28
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:55
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:1005
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:299
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:171
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:186
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:263
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:260
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:413
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:66
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:19
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:126
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/home/ellite/code/labs/jagirhouse/vendor/laravel/dusk/src/TestCase.php:23
Automated tests are supposed to be side-effect free. That means well-designed tests should be possible to execute in any order and still receive the same results per test.
So when you have a test which passes or fails depending on other tests, then your tests are not properly isolated.
Find out what pre-conditions a test assumes (like specific data being in a specific database table or specific global variables hold a specific value) and ensure that your pre-conditions are fulfilled before the actual test is performed.

Mews\Purifier\Purifier::__construct() must be an instance of Illuminate\Filesystem\Filesystem

I installed Mews-Purifier on laravel 5.1
I used Froala Editor.
https://stackoverflow.com/
<script>asdfasdfasdf</script>
<iframe>qwerqwerqwer</iframe>
I expect result.
<p>https://stackoverflow.com/</p>
<p>qwerqwerqwer</p>
but, I am not see it.
when save it,
$data['detail'] = app('purifier')->clean($data['detail']);
if use Purifier::clean($data['detail']), It send error message me.
Non-static method Mews\Purifier\Purifier::clean() should not be called statically
so, It changed.
$data['detail'] = (new \Mews\Purifier\Purifier)->clean($data['detail']);
send error message too.
Type error: Too few arguments to function Mews\Purifier\Purifier::__construct(),
so I do changed.
(new Mews\Purifier\Purifier($data['detail'], ['AutoFormat.DisplayLinkURI' => true] ))->clean($data['detail');
Type error: Argument 1 passed to Mews\Purifier\Purifier::__construct() must be an instance of Illuminate\Filesystem\Filesystem, string given, called in
what Filesystem? what file??
Let me know How should I use it?
If I wasn't good at installing, what would it be?
Oh the installation did the following.
terminal
composer require mews/purifier
/config/app.php
'providers' => [
// ...
Mews\Purifier\PurifierServiceProvider::class,
],
'aliases' => [
// ...
'Purifier' => Mews\Purifier\Facades\Purifier::class,
]
terminal
php artisan vendor:publish
check created file. /config/purifier.php
Thanks for watching
Please let me know if you know.

Clojure: using ragtime with sqlite3

I'd like to use ragtime to manage migrations on an SQLite database. Following the instructions here, i've tried the following in the REPL:
(require '[ragtime.jdbc :as jdbc]
'[ragtime.repl :as repl])
(def config
{:datastore (jdbc/sql-database {:connection-uri "jdbc:sqlite:resources/db.sqlite3"})
:migrations (jdbc/load-resources "migrations")})
(repl/migrate config)
All I get is the following error:
ClassCastException clojure.lang.PersistentVector cannot be cast to clojure.lang.Named clojure.core/name (core.clj:1546)
The database file exists in resources/db.sqlite3. I've tried tracing the exception (i can add the stack trace if needed), but it seems to happen deep in clojure.java.jdbc.
As I'm new to the JVM and JDBC, I'm also not sure whether I'm specifying the :connection-uri correctly; I've tried several variants but can't seem to make it worK.
Any help would be much appreciated !
EDIT: stack trace:
java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to clojure.lang.Named
at clojure.core$name.invokeStatic (core.clj:1546)
clojure.core$name.invoke (core.clj:1540)
clojure.java.jdbc$as_sql_name.invokeStatic (jdbc.clj:67)
clojure.java.jdbc$as_sql_name.invoke (jdbc.clj:56)
clojure.java.jdbc$create_table_ddl$spec_to_string__2511.invoke (jdbc.clj:1052)
clojure.core$map$fn__4785.invoke (core.clj:2646)
clojure.lang.LazySeq.sval (LazySeq.java:40)
clojure.lang.LazySeq.seq (LazySeq.java:49)
clojure.lang.LazySeq.first (LazySeq.java:71)
clojure.lang.RT.first (RT.java:667)
clojure.core$first__4339.invokeStatic (core.clj:55)
clojure.string$join.invokeStatic (string.clj:180)
clojure.string$join.invoke (string.clj:180)
clojure.java.jdbc$create_table_ddl.invokeStatic (jdbc.clj:1056)
clojure.java.jdbc$create_table_ddl.doInvoke (jdbc.clj:1041)
clojure.lang.RestFn.invoke (RestFn.java:423)
ragtime.jdbc$migrations_table_ddl.invokeStatic (jdbc.clj:16)
ragtime.jdbc$migrations_table_ddl.invoke (jdbc.clj:15)
ragtime.jdbc$ensure_migrations_table_exists.invokeStatic (jdbc.clj:22)
ragtime.jdbc$ensure_migrations_table_exists.invoke (jdbc.clj:20)
ragtime.jdbc.SqlDatabase.applied_migration_ids (jdbc.clj:42)
ragtime.core$migrate_all.invokeStatic (core.clj:43)
ragtime.core$migrate_all.invoke (core.clj:32)
ragtime.repl$migrate.invokeStatic (repl.clj:49)
ragtime.repl$migrate.invoke (repl.clj:34)
thulium.core$eval8407.invokeStatic (form-init2686611279014890656.clj:1)
(the rest is REPL and compiler calls)
And the two migration files, resources/migrations/001-initial.up.sql:
CREATE TABLE tests (
id INTEGER PRIMARY KEY AUTOINCREMENT
);
and resources/migrations/001-initial.down.sql:
DROP TABLE tests;
Give it a go with these versions:
[org.clojure/java.jdbc "0.6.1"]
[org.xerial/sqlite-jdbc "3.8.7"]

Laravel 5.0, env() returns null during concurrent requests

The problem is that when I try to get a config variable using env('setting') or \Config::get('setting'), sometimes it returns null.
For the testing reason I created a simple route:
Route::get('/test', function () {
$env = env('SETTING');
if (!$env) {
\Log::warning('test', [$env]);
}
});
Then I used apache benchmark. And the results were like this:
Calling only one request at a time (ab -n 100 -c 1 http://localhost/test) there were no problem, no records in the log file
Calling with 10 concurrent requests (ab -n 100 -c 10 http://localhost/test) I got about 20 lines like this: [2015-06-22 14:19:48] local.WARNING: test [null]
Does anybody know, what can be the problem? Is there something missing in my configuration or in php settings?
This is a know bug in dotenv package - see the discussion here
https://github.com/laravel/framework/issues/8191
This happen to me as well. My workaround is in your config/app.php you have to add this:
'setting' => env('SETTING'),
Then when you want to get the setting config you have to do this:
$env = config('app.setting');

How to mock/stub the config initializer hash in rails 3

Environment : Rails 3.1.1 and Rspec 2.10.1
I am loading all my application configuration through an external YAML file. My initializer (config/initializers/load_config.rb) looks like this
AppConfig = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
And my YAML file sits under config/config.yml
development:
client_system: SWN
b2c_agent_number: '10500'
advocacy_agent_number: 16202
motorcycle_agent_number: '10400'
tso_agent_number: '39160'
feesecure_eligible_months_for_monthly_payments: 1..12
test:
client_system: SWN
b2c_agent_number: '10500'
advocacy_agent_number: 16202
motorcycle_agent_number: '10400'
tso_agent_number: '39160'
feesecure_eligible_months_for_monthly_payments: 1..11
And I access these values as, For example AppConfig['feesecure_eligible_months_for_monthly_payments']
In one of my tests I need AppConfig['feesecure_eligible_months_for_monthly_payments'] to return a different value but am not sure how to accomplish this. I tried the following approach with no luck
describe 'monthly_option_available?' do
before :each do
#policy = FeeSecure::Policy.new
#settlement_breakdown = SettlementBreakdown.new
#policy.stub(:settlement_breakdown).and_return(#settlement_breakdown)
#date = Date.today
Date.should_receive(:today).and_return(#date)
#config = mock(AppConfig)
AppConfig.stub(:feesecure_eligible_months_for_monthly_payments).and_return('1..7')
end
.....
end
In my respective class I am doing something like this
class Policy
def eligible_month?
eval(AppConfig['feesecure_eligible_months_for_monthly_payments']).include?(Date.today.month)
end
....
end
Can someone please point me in the right direction!!
The method that is being called when you do AppConfig['foo'] is the [] method, which takes one argument (the key to retrieve)
Therefore what you could do in your test is
AppConfig.stub(:[]).and_return('1..11')
You can use with to setup different expectations based on the value of the argument, ie
AppConfig.stub(:[]).with('foo').and_return('1..11')
AppConfig.stub(:[]).with('bar').and_return(3)
You don't need to setup a mock AppConfig object - you can stick your stub straight on the 'real' one.

Resources