Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户的Laravel phpunit访问被拒绝_Php_Unit Testing_Laravel 4_Phpunit - Fatal编程技术网

用户的Laravel phpunit访问被拒绝

用户的Laravel phpunit访问被拒绝,php,unit-testing,laravel-4,phpunit,Php,Unit Testing,Laravel 4,Phpunit,我想用Laravel4做单元测试。但我有个问题 这是我的控制器 class HalisahalarController extends BaseController { public function getIndex(){ // halı sahaların bilgilerini toplamak için bir dizi değişken oluşturulur $halisahalar = []; // halı sahalar

我想用Laravel4做单元测试。但我有个问题

这是我的控制器

class HalisahalarController extends BaseController {

    public function getIndex(){
        // halı sahaların bilgilerini toplamak için bir dizi değişken oluşturulur
        $halisahalar = [];
        // halı sahaların foreach içinde gerekli bilgileri alınır
        foreach (HalisahaAccount::with(
            'halisahaInformation',
            'halisahaAdress',
            'services',
            'halisahaCoverPhoto',
            'halisahaUrl'
        )->get() as $halisaha) {
            $hs['id'] = $halisaha->id; #halı saha id
            $hs['name'] = $halisaha->halisahaInformation->halisaha_name; #halı saha ad
            $hs['adress']['province'] = isset($halisaha->halisahaAdress) ? $halisaha->halisahaAdress->province->province : -1; # halı saha il
            $hs['adress']['county'] = isset($halisaha->halisahaAdress) ? $halisaha->halisahaAdress->county->county : -1; #halı saha ilçe
            $hs['services'] = $halisaha->services->toArray(); #halı saha servisleri
            $hs['coverPhoto'] = $halisaha->halisahaCoverPhoto->toArray(); #halı saha kapak foto
            $hs['halisahaUrl'] = isset($halisaha->halisahaUrl) ? $halisaha->halisahaUrl->url : -1; #halı saha url
            // alınan veriler dizi değişken içine itilir
            array_push($halisahalar,$hs);
        }        
        return View::make('index',array('halisahalar' => $halisahalar));
    }
}
这是我的测试代码

class HalisahalarControllerTest extends TestCase {

    /**
     * /halisahalar test
     *
     * @dataProvider halisahaDatas
     */
    public function testGetIndex($halisaha)
    {

        $crawler = $this->client->request('GET', '/halisahalar');
        // yanıt başarılı bir şekilde geldi mi
        $this->assertResponseOk();
        // 200 kodu geldi mi
        $this->assertResponseStatus(200);
    }

    /**
     * halisaha datas
     */
    public function halisahaDatas () {
        return [
            [
                'id' => 1,
                'name' => 'Lider Halı Saha',
                'adress' => [
                    'province' => 'İstanbul',
                    'county' => 'Sultanbeyli'
                ],
                'services' => [
                    [
                        'service' => 'Duş',
                        'icon' => 'dus.png'
                    ],
                    [
                        'service' => 'Çeşitli Oyunlar',
                        'icon' => 'oyun.png'
                    ]
                ],
                'coverPhoto' => [
                    [
                        'photo' => 'lider4.jpg'
                    ]
                ],
                'halisahaUrl' => 'lider'
            ],[
                'id' => 2,
                'name' => 'Çalışkan Halı Saha',
                'adress' => [
                    'province' => 'İstanbul',
                    'county' => 'Sancaktepe'
                ],
                'services' => [
                    [
                        'service' => 'Duş',
                        'icon' => 'dus.png'
                    ],
                    [
                        'service' => 'İnternet',
                        'icon' => 'wifi.png'
                    ]
                ],
                'coverPhoto' => [
                ],
                'halisahaUrl' => 'caliskan-halisaha'
            ]
        ];
    }
}
我得到以下错误:

1) HalisahalarControllerTest::testGetIndex with data set #0 (1, 'Lider Halı Saha', array('İstanbul', 'Sultanbeyli'), array(array('Duş', 'dus.png'), arra
y('Çeşitli Oyunlar', 'oyun.png')), array(array('lider4.jpg')), 'lider')
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'halisaha_HSHadm'@'localhost' (using password: YES)

