Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Jan Kuchař
grifart-enum
Commits
0c4bb6dd
Commit
0c4bb6dd
authored
Jan 03, 2019
by
Jan Kuchař
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
example order-state: renamed preparing -> processing
parent
4ffbcd20
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
37 deletions
+52
-37
tests/Example/OrderState/refactoring-1.phpt
tests/Example/OrderState/refactoring-1.phpt
+6
-6
tests/Example/OrderState/refactoring-2.phpt
tests/Example/OrderState/refactoring-2.phpt
+7
-8
tests/Example/OrderState/refactoring-3.phpt
tests/Example/OrderState/refactoring-3.phpt
+25
-7
tests/Example/OrderState/refactoring-4.phpt
tests/Example/OrderState/refactoring-4.phpt
+7
-7
tests/Example/OrderState/refactoring-5.phpt
tests/Example/OrderState/refactoring-5.phpt
+7
-9
No files found.
tests/Example/OrderState/refactoring-1.phpt
View file @
0c4bb6dd
...
...
@@ -19,7 +19,7 @@ class OrderService
{
public
const
STATE_RECEIVED
=
'received'
;
public
const
STATE_PR
EPAR
ING
=
'pr
epar
ing'
;
public
const
STATE_PR
OCESS
ING
=
'pr
ocess
ing'
;
public
const
STATE_FINISHED
=
'finished'
;
public
const
STATE_CANCELLED
=
'cancelled'
;
...
...
@@ -31,8 +31,8 @@ class OrderService
switch
(
$currentState
)
{
case
self
::
STATE_RECEIVED
:
return
$desiredState
===
self
::
STATE_PR
EPAR
ING
||
$desiredState
===
self
::
STATE_CANCELLED
;
case
self
::
STATE_PR
EPAR
ING
:
return
$desiredState
===
self
::
STATE_PR
OCESS
ING
||
$desiredState
===
self
::
STATE_CANCELLED
;
case
self
::
STATE_PR
OCESS
ING
:
return
$desiredState
===
self
::
STATE_FINISHED
;
case
self
::
STATE_FINISHED
:
return
FALSE
;
...
...
@@ -51,12 +51,12 @@ $orderService = new OrderService();
Assert
::
true
(
$orderService
->
canDoTransition
(
OrderService
::
STATE_RECEIVED
,
OrderService
::
STATE_PR
EPAR
ING
OrderService
::
STATE_PR
OCESS
ING
)
);
Assert
::
true
(
$orderService
->
canDoTransition
(
OrderService
::
STATE_PR
EPAR
ING
,
OrderService
::
STATE_PR
OCESS
ING
,
OrderService
::
STATE_FINISHED
)
);
...
...
@@ -91,7 +91,7 @@ Assert::false(
);
Assert
::
false
(
$orderService
->
canDoTransition
(
OrderService
::
STATE_PR
EPAR
ING
,
OrderService
::
STATE_PR
OCESS
ING
,
OrderService
::
STATE_CANCELLED
)
);
...
...
tests/Example/OrderState/refactoring-2.phpt
View file @
0c4bb6dd
...
...
@@ -15,7 +15,7 @@ final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PR
EPAR
ING()
* @method static OrderState PR
OCESS
ING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
...
...
@@ -26,14 +26,13 @@ final class OrderState extends Enum
protected
const
RECEIVED
=
'received'
,
PR
EPAR
ING
=
'pr
epar
ing'
,
PR
OCESS
ING
=
'pr
ocess
ing'
,
FINISHED
=
'finished'
,
CANCELLED
=
'cancelled'
;
}
class
OrderService
{
...
...
@@ -41,10 +40,10 @@ class OrderService
{
if
(
$currentState
===
OrderState
::
RECEIVED
())
{
return
$desiredState
===
OrderState
::
PR
EPAR
ING
()
||
$desiredState
===
OrderState
::
CANCELLED
();
return
$desiredState
===
OrderState
::
PR
OCESS
ING
()
||
$desiredState
===
OrderState
::
CANCELLED
();
}
if
(
$currentState
===
OrderState
::
PR
EPAR
ING
())
{
if
(
$currentState
===
OrderState
::
PR
OCESS
ING
())
{
return
$desiredState
===
OrderState
::
FINISHED
();
}
...
...
@@ -67,12 +66,12 @@ $orderService = new OrderService();
Assert
::
true
(
$orderService
->
canDoTransition
(
OrderState
::
RECEIVED
(),
OrderState
::
PR
EPAR
ING
()
OrderState
::
PR
OCESS
ING
()
)
);
Assert
::
true
(
$orderService
->
canDoTransition
(
OrderState
::
PR
EPAR
ING
(),
OrderState
::
PR
OCESS
ING
(),
OrderState
::
FINISHED
()
)
);
...
...
@@ -107,7 +106,7 @@ Assert::false(
);
Assert
::
false
(
$orderService
->
canDoTransition
(
OrderState
::
PR
EPAR
ING
(),
OrderState
::
PR
OCESS
ING
(),
OrderState
::
CANCELLED
()
)
);
...
...
tests/Example/OrderState/refactoring-3.phpt
View file @
0c4bb6dd
...
...
@@ -16,7 +16,7 @@ final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PR
EPAR
ING()
* @method static OrderState PR
OCESS
ING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
...
...
@@ -27,7 +27,7 @@ final class OrderState extends Enum
protected
const
RECEIVED
=
'received'
,
PR
EPAR
ING
=
'pr
epar
ing'
,
PR
OCESS
ING
=
'pr
ocess
ing'
,
FINISHED
=
'finished'
,
CANCELLED
=
'cancelled'
;
...
...
@@ -36,10 +36,10 @@ final class OrderState extends Enum
public
function
canDoTransition
(
OrderState
$desiredState
):
bool
{
if
(
$this
===
self
::
RECEIVED
())
{
return
$desiredState
===
self
::
PR
EPAR
ING
()
||
$desiredState
===
self
::
CANCELLED
();
return
$desiredState
===
self
::
PR
OCESS
ING
()
||
$desiredState
===
self
::
CANCELLED
();
}
if
(
$this
===
self
::
PR
EPAR
ING
())
{
if
(
$this
===
self
::
PR
OCESS
ING
())
{
return
$desiredState
===
self
::
FINISHED
();
}
...
...
@@ -59,11 +59,11 @@ final class OrderState extends Enum
// Standard order flow:
Assert
::
true
(
OrderState
::
RECEIVED
()
->
canDoTransition
(
OrderState
::
PR
EPAR
ING
()
OrderState
::
PR
OCESS
ING
()
)
);
Assert
::
true
(
OrderState
::
PR
EPAR
ING
()
->
canDoTransition
(
OrderState
::
PR
OCESS
ING
()
->
canDoTransition
(
OrderState
::
FINISHED
()
)
);
...
...
@@ -94,7 +94,7 @@ Assert::false(
)
);
Assert
::
false
(
OrderState
::
PR
EPAR
ING
()
->
canDoTransition
(
OrderState
::
PR
OCESS
ING
()
->
canDoTransition
(
OrderState
::
CANCELLED
()
)
);
...
...
@@ -104,3 +104,21 @@ Assert::false(
)
);
$state1
=
OrderState
::
RECEIVED
();
$state2
=
OrderState
::
RECEIVED
();
Assert
::
true
(
$state1
===
$state2
);
$state3
=
OrderState
::
PROCESSING
();
Assert
::
true
(
$state1
!==
$state3
);
Assert
::
true
(
$state2
!==
$state3
);
tests/Example/OrderState/refactoring-4.phpt
View file @
0c4bb6dd
...
...
@@ -16,7 +16,7 @@ final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PR
EPAR
ING()
* @method static OrderState PR
OCESS
ING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
...
...
@@ -25,7 +25,7 @@ final class OrderState extends Enum
protected
const
RECEIVED
=
'received'
,
PR
EPAR
ING
=
'pr
epar
ing'
,
PR
OCESS
ING
=
'pr
ocess
ing'
,
FINISHED
=
'finished'
,
CANCELLED
=
'cancelled'
;
// domain logic: can be cancelled before preparation is started
...
...
@@ -57,8 +57,8 @@ final class OrderState extends Enum
// again and you will get infinite loop.
return
[
self
::
RECEIVED
=>
new
self
([
self
::
PR
EPAR
ING
,
self
::
CANCELLED
]),
self
::
PR
EPAR
ING
=>
new
self
([
self
::
FINISHED
]),
self
::
RECEIVED
=>
new
self
([
self
::
PR
OCESS
ING
,
self
::
CANCELLED
]),
self
::
PR
OCESS
ING
=>
new
self
([
self
::
FINISHED
]),
self
::
FINISHED
=>
new
self
([]),
self
::
CANCELLED
=>
new
self
([]),
];
...
...
@@ -70,11 +70,11 @@ final class OrderState extends Enum
// Standard order flow:
Assert
::
true
(
OrderState
::
RECEIVED
()
->
canDoTransition
(
OrderState
::
PR
EPAR
ING
()
OrderState
::
PR
OCESS
ING
()
)
);
Assert
::
true
(
OrderState
::
PR
EPAR
ING
()
->
canDoTransition
(
OrderState
::
PR
OCESS
ING
()
->
canDoTransition
(
OrderState
::
FINISHED
()
)
);
...
...
@@ -104,7 +104,7 @@ Assert::false(
)
);
Assert
::
false
(
OrderState
::
PR
EPAR
ING
()
->
canDoTransition
(
OrderState
::
PR
OCESS
ING
()
->
canDoTransition
(
OrderState
::
CANCELLED
()
)
);
...
...
tests/Example/OrderState/refactoring-5.phpt
View file @
0c4bb6dd
...
...
@@ -44,7 +44,7 @@ class Order
/**
* @method static OrderState RECEIVED()
* @method static OrderState PR
EPAR
ING()
* @method static OrderState PR
OCESS
ING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
...
...
@@ -52,7 +52,7 @@ abstract class OrderState extends Enum {
protected
const
RECEIVED
=
'received'
,
PR
EPAR
ING
=
'pr
epar
ing'
,
PR
OCESS
ING
=
'pr
ocess
ing'
,
FINISHED
=
'finished'
,
CANCELLED
=
'cancelled'
;
// domain logic: can be cancelled before preparation is started
...
...
@@ -83,19 +83,17 @@ abstract class OrderState extends Enum {
public
function
canDoTransition
(
OrderState
$nextState
):
bool
{
return
$nextState
===
$this
::
PR
EPAR
ING
()
||
$nextState
===
$this
::
CANCELLED
();
return
$nextState
===
$this
::
PR
OCESS
ING
()
||
$nextState
===
$this
::
CANCELLED
();
}
},
self
::
PREPARING
=>
new
class
extends
OrderState
{
self
::
PROCESSING
=>
new
class
extends
OrderState
{
public
function
canDoTransition
(
OrderState
$nextState
):
bool
{
return
$nextState
===
$this
::
FINISHED
();
}
},
...
...
@@ -134,7 +132,7 @@ abstract class OrderState extends Enum {
(
function
()
{
$order
=
new
Order
();
Assert
::
same
(
'employee'
,
$order
->
getEmployee
());
$order
->
changeState
(
OrderState
::
PR
EPAR
ING
());
$order
->
changeState
(
OrderState
::
PR
OCESS
ING
());
Assert
::
same
(
'employee'
,
$order
->
getEmployee
());
$order
->
changeState
(
OrderState
::
FINISHED
());
Assert
::
null
(
$order
->
getEmployee
());
...
...
@@ -159,13 +157,13 @@ Assert::exception(function () {
Assert
::
exception
(
function
()
{
$order
=
new
Order
();
$order
->
changeState
(
OrderState
::
PR
EPAR
ING
());
$order
->
changeState
(
OrderState
::
PR
OCESS
ING
());
$order
->
changeState
(
OrderState
::
CANCELLED
());
// not allowed
},
InvalidTransitionException
::
class
);
Assert
::
exception
(
function
()
{
$order
=
new
Order
();
$order
->
changeState
(
OrderState
::
PR
EPAR
ING
());
$order
->
changeState
(
OrderState
::
PR
OCESS
ING
());
$order
->
changeState
(
OrderState
::
FINISHED
());
$order
->
changeState
(
OrderState
::
CANCELLED
());
// not allowed
...
...
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