Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tables
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
grifart libs
tables
Commits
5c2df26f
Verified
Commit
5c2df26f
authored
2 years ago
by
Daniel Kurowski
Committed by
Jiří Pudil
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add failing test for composite array
parent
70b1838d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!40
CompositeType: add explicit type cast to support composite values in WHERE clause
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/Types/ArrayTypeTest.phpt
+31
-0
31 additions, 0 deletions
tests/Types/ArrayTypeTest.phpt
with
31 additions
and
0 deletions
tests/Types/ArrayTypeTest.phpt
+
31
−
0
View file @
5c2df26f
...
...
@@ -4,10 +4,17 @@ declare(strict_types=1);
namespace
Grifart\Tables\Tests\Types
;
use
Dibi\Literal
;
use
Grifart\ClassScaffolder\Definition\Types\Type
as
PhpType
;
use
Grifart\Tables\NamedIdentifier
;
use
Grifart\Tables\Types\ArrayType
;
use
Grifart\Tables\Types\CompositeType
;
use
Grifart\Tables\Types\IntType
;
use
Grifart\Tables\Types\TextType
;
use
Tester\Assert
;
use
function
Functional\map
;
use
function
Grifart\ClassScaffolder\Definition\Types\resolve
;
require
__DIR__
.
'/../bootstrap.php'
;
...
...
@@ -22,3 +29,27 @@ Assert::same([[4, 8], [15, 16], [23, 42]], $nestedArrayType->fromDatabase('{{4,8
$textArrayType
=
ArrayType
::
of
(
new
TextType
());
Assert
::
same
(
'{simple,NULL,"","co,m\\\\ple\\"x"}'
,
$textArrayType
->
toDatabase
([
'simple'
,
null
,
''
,
'co,m\\ple"x'
]));
Assert
::
same
([
'simple'
,
null
,
''
,
'co,m\\ple"x'
],
$textArrayType
->
fromDatabase
(
'{simple,NULL,"","co,m\\\\ple\\"x"}'
));
final
class
Version
{
public
function
__construct
(
public
int
$major
,
public
int
$minor
,
public
int
$patch
)
{}
/** @return array{int,int,int} */
public
function
toArray
():
array
{
return
[
$this
->
major
,
$this
->
minor
,
$this
->
patch
];
}
}
/** @extends CompositeType<Version> */
final
class
VersionType
extends
CompositeType
{
public
function
__construct
()
{
parent
::
__construct
(
new
NamedIdentifier
(
'public'
,
'packageVersion'
),
new
IntType
(),
new
IntType
(),
new
IntType
()
);
}
public
function
getPhpType
():
PhpType
{
return
resolve
(
Version
::
class
);
}
public
function
toDatabase
(
mixed
$value
):
Literal
{
return
$this
->
tupleToDatabase
(
$value
->
toArray
());
}
public
function
fromDatabase
(
mixed
$value
):
Version
{
return
new
Version
(
...
$this
->
tupleFromDatabase
(
$value
));
}
};
$compositeArrayType
=
ArrayType
::
of
(
new
VersionType
());
Assert
::
same
(
'{(0,1,0),(1,0,0),(1,0,1)}::public."packageVersion"[]'
,
$compositeArrayType
->
toDatabase
([
new
Version
(
0
,
1
,
0
),
new
Version
(
1
,
0
,
0
),
new
Version
(
1
,
0
,
1
)]));
/** @var callable(Version[] $versions):array<array{int,int,int}> $toTuples */
// scalar tuples are comparable with ===
$toTuples
=
static
fn
(
array
$versions
):
array
=>
map
(
$versions
,
static
fn
(
$version
)
=>
$version
->
toArray
());
Assert
::
same
(
$toTuples
([
new
Version
(
0
,
1
,
0
),
new
Version
(
1
,
0
,
0
),
new
Version
(
1
,
0
,
1
)]),
$toTuples
(
$compositeArrayType
->
fromDatabase
(
'{(0,1,0),(1,0,0),(1,0,1)}'
)),
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment