Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing PHP Warning - count(): Parameter must be an array or an object… #8448

Merged
merged 3 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 4 additions & 12 deletions app/bundles/CoreBundle/Model/IteratorExportDataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

use Mautic\CoreBundle\Helper\DataExporterHelper;

/**
* Class IteratorExportDataModel.
*/
class IteratorExportDataModel implements \Iterator
{
private $position;
Expand All @@ -24,20 +21,15 @@ class IteratorExportDataModel implements \Iterator
private $callback;
private $total;
private $data;
private $page;
private $totalResult;

/**
* IteratorExportDataModel constructor.
*/
public function __construct(AbstractCommonModel $model, $args, callable $callback)
{
$this->model = $model;
$this->args = $args;
$this->callback = $callback;
$this->position = 0;
$this->total = 0;
$this->page = 1;
$this->totalResult = 0;
$this->data = 0;
}
Expand Down Expand Up @@ -68,8 +60,8 @@ public function next()
if ($this->position === $this->totalResult) {
$data = new DataExporterHelper();
$this->data = $data->getDataForExport($this->total, $this->model, $this->args, $this->callback);
$this->total = $this->total + count($this->data);
$this->totalResult = count($this->data);
$this->totalResult = $this->data ? count($this->data) : 0;
$this->total = $this->total + $this->totalResult;
$this->position = 0;
++$this->page;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove this line. Variable is not used and it's producing

PHP Notice - Undefined property: Mautic\CoreBundle\Model\IteratorExportDataModel::$page" at /Users/lukas.drahy/dev/community-fork/app/bundles/CoreBundle/Model/IteratorExportDataModel.php line 66

for me now.

}
Expand Down Expand Up @@ -118,8 +110,8 @@ public function rewind()
{
$data = new DataExporterHelper();
$this->data = $data->getDataForExport($this->total, $this->model, $this->args, $this->callback);
$this->total = $this->total + count($this->data);
$this->totalResult = count($this->data);
$this->totalResult = $this->data ? count($this->data) : 0;
$this->total = $this->total + $this->totalResult;
$this->position = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* @copyright 2020 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\CoreBundle\Tests\Unit\Validator;

use Mautic\CoreBundle\Entity\CommonRepository;
use Mautic\CoreBundle\Model\AbstractCommonModel;
use Mautic\CoreBundle\Model\IteratorExportDataModel;
use PHPUnit\Framework\MockObject\MockObject;

class IteratorExportDataModelTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject|AbstractCommonModel
*/
private $commonModel;

/**
* @var MockObject|CommonRepository
*/
private $commonRepository;

/**
* @var IteratorExportDataModel
*/
private $iteratorExportDataModel;

protected function setUp(): void
{
parent::setUp();

$this->commonModel = $this->createMock(AbstractCommonModel::class);
$this->commonRepository = $this->createMock(CommonRepository::class);
$args = ['limit' => 1000];
$callback = function ($var) {
return $var;
};

$this->iteratorExportDataModel = new IteratorExportDataModel($this->commonModel, $args, $callback);
}

public function testWorkflowWithItems(): void
{
$this->commonModel->expects($this->once())
->method('getEntities')
->with(['limit' => 1000, 'start' => 0])
->willReturn(['results' => [['a'], ['b']]]);

$this->commonModel->method('getRepository')->willReturn($this->commonRepository);

$this->assertSame(0, $this->iteratorExportDataModel->key());
$this->iteratorExportDataModel->rewind();
$this->iteratorExportDataModel->next();
$this->assertSame(1, $this->iteratorExportDataModel->key());
}

public function testWorkflowWithoutItems(): void
{
$this->commonModel->expects($this->once())
->method('getEntities')
->with(['limit' => 1000, 'start' => 0])
->willReturn(['results' => []]);

$this->commonModel->method('getRepository')->willReturn($this->commonRepository);

$this->assertSame(0, $this->iteratorExportDataModel->key());
$this->iteratorExportDataModel->rewind();
$this->iteratorExportDataModel->next();
$this->assertSame(1, $this->iteratorExportDataModel->key());
}
}