Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Mocking a connection</h2> <p>Mocking a database connection gets complicated (<a href="https://github.com/fideloper/ResourceCache/blob/master/tests/EloquentResourceTest.php#L73" rel="nofollow">I've done it before, it's messy!</a>). </p> <p>This is some code I had working in <a href="https://github.com/fideloper/ResourceCache/blob/master/tests/EloquentResourceTest.php" rel="nofollow">a unit test</a> after a ton of trial and error some months ago:</p> <pre><code>Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver = m::mock('Illuminate\Database\ConnectionResolverInterface')); $resolver-&gt;shouldReceive('connection')-&gt;andReturn($mockConnection = m::mock('Illuminate\Database\ConnectionInterface')); $mockConnection-&gt;shouldreceive('getPostProcessor')-&gt;andReturn(m::mock('Illuminate\Database\Query\Processors\Processor')); $mockConnection-&gt;shouldReceive('getQueryGrammar')-&gt;andReturn($queryGrammar = m::mock('Illuminate\Database\Query\Grammars\Grammar')); $queryGrammar-&gt;shouldReceive('getDateFormat')-&gt;andReturn('Y-m-d H:i:s'); </code></pre> <h2>Using sqllite</h2> <p>I will suggest, then, to actually use a sqlite database for testing database connections. I've found this much easier. </p> <p>Testing with PHPunit triggers the <code>testing</code> environment in Laravel, so you can define an in-memory (only on for that request) sqlite database connection on that environment and and even seed it in your test setup. </p> <p>Here's a sample gist for <a href="https://gist.github.com/fideloper/7413518" rel="nofollow">testing using an in-memory sqlite database</a>.</p> <h2>Testing things already tested</h2> <p>Your unit test here might be simplistic for example, I'm not sure - However, you're testing something that's <a href="https://github.com/laravel/framework/blob/master/tests/Database/DatabaseEloquentModelTest.php" rel="nofollow">already tested in Laravel</a>. Consider carefully what you decide to test :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload