Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I test Eloquent in Workbench?
    primarykey
    data
    text
    <p><strong>EDITS: 1</strong></p> <p>I need to test an Eloquent model by touching the database and I Have this test case:</p> <p> <pre><code>use Mockery as m; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Database\DatabaseManager; use Illuminate\Database\ConnectionResolver; use Illuminate\Foundation\Application; use Illuminate\Foundation\Artisan; use Illuminate\Container\Container; use Illuminate\Config\Repository as Config; use Illuminate\Database\Console\Migrations\MigrateCommand; use Illuminate\Database\Migrations\Migrator; use Illuminate\Database\Migrations\DatabaseMigrationRepository; use Illuminate\Filesystem\Filesystem; class Module extends Eloquent { protected $table = 'modules'; protected $fillable = array('name'); } class UserTest extends PHPUnit_Framework_TestCase{ public function setUp() { $this-&gt;modelName = 'My Model Name'; $this-&gt;migrationsTable = 'migrations'; $this-&gt;environment = 'testing'; $this-&gt;connectionName = 'postgresql'; // postgresql memory date_default_timezone_set('UTC'); $this-&gt;app = new Application; $this-&gt;migrationPath = __DIR__ . '/../../migrations'; $this-&gt;app-&gt;instance('path', __DIR__); $this-&gt;app-&gt;instance('config', $config = new Config( $this-&gt;app-&gt;getConfigLoader(), $this-&gt;environment )); $this-&gt;app['config']['app'] = array( 'debug' =&gt; true, 'url' =&gt; 'http://localhost', 'timezone' =&gt; 'UTC', 'locale' =&gt; 'en', 'key' =&gt; 'YourSecretKey!!!', 'providers' =&gt; array( 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', 'Illuminate\Cache\CacheServiceProvider', 'Illuminate\Session\CommandsServiceProvider', 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider', 'Illuminate\Routing\ControllerServiceProvider', 'Illuminate\Cookie\CookieServiceProvider', 'Illuminate\Database\DatabaseServiceProvider', 'Illuminate\Encryption\EncryptionServiceProvider', 'Illuminate\Filesystem\FilesystemServiceProvider', 'Illuminate\Hashing\HashServiceProvider', 'Illuminate\Html\HtmlServiceProvider', 'Illuminate\Log\LogServiceProvider', 'Illuminate\Mail\MailServiceProvider', 'Illuminate\Database\MigrationServiceProvider', 'Illuminate\Pagination\PaginationServiceProvider', 'Illuminate\Queue\QueueServiceProvider', 'Illuminate\Redis\RedisServiceProvider', 'Illuminate\Remote\RemoteServiceProvider', 'Illuminate\Auth\Reminders\ReminderServiceProvider', 'Illuminate\Database\SeedServiceProvider', 'Illuminate\Session\SessionServiceProvider', 'Illuminate\Translation\TranslationServiceProvider', 'Illuminate\Validation\ValidationServiceProvider', 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', ), 'manifest' =&gt; '/meta', 'aliases' =&gt; array( 'App' =&gt; 'Illuminate\Support\Facades\App', 'Artisan' =&gt; 'Illuminate\Support\Facades\Artisan', 'Auth' =&gt; 'Illuminate\Support\Facades\Auth', 'Blade' =&gt; 'Illuminate\Support\Facades\Blade', 'Cache' =&gt; 'Illuminate\Support\Facades\Cache', 'ClassLoader' =&gt; 'Illuminate\Support\ClassLoader', 'Config' =&gt; 'Illuminate\Support\Facades\Config', 'Controller' =&gt; 'Illuminate\Routing\Controller', 'Cookie' =&gt; 'Illuminate\Support\Facades\Cookie', 'Crypt' =&gt; 'Illuminate\Support\Facades\Crypt', 'DB' =&gt; 'Illuminate\Support\Facades\DB', 'Eloquent' =&gt; 'Illuminate\Database\Eloquent\Model', 'Event' =&gt; 'Illuminate\Support\Facades\Event', 'File' =&gt; 'Illuminate\Support\Facades\File', 'Form' =&gt; 'Illuminate\Support\Facades\Form', 'Hash' =&gt; 'Illuminate\Support\Facades\Hash', 'HTML' =&gt; 'Illuminate\Support\Facades\HTML', 'Input' =&gt; 'Illuminate\Support\Facades\Input', 'Lang' =&gt; 'Illuminate\Support\Facades\Lang', 'Log' =&gt; 'Illuminate\Support\Facades\Log', 'Mail' =&gt; 'Illuminate\Support\Facades\Mail', 'Paginator' =&gt; 'Illuminate\Support\Facades\Paginator', 'Password' =&gt; 'Illuminate\Support\Facades\Password', 'Queue' =&gt; 'Illuminate\Support\Facades\Queue', 'Redirect' =&gt; 'Illuminate\Support\Facades\Redirect', 'Redis' =&gt; 'Illuminate\Support\Facades\Redis', 'Request' =&gt; 'Illuminate\Support\Facades\Request', 'Response' =&gt; 'Illuminate\Support\Facades\Response', 'Route' =&gt; 'Illuminate\Support\Facades\Route', 'Schema' =&gt; 'Illuminate\Support\Facades\Schema', 'Seeder' =&gt; 'Illuminate\Database\Seeder', 'Session' =&gt; 'Illuminate\Support\Facades\Session', 'SSH' =&gt; 'Illuminate\Support\Facades\SSH', 'Str' =&gt; 'Illuminate\Support\Str', 'URL' =&gt; 'Illuminate\Support\Facades\URL', 'Validator' =&gt; 'Illuminate\Support\Facades\Validator', 'View' =&gt; 'Illuminate\Support\Facades\View', ), ); $this-&gt;app['config']['database'] = array('connections' =&gt; array( 'memory' =&gt; array( 'driver' =&gt; 'sqlite', 'database' =&gt; ':memory:', 'prefix' =&gt; '' ), 'postgresql' =&gt; array( 'driver' =&gt; 'pgsql', 'host' =&gt; 'localhost', 'database' =&gt; 'antoniocarlosribeiro', 'username' =&gt; 'antoniocarlos', 'password' =&gt; 'basswort', 'charset' =&gt; 'utf8', 'prefix' =&gt; '', 'schema' =&gt; 'public', ), ) ); $this-&gt;factory = new ConnectionFactory($this-&gt;app); $this-&gt;manager = new DatabaseManager($this-&gt;app, $this-&gt;factory); $this-&gt;migrateCommand = new MigrateCommand( new Migrator( new DatabaseMigrationRepository( $this-&gt;manager, $this-&gt;migrationsTable ), $this-&gt;manager, new Filesystem ), $this-&gt;migrationPath ); $this-&gt;model = new Module; $this-&gt;model-&gt;setConnectionResolver($this-&gt;manager); $this-&gt;model-&gt;setConnection($this-&gt;connectionName); // $this-&gt;app-&gt;instance('artisan', $artisan = new Artisan($this-&gt;app)); // $this-&gt;app['artisan']-&gt;add($this-&gt;migrateCommand); // $this-&gt;app['artisan']-&gt;call('migrate:refresh'); } public function tearDown() { m::close(); } public function testCreated() { $this-&gt;assertInstanceOf('Illuminate\Database\Eloquent\Model', $this-&gt;model); } public function testDeleteCreateSelect() { $this-&gt;model-&gt;on($this-&gt;connectionName)-&gt;where('name', $this-&gt;modelName)-&gt;delete(); $this-&gt;model-&gt;on($this-&gt;connectionName)-&gt;insert( array('name' =&gt; $this-&gt;modelName, 'created_at' =&gt; new DateTime, 'updated_at' =&gt; new DateTime) ); $this-&gt;assertEquals($this-&gt;model-&gt;where('name', $this-&gt;modelName)-&gt;first()-&gt;name, $this-&gt;modelName); } } </code></pre> <p>This thing is working fine. Delete, insert and select are working and hitting the database, so I get a green.</p> <p>I plan to develop the tests on database, to take a look at the data and then move to <code>:memory:</code>, for speed.</p> <p>The problem now is that I need Artisan to be working, so I can <code>migrate:refresh</code> on every run, but if I enable those 3 artisan lines it goes down to a recursive call to artisan:</p> <pre><code> PHP 1. {main}() /usr/share/phpunit/vendor/phpunit/phpunit/composer/bin/phpunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/share/phpunit/vendor/phpunit/phpunit/composer/bin/phpunit:63 PHP 3. PHPUnit_TextUI_Command-&gt;run() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129 PHP 4. PHPUnit_TextUI_TestRunner-&gt;doRun() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176 PHP 5. PHPUnit_Framework_TestSuite-&gt;run() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349 PHP 6. PHPUnit_Framework_TestSuite-&gt;run() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705 PHP 7. PHPUnit_Framework_TestSuite-&gt;runTest() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745 PHP 8. PHPUnit_Framework_TestCase-&gt;run() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775 PHP 9. PHPUnit_Framework_TestResult-&gt;run() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:783 PHP 10. PHPUnit_Framework_TestCase-&gt;runBare() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648 PHP 11. UserTest-&gt;setUp() /usr/share/phpunit/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:835 PHP 12. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/tests/Package/UserTest.php:174 PHP 13. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/tests/Package/UserTest.php:174 PHP 14. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 15. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 16. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:0 PHP 17. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 18. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 19. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:0 PHP 20. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 21. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 22. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:0 PHP 23. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 24. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 25. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:0 PHP 26. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 27. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 28. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:0 PHP 29. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 30. Illuminate\Foundation\Artisan-&gt;add() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 PHP 31. Illuminate\Foundation\Artisan-&gt;__call() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:0 PHP 32. call_user_func_array() /dev.app/workbench/antonio/package/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:82 </code></pre> <p>Any leads on instantiating and using Artisan correctly?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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