2) HalisahalarControllerTest::testGetIndex with data set #1 (2, 'Çalışkan Halı Saha', array('İstanbul', 'Sancaktepe'), array(array('Duş', 'dus.png'),
 array('İnternet', 'wifi.png')), array(), 'caliskan-halisaha')
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'halisaha_HSHadm'@'localhost' (using password: YES)

我不想在测试中使用数据库。我想使用halisahaDatas方法中的数据。我认为php单元试图连接到数据库时出错。

您应该在控制器中使用Laravel依赖项注入,如下所示:

class HalisahalarController extends BaseController {
   protected $halisahaAccount;

   public function __construct(HalisahaAccount $halisahaAccount){
      $this->halisahaAccount = $halisahaAccount;
   }
   public function getIndex(){
       $yourAccounts = $this->halisahaAccount->with(
         'halisahaInformation',
        'halisahaAdress',
        'services',
        'halisahaCoverPhoto',
        'halisahaUrl'
       )->get();
   }
}
class HalisahalarControllerTest extends TestCase {

    /**
    * /halisahalar test
    *
    * @dataProvider halisahaDatas
    */
    public function testGetIndex($halisaha)
    {
       $mockHalisahaAccount = new Mockery::mock('HalisahaAccount');

       $mockHalisahaAccount->shouldReceive('with')->once()
       ->with('halisahaInformation',
        'halisahaAdress',
        'services',
        'halisahaCoverPhoto',
        'halisahaUrl')->andReturnSelf();

      $mockHalisahaAccount->shouldReceive('get')->once()->andReturn($this->halisahaDatas());

       $this->app->instance('HalisahaAccount', $mockHalisahaAccount);

       $crawler = $this->client->request('GET', '/halisahalar');
       // yanıt başarılı bir şekilde geldi mi
       $this->assertResponseOk();
       // 200 kodu geldi mi
       $this->assertResponseStatus(200);
    }
} 
在测试中,您将执行以下操作:

class HalisahalarController extends BaseController {
   protected $halisahaAccount;

   public function __construct(HalisahaAccount $halisahaAccount){
      $this->halisahaAccount = $halisahaAccount;
   }
   public function getIndex(){
       $yourAccounts = $this->halisahaAccount->with(
         'halisahaInformation',
        'halisahaAdress',
        'services',
        'halisahaCoverPhoto',
        'halisahaUrl'
       )->get();
   }
}
class HalisahalarControllerTest extends TestCase {

    /**
    * /halisahalar test
    *
    * @dataProvider halisahaDatas
    */
    public function testGetIndex($halisaha)
    {
       $mockHalisahaAccount = new Mockery::mock('HalisahaAccount');

       $mockHalisahaAccount->shouldReceive('with')->once()
       ->with('halisahaInformation',
        'halisahaAdress',
        'services',
        'halisahaCoverPhoto',
        'halisahaUrl')->andReturnSelf();

      $mockHalisahaAccount->shouldReceive('get')->once()->andReturn($this->halisahaDatas());

       $this->app->instance('HalisahaAccount', $mockHalisahaAccount);

       $crawler = $this->client->request('GET', '/halisahalar');
       // yanıt başarılı bir şekilde geldi mi
       $this->assertResponseOk();
       // 200 kodu geldi mi
       $this->assertResponseStatus(200);
    }
} 

你需要模仿这个模型。因为目前我不知道你的
halisahaDatas
是如何进入控制器的。因此,当您在模型上调用
get()
时,它当然使用了DB。如何断开模型上的DB进行测试。我在应用程序中使用get(),但你不能这样做。您可以选择使用或模拟模型。