2016-11-09 79 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

所有的作品,我已经进入了文件夹测试。如何从我的测试时,我创建对象我的解析器把对象实体管理器?因为它需要课堂? –