Skip to content

Commit

Permalink
Merge pull request #1 from niden/fixes/issue-14783
Browse files Browse the repository at this point in the history
[issue] - Adjustments to the test; trying to reproduce #14783
  • Loading branch information
ruudboon authored Feb 8, 2020
2 parents b95e183 + a1ac003 commit ace9f7a
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 17 deletions.
12 changes: 5 additions & 7 deletions tests/_data/fixtures/models/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
use Phalcon\Mvc\Model;

/**
* Class Invoices
* Class Customers
*
* @property int $inv_id
* @property int $inv_cst_id
* @property int $inv_status_flag
* @property string $inv_title
* @property float $inv_total
* @property string $inv_created_at
* @property int $cst_id;
* @property int $cst_status_flag;
* @property string $cst_name_last;
* @property string $cst_name_first;
*/
class Customers extends Model
{
Expand Down
48 changes: 48 additions & 0 deletions tests/_data/fixtures/models/CustomersKeepSnapshots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Models;

use Phalcon\Mvc\Model;

/**
* Class CustomersKeepSnapshots
*
* @property int $cst_id;
* @property int $cst_status_flag;
* @property string $cst_name_last;
* @property string $cst_name_first;
*/
class CustomersKeepSnapshots extends Model
{
public $cst_id;
public $cst_status_flag;
public $cst_name_last;
public $cst_name_first;

public function initialize()
{
$this->keepSnapshots(true);
$this->setSource('co_customers');

$this->belongsTo(
'cst_id',
__NAMESPACE__ . 'InvoicesKeepSnapshots',
'inv_cst_id',
[
'alias' => 'invoices',
'reusable' => true,
]
);
}
}
33 changes: 33 additions & 0 deletions tests/_data/fixtures/models/InvoicesKeepSnapshots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Models;

/**
* Class InvoicesMap
*
* @property int $inv_id
* @property int $inv_cst_id
* @property int $inv_status_flag
* @property string $inv_title
* @property float $inv_total
* @property string $inv_created_at
*/
class InvoicesKeepSnapshots extends Invoices
{
public function initialize()
{
$this->setSource('co_invoices');
$this->keepSnapshots(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @method static Simple|Customers[] find($parameters = null)
* @property Simple|Users $user
*/
class Customers extends Model
class Customers1 extends Model
{
public $id;
public $document_id;
Expand Down
64 changes: 55 additions & 9 deletions tests/database/Mvc/Model/QueryCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Phalcon\Test\Fixtures\Migrations\InvoicesMigration;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Fixtures\Traits\RecordsTrait;
use Phalcon\Test\Models\Customers;
use Phalcon\Test\Models\Invoices;
use Phalcon\Test\Models\CustomersKeepSnapshots;
use Phalcon\Test\Models\InvoicesKeepSnapshots;

use function uniqid;

/**
* Class QueryCest
Expand All @@ -35,8 +37,7 @@ public function _before(DatabaseTester $I)

/** @var PDO $connection */
$connection = $I->getConnection();
$migration = new InvoicesMigration($connection);
$migration->clear();
(new InvoicesMigration($connection));
}

/**
Expand All @@ -56,20 +57,65 @@ public function mvcModelQuery(DatabaseTester $I)
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
* @issue 14783
*/
public function mvcModelQueryIssue14783(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - query()');
$query = Invoices::query();

$query->columns([Invoices::class . '.*', 'join_1.*']);
$query->leftJoin(Customers::class, 'join_1.id = ' . Invoices::class . '.model2_id', 'join_1');
$this->addTestData($I);

$query = CustomersKeepSnapshots::query();
$query->columns(
[
CustomersKeepSnapshots::class . '.*',
'join_1.*'
]
);
$query->leftJoin(
InvoicesKeepSnapshots::class,
'join_1.inv_cst_id = ' . CustomersKeepSnapshots::class . '.cst_id',
'join_1'
);
$query->limit(20, 0);//I have 50 rows in my db
$resultsets = $query->execute();

foreach ($resultsets as $resultset) {
$model1 = $this->traitement($resultset);
$model = $this->transform($resultset);
$I->assertInstanceOf(InvoicesKeepSnapshots::class, $model);
$I->assertInstanceOf(CustomersKeepSnapshots::class, $model->customer);
}
}

/**
* Transforming method used for test
*
* @param $resultset
* @issue 14783
*
* @return mixed
*/
private function transform($resultset)
{
$invoice = $resultset->readAttribute(lcfirst(CustomersKeepSnapshots::class));
$customer = $resultset->readAttribute('join_1');
$invoice->customer = $customer;

return $invoice;
}

private function addTestData(DatabaseTester $I)
{
$connection = $I->getConnection();
$migration = new InvoicesMigration($connection);

for ($counter = 1; $counter <= 50; $counter++) {
$migration->insert(
$counter,
1,
1,
uniqid('inv-')
);
}
echo 'OK';
}
}

0 comments on commit ace9f7a

Please sign in to comment.