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
e4978c22
Unverified
Commit
e4978c22
authored
Apr 23, 2019
by
Jonathan H. Wage
Committed by
GitHub
Apr 23, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3530 from doctrine/fix-readonly-sqlite-exception-test
Improve ExceptionTest::testConnectionExceptionSqLite
parents
945b2710
b244b9e6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
19 deletions
+42
-19
ExceptionTest.php
tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
+42
-19
No files found.
tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
View file @
e4978c22
...
...
@@ -9,10 +9,13 @@ use Doctrine\DBAL\Schema\Schema;
use
Doctrine\DBAL\Schema\Table
;
use
Doctrine\Tests\DbalFunctionalTestCase
;
use
Throwable
;
use
const
PHP_OS
;
use
function
array_merge
;
use
function
chmod
;
use
function
defined
;
use
function
exec
;
use
function
file_exists
;
use
function
posix_geteuid
;
use
function
posix_getpwuid
;
use
function
sprintf
;
use
function
sys_get_temp_dir
;
use
function
touch
;
...
...
@@ -284,25 +287,28 @@ class ExceptionTest extends DbalFunctionalTestCase
$this
->
connection
->
executeQuery
(
$sql
);
}
/**
* @dataProvider getSqLiteOpenConnection
*/
public
function
testConnectionExceptionSqLite
(
$mode
,
$exceptionClass
)
public
function
testConnectionExceptionSqLite
()
{
if
(
$this
->
connection
->
getDatabasePlatform
()
->
getName
()
!==
'sqlite'
)
{
$this
->
markTestSkipped
(
'Only fails this way on sqlite'
);
}
// mode 0 is considered read-only on Windows
$mode
=
PHP_OS
===
'Linux'
?
0444
:
0000
;
$filename
=
sprintf
(
'%s/%s'
,
sys_get_temp_dir
(),
'doctrine_failed_connection_'
.
$mode
.
'.db'
);
if
(
file_exists
(
$filename
))
{
chmod
(
$filename
,
0200
);
// make the file writable again, so it can be removed on Windows
unlink
(
$filename
);
$this
->
cleanupReadOnlyFile
(
$filename
);
}
touch
(
$filename
);
chmod
(
$filename
,
$mode
);
if
(
$this
->
isLinuxRoot
())
{
exec
(
sprintf
(
'chattr +i %s'
,
$filename
));
}
$params
=
[
'driver'
=>
'pdo_sqlite'
,
'path'
=>
$filename
,
...
...
@@ -313,19 +319,21 @@ class ExceptionTest extends DbalFunctionalTestCase
$table
=
$schema
->
createTable
(
'no_connection'
);
$table
->
addColumn
(
'id'
,
'integer'
);
$this
->
expectException
(
$exceptionClass
);
$this
->
expectException
(
Exception\ReadOnlyException
::
class
);
$this
->
expectExceptionMessage
(
<<<EOT
An exception occurred while executing 'CREATE TABLE no_connection (id INTEGER NOT NULL)':
SQLSTATE[HY000]: General error: 8 attempt to write a readonly database
EOT
);
try
{
foreach
(
$schema
->
toSql
(
$conn
->
getDatabasePlatform
())
as
$sql
)
{
$conn
->
exec
(
$sql
);
}
}
finally
{
$this
->
cleanupReadOnlyFile
(
$filename
);
}
public
function
getSqLiteOpenConnection
()
{
return
[
// mode 0 is considered read-only on Windows
[
0000
,
defined
(
'PHP_WINDOWS_VERSION_BUILD'
)
?
Exception\ReadOnlyException
::
class
:
Exception\ConnectionException
::
class
],
[
0444
,
Exception\ReadOnlyException
::
class
],
];
}
/**
...
...
@@ -395,4 +403,19 @@ class ExceptionTest extends DbalFunctionalTestCase
$schemaManager
->
dropTable
(
'owning_table'
);
$schemaManager
->
dropTable
(
'constraint_error_table'
);
}
private
function
isLinuxRoot
()
:
bool
{
return
PHP_OS
===
'Linux'
&&
posix_getpwuid
(
posix_geteuid
())[
'name'
]
===
'root'
;
}
private
function
cleanupReadOnlyFile
(
string
$filename
)
:
void
{
if
(
$this
->
isLinuxRoot
())
{
exec
(
sprintf
(
'chattr -i %s'
,
$filename
));
}
chmod
(
$filename
,
0200
);
// make the file writable again, so it can be removed on Windows
unlink
(
$filename
);
}
}
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