Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
385cbb7d
Commit
385cbb7d
authored
Jul 25, 2017
by
Ian Jenkins
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a spy to check fetch is called appropriate number of times
parent
7cd962e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
26 deletions
+62
-26
DataAccessTest.php
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
+62
-26
No files found.
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
View file @
385cbb7d
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
Doctrine\Tests\DBAL\Functional
;
namespace
Doctrine\Tests\DBAL\Functional
;
use
Doctrine\DBAL\Connection
;
use
Doctrine\DBAL\Connection
;
use
Doctrine\DBAL\Driver\Statement
;
use
Doctrine\DBAL\Platforms\AbstractPlatform
;
use
Doctrine\DBAL\Platforms\AbstractPlatform
;
use
Doctrine\DBAL\Types\Type
;
use
Doctrine\DBAL\Types\Type
;
use
PDO
;
use
PDO
;
...
@@ -867,47 +868,82 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
...
@@ -867,47 +868,82 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
}
}
}
}
public
function
testIteratorCa
nBeIteratedMultiple
Times
()
public
function
testIteratorCa
llsFetchTheExpectedNumberOf
Times
()
{
{
$skippedMessage
=
<<<'MSG'
$sql
=
'SELECT test_int, test_string FROM fetch_table'
;
Iterators are not rewindable for either PDO or Non PDO drivers. This is because PDO by default is not rewindable and
$originalStmt
=
$this
->
_conn
->
prepare
(
$sql
);
to be consistent we have not implemented rewindable iterators for non PDO drivers. Should PDO ever become rewindable by
default we should wrap the legacy drivers in Doctrine\DBAL\RewindableGenerator for example on the Mysqli driver you
would do something like the following:
return new RewindableGenerator(function() {
// Mock the connection
$this->_stmt->data_seek(0);
$connectionMock
=
$this
->
getMockBuilder
(
'Doctrine\\DBAL\\Driver\\Connection'
)
while ($row = $this->fetch()) {
->
setMethods
([
'prepare'
,
'query'
])
yield $row;
->
getMockForAbstractClass
();
}
});
If this has been done you can include this test.
$stmtSpy
=
new
StatementSpy
(
$originalStmt
);
MSG;
$this
->
markTestSkipped
(
$skippedMessage
);
// Return our mocked statement from the query
$connectionMock
->
expects
(
$this
->
any
())
->
method
(
'prepare'
)
->
will
(
$this
->
returnValue
(
$stmtSpy
));
$sql
=
'SELECT CURTIME(6) AS time_started, test_int, test_string FROM fetch_table'
;
$connectionMock
->
expects
(
$this
->
any
())
$stmt
=
$this
->
_conn
->
query
(
$sql
);
->
method
(
'query'
)
->
will
(
$this
->
returnCallback
(
function
()
use
(
$stmtSpy
)
{
$stmtSpy
->
execute
();
return
$stmtSpy
;
}));
$stmt
=
$connectionMock
->
query
(
$sql
);
$stmt
->
setFetchMode
(
\PDO
::
FETCH_ASSOC
);
$stmt
->
setFetchMode
(
\PDO
::
FETCH_ASSOC
);
$timeStartedMsFirstLoop
=
null
;
// Assert no fetch calls thus far
$timeStartedMsSecondLoop
=
null
;
self
::
assertEquals
(
0
,
$stmtSpy
->
getFetchCalls
())
;
$i
=
0
;
$i
=
0
;
foreach
(
$stmt
as
$row
)
{
foreach
(
$stmt
as
$row
)
{
$timeStartedMsFirstLoop
=
$row
[
'time_started'
];
$i
++
;
$i
++
;
}
}
$j
=
0
;
// Assert expected number of fetches
foreach
(
$stmt
as
$row2
)
{
$this
->
assertGreaterThan
(
0
,
$stmtSpy
->
getFetchCalls
());
$timeStartedMsSecondLoop
=
$row2
[
'time_started'
];
$this
->
assertEquals
(
$i
,
$stmtSpy
->
getFetchCalls
());
$j
++
;
}
}
class
StatementSpy
implements
\IteratorAggregate
{
/**
* @var Statement|\Doctrine\DBAL\Statement
*/
private
$stmt
;
private
$fetchCalls
=
0
;
public
function
__construct
(
Statement
$stmt
)
{
$this
->
stmt
=
$stmt
;
}
public
function
setFetchMode
(
$fetchMode
,
$arg2
=
null
,
$arg3
=
null
)
{
return
$this
->
stmt
->
setFetchMode
(
$fetchMode
,
$arg2
,
$arg3
);
}
public
function
execute
(
$params
=
null
)
{
return
$this
->
stmt
->
execute
(
$params
);
}
public
function
getIterator
()
{
foreach
(
$this
->
stmt
as
$item
)
{
$this
->
fetchCalls
++
;
yield
$item
;
}
}
}
$this
->
assertEquals
(
$i
,
$j
);
public
function
getFetchCalls
()
$this
->
assertEquals
(
$timeStartedMsFirstLoop
,
$timeStartedMsSecondLoop
);
{
return
$this
->
fetchCalls
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment