2

ここで Laravel 4.2のパッケージリンクを作成しています。単体テストとデータベース テストを作成したいと考えています。私が読んだ限りでは、テストですべての機能を使用するには、Laravel アプリのインスタンスを作成する必要があります。

composer.jsonこのようにLaravelパッケージを修正して追加しました

 "require-dev": {
        "phpunit/phpunit" : "4.*",
        "laravel/laravel": "4.2.*"
    },

composer updateLaravel のような TestCase クラスを作成した後

public function createApplication()
    {
        $unitTesting = true;
        $testEnvironment = 'testing';
        return require __DIR__.'/../vendor/laravel/laravel/bootstrap/start.php';
    }

問題は、start.php で require を実行するときに、パスを使用してフレームワークを作成するときにエラーが発生することです。 ./vjroby/laravel-nonce/vendor/laravel/laravel/vendor/laravel/framework/src

そのようなファイルがないため、ファイルは ./vjroby/laravel-nonce/vendor/laravel/framework/src

4

3 に答える 3

1

データベースのテストをメモリ内の sqlite と統合することができました。答えを投稿しているので、将来誰かがそれを必要とするかもしれません。

  1. tests ディレクトリ内に bootstrap ディレクトリを作成しました
  2. phpunit.xml を変更して、boostrap ディレクトリから autoload をロードします。bootstrap="tests/bootstrap/autoload.php"
  3. boostrap ディレクトリ内に 3 つのファイルGit Linkがあり、これらのファイルの助けを借りて、テストする Laravel アプリケーションを作成することができました
  4. 上記は私のデータベース テストです。アプリに移行を実行させることができなかったため、移行をクレートする必要がありました。試してみましたArtisan::call()が、成功しませんでした。

     <?php
        use Mockery as m;
    
        class NonceTest extends Illuminate\Foundation\Testing\TestCase{
    
        public function tearDown()
        {
            m::close();
        }
        public function testWithDatabase(){
        $nonce = new \Vjroby\LaravelNonce\Nonce();
    
        $nonceId = uniqid();
    
        $nonce->setNonce($nonceId, '');
    
        $nonce = DB::table('nonce')->where('id', $nonceId)->get();
    
        $this->assertTrue(count($nonce) == 1);
    
        $this->assertEquals($nonceId, $nonce[0]->id);
    }
    
    /**
     * Creates the application.
     *
     * Needs to be implemented by subclasses.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;
    
        $testEnvironment = 'testing';
    
        $app = require __DIR__.'/bootstrap/start.php';
    
        $app->register('Vjroby\LaravelNonce\LaravelNonceServiceProvider');
    
        return $app;
    }
    public function setUp()
    {
        parent::setUp();
    
        $this->app['config']->set('database.default', 'testing');
    
        $a = __DIR__.'/../src/migrations';
    
        $this->app['config']->set('database.connections.testing', array(
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ));
    
        $this->migrate();
    }
    public function migrate()
    {
        Schema::dropIfExists('nonce');
        Schema::create('nonce', function(\Illuminate\Database\Schema\Blueprint $table){
            $table->string('id',255);
            $table->string('data',255)->nullable();
            $table->nullableTimestamps();
    
            $table->unique(['id', 'data']);
            $table->index(['id', 'data']);
        });
    
    }
    
    }
    
于 2015-02-12T09:02:29.163 に答える