dsfdsfsdf45489 2014-12-24 08:10
浏览 65
已采纳

Laravel phpunit用户拒绝访问

I want to do unit testing with Laravel 4. But i have a problem.

This is my 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));
    }
}

And this is my test code

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'
            ]
        ];
    }
}

I get the following errors:

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)

I dont want to use database in the test. I want to use data in halisahaDatas method. I think php unit trying to connect to the database and getting error.

  • 写回答

1条回答 默认 最新

  • doupai8095 2015-01-13 11:19
    关注

    You should use Laravel dependency injection in you controller like this:

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

    And in your tests you will do something like this:

    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);
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题