在foreach循环中更改方法返回值不起作用

时间:2017-02-10 15:44:12

标签: php arrays multidimensional-array phpunit magento2

我创建测试以检查数据验证。下面我发布了我的phpunit ValidatorTest和Validator类的代码。没有foreach循环它工作正常。 首先,我得到的数组里面可以有很多数组。每个数组必须包含两个键:'code'和'tax_class_id'。我的问题是,在第二次迭代时,我的Mock - groupSearchResultsInterfaceMock没有设置正确的数据。函数checkByCodeIfGroupExists获取空数组,尽管它应该得到['General']。当我在我的dataProvider中更改它们的顺序时,那个带有'General'代码的数组就是第一个,它给了我预期的错误。我必须在每次迭代中更改两个函数中的模拟返回值。我使用了xdebug,而taxClassRepositoryInterfaceMock正在以正确的方式更改它们。我试图理解它为什么会发生但却一无所获。我也尝试改变setUp中的一些东西但没有改变。

protected $existingTaxClassesId = [1, 2, 3];

protected $existingCodeGroups = ['General', 'NOT LOGGED IN'];

public function fullyValidBothDataProvider()
{
    return [
        [
            [
                ['code' => 'first_customer_group', 'tax_class_id' => 10],
                ['code' => 'General', 'tax_class_id' => 1],
            ]
        ]
    ];
}

/**
* @dataProvider fullyValidBothDataProvider
* @test
* @param array $validData
*/
public function passingValidationWhenTaxClassIdAndGroupCodeAreValidAndWhenTaxCodeExistsAndGroupCodeDoesNot(
    array $validData
) {
    foreach ($validData as $value) {
        $this->taxClassRepositoryInterfaceMock->method('get')->willReturn($value['tax_class_id']);
        $items = in_array($value['code'], $this->existingCodeGroups) ? [$value['code']] : [];
        $this->groupSearchResultsInterfaceMock->method('getItems')->willReturn($items);

        $this->assertTrue($this->validatorMock->validateData($validData));
    }
}

/**
 * Validates customer groups data
 *
 * @param array $data
 * @return bool
 * @throws GroupDataException when customer group data is incorrect
 */
public function validateData(array $data): bool
{
    $taxClassIdKey = GroupInterface::TAX_CLASS_ID;
    $groupCodeKey = GroupInterface::CODE;

    if (empty($data)) {
        throw new DataNotSetException(__('Data is empty.'));
    } else {

        foreach ($data as $group) {

            if (!isset($group[$taxClassIdKey]) || !isset($group[$groupCodeKey]) || !is_int($group[$taxClassIdKey])) {
                throw new GroupDataException(__('Invalid data format. Every group array must contain ' .
                    $taxClassIdKey . ' and integer ' . $groupCodeKey . '.'));

            } elseif ($this->checkByCodeIfGroupExists($group[$groupCodeKey])) {
                throw new CodeGroupException(__('Group code ' . $group[$groupCodeKey] . ' already exists.'));

            } elseif (!$this->checkIfTaxClassIdExists($group[$taxClassIdKey])) {
                throw new TaxClassException(__('Tax class id ' . $group[$taxClassIdKey] . ' does not exist.'));
            }
        }
    }

    return true;
}

/**
 * Return true if given group code exist
 *
 * @param string $groupCode
 * @return bool
 */
public function checkByCodeIfGroupExists(string $groupCode): bool
{
    $filter = $this->filterBuilder
        ->setField('customer_group_code')
        ->setValue($groupCode)
        ->setConditionType('eq')
        ->create();

    $this->searchCriteriaBuilder->addFilters([$filter]);
    $searchCriteria = $this->searchCriteriaBuilder->create();
    $result = $this->groupRepository->getList($searchCriteria);

    $items = $result->getItems();
    return !empty($items);
}



/**
 * Test setup
 */
public function setUp()
{
    //
    $objectFactoryMock = $this->getMockBuilder('Magento\Framework\Api\ObjectFactory')
        ->disableOriginalConstructor()
        ->getMock();

    $filterBuilderMock = $this->getMock('Magento\Framework\Api\FilterBuilder', ['setValue'], [$objectFactoryMock],
        '');
    $this->filterBuilderMock = $filterBuilderMock;
    $this->filterBuilderMock->method('setValue')->willReturn($filterBuilderMock);

    $this->groupSearchResultsInterfaceMock = $this->getMock('Magento\Customer\Api\Data\GroupSearchResultsInterface');

    $this->filterGroupBuilderMock = $this->getMockBuilder('Magento\Framework\Api\Search\FilterGroupBuilder')
        ->setMethods(['setFilters'])
        ->disableOriginalConstructor()
        ->getMock();
    $this->filterGroupBuilderMock->method('setFilters')->willReturn($this->filterGroupBuilderMock);

    //
    $searchCriteriaInterfaceMock = $this->getMock('Magento\Framework\Api\SearchCriteriaInterface');

    $this->searchCriteriaBuilderMock = $this->getMockBuilder('Magento\Framework\Api\SearchCriteriaBuilder')
        ->setMethods(['create', 'addFilters'])
        ->disableOriginalConstructor()
        ->getMock();
    $this->searchCriteriaBuilderMock->method('addFilters')->willReturn($this->searchCriteriaBuilderMock);
    $this->searchCriteriaBuilderMock->method('create')->willReturn($searchCriteriaInterfaceMock);

    $this->groupRepositoryInterfaceMock = $this->getMock('Magento\Customer\Api\GroupRepositoryInterface');
    $this->groupRepositoryInterfaceMock->method('getList')->willReturn($this->groupSearchResultsInterfaceMock);


    $taxClassRepositoryInterfaceMock = $this->getMock('Magento\Tax\Api\TaxClassRepositoryInterface');
    //changing its get function in each loop iteration later
    $this->taxClassRepositoryInterfaceMock = $taxClassRepositoryInterfaceMock;

    //
    $loggerMock = $this->getMock('Psr\Log\LoggerInterface');

    $validatorMock = $this->getMockBuilder($this->className)
        ->setMethods(null)
        ->setConstructorArgs([
            'groupRepository' => $this->groupRepositoryInterfaceMock,
            'taxClassRepository' => $this->taxClassRepositoryInterfaceMock,
            'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
            'filterBuilder' => $this->filterBuilderMock,
            'logger' => $loggerMock
        ])->getMock();

    $this->validatorMock = $validatorMock;
}

0 个答案:

没有答案