{"_id":"5772130888dd9819003d3878","project":"576ebdb79c84a31900958aba","version":{"_id":"576ebdb79c84a31900958abd","project":"576ebdb79c84a31900958aba","__v":10,"createdAt":"2016-06-25T17:21:59.854Z","releaseDate":"2016-06-25T17:21:59.854Z","categories":["576ebdb79c84a31900958abe","576ebfc59c84a31900958ac4","576ec32f52f96619007cfb9a","576ec7b7560eef0e00cd3096","576ed4249c84a31900958add","576ed429560eef0e00cd30a3","576ed43a52f96619007cfbb5","576ed44d5a8c72170082b794","577212f20da40019004f0816","57725c7e0a6d610e00de9e4c"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"3.3.0","version":"3.3"},"user":"576ebd239c84a31900958ab9","githubsync":"","__v":21,"category":{"_id":"577212f20da40019004f0816","version":"576ebdb79c84a31900958abd","__v":0,"project":"576ebdb79c84a31900958aba","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-06-28T06:02:26.465Z","from_sync":false,"order":8,"slug":"components","title":"Components"},"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-06-28T06:02:48.002Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"Testbench Component bring Laravel Framework testing features to Laravel package.\n\n* [Installation](#installation)\n* [Configuration](#configuration) \n* [Usage](#usage)\n* [Alternative 3rd Party Testing](#alternative-3rd-party-testing)\n* [Troubleshooting](#troubleshooting)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Installation\"\n}\n[/block]\nYou can either update the `composer.json` file and run `composer install` or run `composer require` directly:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n  \\\"require-dev\\\": {\\n    \\\"orchestra/testbench\\\": \\\"~3.0\\\"\\n  }\\n}\",\n      \"language\": \"json\",\n      \"name\": \"composer.json\"\n    },\n    {\n      \"code\": \"composer require --dev \\\"orchestra/testbench=~3.0\\\"\",\n      \"language\": \"text\",\n      \"name\": \"Terminal\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Usage\"\n}\n[/block]\nTo use Testbench Component, all you need to do is extend `Orchestra\\Testbench\\TestCase` instead of `PHPUnit_Framework_TestCase`. The fixture app booted by `Orchestra\\Testbench\\TestCase` is predefined to follow the base application skeleton of Laravel 5.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n\\t//\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Custom Service Providers\n\nTo load your package service provider, override the `getPackageProviders()`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function getPackageProviders($app)\\n  {\\n\\t\\treturn ['Acme\\\\AcmeServiceProvider'];\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Custom Aliases\n\nTo load your package alias, override the `getPackageAliases`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function getPackageAliases($app)\\n  {\\n\\t\\treturn [\\n    \\t'Acme' => 'Acme\\\\Facade'\\n    ];\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Overriding setUp() method\n\nSince `Orchestra\\Testbench\\TestCase` replace Laravel's `Illuminate\\Foundation\\Testing\\TestCase`, if you need your own `setUp()` implementation, do not forget to call `parent::setUp()`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function setUp()\\n  {\\n\\t\\tparent::setUp();\\n    \\n    // Your code here\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nIf you need to add something early in the application bootstrapping process, you could use the `getEnvironmentSetUp()` method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function getEnvironmentSetUp($app)\\n  {\\n    # Setup default database to use sqlite :memory:\\n    $app['config']->set('database.default', 'testbench');\\n    $app['config']->set('database.connections.testbench', [\\n        'driver'   => 'sqlite',\\n        'database' => ':memory:',\\n        'prefix'   => '',\\n    ]);\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Overriding Console Kernel\n\nYou can easily swap Console Kernel for application bootstrap by overriding `resolveApplicationConsoleKernel()` method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function resolveApplicationConsoleKernel($app)\\n  {\\n    $app->singleton('Illuminate\\\\Contracts\\\\Console\\\\Kernel', 'Acme\\\\Testbench\\\\Console\\\\Kernel');\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Overriding HTTP Kernel\n\nYou can easily swap HTTP Kernel for application bootstrap by overriding `resolveApplicationHttpKernel()` method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function resolveApplicationHttpKernel($app)\\n  {\\n    $app->singleton('Illuminate\\\\Contracts\\\\Http\\\\Kernel', 'Acme\\\\Testbench\\\\Http\\\\Kernel');\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Overriding Application Timezone\n\nYou can also easily override application default timezone, instead of the default `\"UTC\"`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function getApplicationTimezone($app)\\n  {\\n    return 'Asia/Kuala_Lumpur';\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Using Migrations\n\nTestbench include a custom migrations command that support `realpath` option instead of the basic relative `path` option, this would make it easier for you to run database migrations during testing by just including the full realpath to your package `database/migration` folder.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function setUp()\\n  {\\n\\t\\tparent::setUp();\\n    \\n    $this->artisan('migrate', [\\n      '--database' => 'testbench',\\n      '--realpath' => realpath(__DIR__.'/../migrations'),\\n    ]);\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n## Using Model Factories\n\nTestbench include `withFactories()` method to allow you to register custom model factory path for your test suite.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass TestCase extends Orchestra\\\\Testbench\\\\TestCase\\n{\\n  \\n  /* ... */\\n  \\n  protected function setUp()\\n  {\\n\\t\\tparent::setUp();\\n    \\n    $this->withFactories(__DIR__.'/factories');\\n\\t}\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Alternative 3rd Party Testing\"\n}\n[/block]\nThere also 3rd party packages that extends Testbench Component on CodeCeption and PHPSpec:\n\n* [Testbench with CodeCeption](https://bitbucket.org/aedart/testing-laravel)\n* [Testbench with PHPSpec](https://github.com/Pixelindustries/phpspec-testbench)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Troubleshooting\"\n}\n[/block]\n## No supported encrypter found. The cipher and / or key length are invalid.\n\n> RuntimeException: No supported encrypter found. The cipher and / or key length are invalid.\n\nThis error would only occur if your test suite require actual usage of the encrypter. To solve this you can add a dummy `APP_KEY` or use a specific key to your application/package `phpunit.xml`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<phpunit>\\n\\n\\t// ...\\n\\n\\t<php>\\n    <env name=\\\"APP_KEY\\\" value=\\\"AckfSECXIvnK5r28GVIWUAxmbBSjTsmF\\\"/>\\n  </php>\\n\\n</phpunit>\",\n      \"language\": \"xml\",\n      \"name\": \"phpunit.xml\"\n    }\n  ]\n}\n[/block]","excerpt":"Testbench Component for Laravel","slug":"testbench","type":"basic","title":"Testbench"}

Testbench

Testbench Component for Laravel

Testbench Component bring Laravel Framework testing features to Laravel package. * [Installation](#installation) * [Configuration](#configuration) * [Usage](#usage) * [Alternative 3rd Party Testing](#alternative-3rd-party-testing) * [Troubleshooting](#troubleshooting) [block:api-header] { "type": "basic", "title": "Installation" } [/block] You can either update the `composer.json` file and run `composer install` or run `composer require` directly: [block:code] { "codes": [ { "code": "{\n \"require-dev\": {\n \"orchestra/testbench\": \"~3.0\"\n }\n}", "language": "json", "name": "composer.json" }, { "code": "composer require --dev \"orchestra/testbench=~3.0\"", "language": "text", "name": "Terminal" } ] } [/block] [block:api-header] { "type": "basic", "title": "Usage" } [/block] To use Testbench Component, all you need to do is extend `Orchestra\Testbench\TestCase` instead of `PHPUnit_Framework_TestCase`. The fixture app booted by `Orchestra\Testbench\TestCase` is predefined to follow the base application skeleton of Laravel 5. [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n\t//\n}", "language": "php" } ] } [/block] ## Custom Service Providers To load your package service provider, override the `getPackageProviders()`. [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function getPackageProviders($app)\n {\n\t\treturn ['Acme\\AcmeServiceProvider'];\n\t}\n}", "language": "php" } ] } [/block] ## Custom Aliases To load your package alias, override the `getPackageAliases`. [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function getPackageAliases($app)\n {\n\t\treturn [\n \t'Acme' => 'Acme\\Facade'\n ];\n\t}\n}", "language": "php" } ] } [/block] ## Overriding setUp() method Since `Orchestra\Testbench\TestCase` replace Laravel's `Illuminate\Foundation\Testing\TestCase`, if you need your own `setUp()` implementation, do not forget to call `parent::setUp()`: [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function setUp()\n {\n\t\tparent::setUp();\n \n // Your code here\n\t}\n}", "language": "php" } ] } [/block] If you need to add something early in the application bootstrapping process, you could use the `getEnvironmentSetUp()` method: [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function getEnvironmentSetUp($app)\n {\n # Setup default database to use sqlite :memory:\n $app['config']->set('database.default', 'testbench');\n $app['config']->set('database.connections.testbench', [\n 'driver' => 'sqlite',\n 'database' => ':memory:',\n 'prefix' => '',\n ]);\n\t}\n}", "language": "php" } ] } [/block] ## Overriding Console Kernel You can easily swap Console Kernel for application bootstrap by overriding `resolveApplicationConsoleKernel()` method: [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function resolveApplicationConsoleKernel($app)\n {\n $app->singleton('Illuminate\\Contracts\\Console\\Kernel', 'Acme\\Testbench\\Console\\Kernel');\n\t}\n}", "language": "php" } ] } [/block] ## Overriding HTTP Kernel You can easily swap HTTP Kernel for application bootstrap by overriding `resolveApplicationHttpKernel()` method: [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function resolveApplicationHttpKernel($app)\n {\n $app->singleton('Illuminate\\Contracts\\Http\\Kernel', 'Acme\\Testbench\\Http\\Kernel');\n\t}\n}", "language": "php" } ] } [/block] ## Overriding Application Timezone You can also easily override application default timezone, instead of the default `"UTC"`: [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function getApplicationTimezone($app)\n {\n return 'Asia/Kuala_Lumpur';\n\t}\n}", "language": "php" } ] } [/block] ## Using Migrations Testbench include a custom migrations command that support `realpath` option instead of the basic relative `path` option, this would make it easier for you to run database migrations during testing by just including the full realpath to your package `database/migration` folder. [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function setUp()\n {\n\t\tparent::setUp();\n \n $this->artisan('migrate', [\n '--database' => 'testbench',\n '--realpath' => realpath(__DIR__.'/../migrations'),\n ]);\n\t}\n}", "language": "php" } ] } [/block] ## Using Model Factories Testbench include `withFactories()` method to allow you to register custom model factory path for your test suite. [block:code] { "codes": [ { "code": "<?php\n\nclass TestCase extends Orchestra\\Testbench\\TestCase\n{\n \n /* ... */\n \n protected function setUp()\n {\n\t\tparent::setUp();\n \n $this->withFactories(__DIR__.'/factories');\n\t}\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Alternative 3rd Party Testing" } [/block] There also 3rd party packages that extends Testbench Component on CodeCeption and PHPSpec: * [Testbench with CodeCeption](https://bitbucket.org/aedart/testing-laravel) * [Testbench with PHPSpec](https://github.com/Pixelindustries/phpspec-testbench) [block:api-header] { "type": "basic", "title": "Troubleshooting" } [/block] ## No supported encrypter found. The cipher and / or key length are invalid. > RuntimeException: No supported encrypter found. The cipher and / or key length are invalid. This error would only occur if your test suite require actual usage of the encrypter. To solve this you can add a dummy `APP_KEY` or use a specific key to your application/package `phpunit.xml`. [block:code] { "codes": [ { "code": "<phpunit>\n\n\t// ...\n\n\t<php>\n <env name=\"APP_KEY\" value=\"AckfSECXIvnK5r28GVIWUAxmbBSjTsmF\"/>\n </php>\n\n</phpunit>", "language": "xml", "name": "phpunit.xml" } ] } [/block]