symfony测试未定义的函数

时间:2016-11-09 06:49:59

标签: symfony testing

我的测试有问题。我不知道为什么我的函数未定义。我添加使用statetment,phpstorm看到这个类。但是当使用undefined运行测试错误时。

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

2 个答案:

答案 0 :(得分:0)

首先,您必须检查您的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

如果你做的一切都正确,那么它应该工作。至少我能够运行你的代码。

答案 1 :(得分:0)

yes really have :) i take a picture for example : )

enter image description here