2016-11-09 87 views
0

我有我的測試問題。我不知道爲什麼我的函數是未定義的。我添加使用狀態,phpstorm看到這個類。但是當運行測試錯誤與undefined。symfony test未定義函數

namespace tests\AppBundle\Parser; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
use AppBundle\Parser\CommissionDataParser; 

class CommissionDataParserTest extends WebTestCase 
{ 

    public function testGroupOrdersByWeek() 
    { 

     $orders = [ 
      0 => [ 
       'date' => '2016-01-10', 
       'client_id'  => '2', 
       'client_type'=> 'natural', 
       'operation_type' => 'cash_in', 
       'operation_sum'  => '200.00', 
       'operation_currency'  => 'EUR', 
      ], 

      1 => [ 
       'date' => '2016-01-05', 
       'client_id'  => '1', 
       'client_type'=> 'legal', 
       'operation_type' => 'cash_out', 
       'operation_sum'  => '300.00', 
       'operation_currency'  => 'EUR', 
      ], 

      2 => [ 

       'date' => '2016-01-11', 
       'client_id'  => '1', 
       'client_type'=> 'natural', 
       'operation_type' => 'cash_out', 
       'operation_sum'  => '30000', 
       'operation_currency'  => 'JPY' 
      ] 
     ]; 


     $expected = [ 
      0 => [ 
       'date' => '2016-01-05', 
       'client_id'  => '2', 
       'client_type'=> 'natural', 
       'operation_type' => 'cash_in', 
       'operation_sum'  => '200.00', 
       'operation_currency'  => 'EUR', 
      ], 

      1 => [ 
       'date' => '2016-01-10', 
       'client_id'  => '1', 
       'client_type'=> 'legal', 
       'operation_type' => 'cash_out', 
       'operation_sum'  => '300.00', 
       'operation_currency'  => 'EUR', 
      ], 

      2 => [ 
       'date' => '2016-01-11', 
       'client_id'  => '1', 
       'client_type'=> 'natural', 
       'operation_type' => 'cash_out', 
       'operation_sum'  => '30000', 
       'operation_currency'  => 'JPY' 
      ] 
     ]; 

     $um = new CommissionDataParser(); 
     $result = $um->groupOrdersByWeek($orders); 


     $this->assertEquals($expected, $result, '**** -->>>>> result array wrong'); 

    } 

有功能,我想測試一下:我把這個類的小部分,例如

namespace AppBundle\Parser; 


class CommissionDataParser 
{ 
    public function getData($file) 
    { 
     $orders = $this->extractOrders($file); 

     if (is_array($orders)) { 
      $orders = $this->groupOrdersByWeek($orders); 
     } 

     // ... 
    } 


    public function extractOrders($file) 
    { 
     $orders = []; 
     $data = []; 
     //$lines = explode(PHP_EOL, file_get_contents($file)); 

     if (($handle = fopen($file, "r")) !== FALSE) { 
      while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) { 

       $num = count($row); 

       if ($num !== 6) { 
        return 'Badly structured file'; 

       } else if ($num == 0) { 
        return 'file is empty'; 
       } 

       $data[] = $row; 
      } 
      fclose($handle); 
     } 

     foreach ($data as $row) 
     { 
      $orders[] = [ 
       'date' => $row[0], 
       'client_id' => $row[1], 
       'client_type' => $row[2], 
       'operation_type' => $row[3], 
       'operation_sum' => $row[4], 
       'operation_currency' => $row[5] 
      ]; 
     } 

     return $orders; 
    } 
+0

ü可以粘貼在這裏üCommissionDataParser的代碼? –

回答

1

在第一,你必須檢查你的PHPUnit的使用app/autoload.php自舉。打開phpunit.xml.dist文件在你的項目的根,找到這一行:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd" 
    backupGlobals="false" 
    colors="true" 
    bootstrap="app/autoload.php" 
> 

如果你發現這行bootstrap="app/autoload.php"在你的文件,然後沒事。

接下來,檢查您的文件CommissionDataParser.php物理上位於此目錄AppBundle\Parser。此文件的完整路徑必須爲YOUR_PROJECT_ROOT\src\AppBundle\Parser\CommissionDataParser.php

如果你做的一切都正確,那麼它應該工作。至少我能夠運行你的代碼。

+0

它是bootstrap =「autoload.php」當我運行這個路徑時,我有未定義函數的錯誤,如果我添加應用程序/ autoload.php,然後我得到無法打開文件「/ var/www/symfony/app/app/autoload.php」。 –

+0

CommissionDataParser.php的路徑正確 –

+0

好的,最後,你是否真的在'CommissionDataParser'類中有'groupOrdersByWeek'方法?))) –

0

是真的:)我拍照例如:)

enter image description here

+0

所有的作品,我已經進入了文件夾測試。如何從我的測試時,我創建對象我的解析器把對象實體管理器?因爲它需要課堂? –