Laravel phpunit Access denied for user

I want to unit test using 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 am getting 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 don't want to use the database in the test. I want to use data in halisahaDatas method . I think the php module is trying to connect to the database and get an error.

+3


source to share


1 answer


You should use Laravel dependency injection in your 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 would 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);
    }
} 

      

+1


source







All Articles