php - Laravel phpunit 拒绝用户访问

标签 php unit-testing laravel-4 phpunit

我想用 Laravel 4 进行单元测试。但是我遇到了问题。

这是我的 Controller

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 单元尝试连接到数据库并出现错误。

最佳答案

你应该像这样在你的 Controller 中使用 Laravel 依赖注入(inject):

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);
    }
} 

关于php - Laravel phpunit 拒绝用户访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633652/

相关文章:

PHP:在准备好的语句上获取插入确认

php - 通过嵌入式YouTube视频将帖子拉入PHP页面

php - 如何在 codeigniter 中接收 post/get 请求

android - 如何在 Android 中使用单元测试或仪器测试来测试警报管理器

Django 1.3 测试错误 : ValueError: list. remove(x): x not in list

php - 使用多个 where 子句创建 SQL 查询

command - 具有嵌套闭包、命令和依赖项的队列管理器

php - 如何覆盖默认布局

c# - 如何使用 IDataContext 接口(interface)运行存储过程?

php - 我在 function/vendor/guzzlehttp/psr7/src/functions.php 第 77 行的 laravel 中有错误