Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pasteque
pasteque-server
Commits
1a9aef25
Commit
1a9aef25
authored
May 31, 2018
by
Karamel
Browse files
Fixes to some product routes, add some tests.
parent
96cd8a41
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/http/routes/product.php
View file @
1a9aef25
...
...
@@ -121,7 +121,7 @@ $app->PUT('/api/product/{reference}', function($request, $response, $args) {
$ptApp
=
$this
->
get
(
'settings'
)[
'ptApp'
];
$tab
=
$request
->
getParsedBody
();
if
(
!
empty
(
$tab
[
'id'
]))
{
return
$response
->
with
Response
(
400
,
'New record cannot have an Id'
);
return
$response
->
with
Status
(
400
,
'New record cannot have an Id'
);
}
$tab
[
'reference'
]
=
$args
[
'reference'
];
$product
=
\
Pasteque\Server\Model\Product
::
fromStruct
(
$tab
,
$ptApp
->
getDao
());
...
...
@@ -129,10 +129,10 @@ $app->PUT('/api/product/{reference}', function($request, $response, $args) {
$existingPrdReq
=
APICaller
::
run
(
$ptApp
,
'product'
,
'getByReference'
,
$product
->
getReference
());
if
(
$existingPrdReq
->
getStatus
()
!=
APIResult
::
STATUS_CALL_OK
)
{
return
$response
->
with
Response
(
$existingPrdReq
);
return
$response
->
with
APIResult
(
$existingPrdReq
);
}
if
(
$existingPrdReq
->
getContent
()
!=
null
)
{
return
$response
->
with
Response
(
400
,
'Reference is already taken'
);
return
$response
->
with
Status
(
400
,
'Reference is already taken'
);
}
return
$response
->
withApiResult
(
APICaller
::
run
(
$ptApp
,
'product'
,
'write'
,
$product
));
...
...
@@ -144,7 +144,7 @@ $app->PATCH('/api/product/{reference}', function($request, $response, $args) {
$ptApp
=
$this
->
get
(
'settings'
)[
'ptApp'
];
$tab
=
$request
->
getParsedBody
();
if
(
!
empty
(
$tab
[
'id'
]))
{
return
$response
->
with
Response
(
400
,
'Do not send Id, use reference instead.'
);
return
$response
->
with
Status
(
400
,
'Do not send Id, use reference instead.'
);
}
$tab
[
'reference'
]
=
$args
[
'reference'
];
$product
=
\
Pasteque\Server\Model\Product
::
fromStruct
(
$tab
,
$ptApp
->
getDao
());
...
...
@@ -152,13 +152,13 @@ $app->PATCH('/api/product/{reference}', function($request, $response, $args) {
$existingPrdReq
=
APICaller
::
run
(
$ptApp
,
'product'
,
'getByReference'
,
$product
->
getReference
());
if
(
$existingPrdReq
->
getStatus
()
!=
APIResult
::
STATUS_CALL_OK
)
{
return
$response
->
with
Response
(
$existingPrdReq
);
return
$response
->
with
APIResult
(
$existingPrdReq
);
}
if
(
$existingPrdReq
->
getContent
()
==
null
)
{
return
$response
->
with
Response
(
404
,
'No product found.'
);
return
$response
->
with
Status
(
404
,
'No product found.'
);
}
if
(
$existingPrdReq
->
getContent
()
->
getId
()
!=
$product
->
getId
())
{
return
$response
->
with
Response
(
500
,
'Id mismatch.'
);
return
$response
->
with
Status
(
500
,
'Id mismatch.'
);
}
return
$response
->
withApiResult
(
APICaller
::
run
(
$ptApp
,
'product'
,
'write'
,
$product
));
});
tests/http/HttpProductTest.php
0 → 100644
View file @
1a9aef25
<?php
// Pasteque server testing
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// This file is part of Pasteque.
//
// Pasteque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pasteque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace
Pasteque\Server
;
use
\
Pasteque\Server\Model\Category
;
use
\
Pasteque\Server\Model\Product
;
use
\
Pasteque\Server\Model\Tax
;
use
\
Pasteque\Server\System\Login
;
use
\
Pasteque\Server\System\DAO\DAOCondition
;
use
\
Pasteque\Server\System\DAO\DAOFactory
;
use
\
PHPUnit\Framework\TestCase
;
require_once
(
dirname
(
dirname
(
__FILE__
))
.
"/common_load.php"
);
class
HttpProductTest
extends
TestCase
{
private
$curl
;
private
static
$token
;
private
$dao
;
private
$cat
;
private
$tax
;
public
static
function
setUpBeforeClass
()
{
static
::
$token
=
obtainToken
();
}
public
static
function
tearDownAfterClass
()
{
}
protected
function
setUp
()
{
$this
->
curl
=
curl_init
();
curl_setopt
(
$this
->
curl
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$this
->
curl
,
CURLOPT_HTTPHEADER
,
[
Login
::
TOKEN_HEADER
.
': '
.
static
::
$token
]);
global
$dbInfo
;
$this
->
dao
=
DAOFactory
::
getDAO
(
$dbInfo
,
[
'debug'
=>
true
]);
$this
->
cat
=
new
Category
();
$this
->
cat
->
setReference
(
'category'
);
$this
->
cat
->
setLabel
(
'Category'
);
$this
->
dao
->
write
(
$this
->
cat
);
$this
->
tax
=
new
Tax
();
$this
->
tax
->
setLabel
(
'VAT'
);
$this
->
tax
->
setRate
(
0.1
);
$this
->
dao
->
write
(
$this
->
tax
);
$this
->
dao
->
commit
();
}
protected
function
tearDown
()
{
curl_close
(
$this
->
curl
);
foreach
([
Product
::
class
,
Tax
::
class
,
Category
::
class
]
as
$class
)
{
$all
=
$this
->
dao
->
search
(
$class
);
foreach
(
$all
as
$record
)
{
$this
->
dao
->
delete
(
$record
);
}
}
$this
->
dao
->
commit
();
$this
->
dao
->
close
();
}
public
function
testPutOk
()
{
$prd
=
new
Product
();
$prd
->
setCategory
(
$this
->
cat
);
$prd
->
setTax
(
$this
->
tax
);
$prd
->
setReference
(
'ref'
);
$prd
->
setLabel
(
'label'
);
$prd
->
setPriceSell
(
10
);
curl_setopt
(
$this
->
curl
,
CURLOPT_CUSTOMREQUEST
,
'PUT'
);
curl_setopt
(
$this
->
curl
,
CURLOPT_URL
,
apiUrl
(
sprintf
(
'api/product/%s'
,
urlencode
(
$prd
->
getReference
()))));
curl_setopt
(
$this
->
curl
,
CURLOPT_POST
,
true
);
curl_setopt
(
$this
->
curl
,
CURLOPT_POSTFIELDS
,
json_encode
(
$prd
->
toStruct
()));
curl_setopt
(
$this
->
curl
,
CURLOPT_HTTPHEADER
,
[
Login
::
TOKEN_HEADER
.
': '
.
static
::
$token
,
'Content-Type: application/json'
]);
$resp
=
curl_exec
(
$this
->
curl
);
$this
->
assertEquals
(
200
,
curl_getinfo
(
$this
->
curl
,
CURLINFO_HTTP_CODE
));
$dbPrd
=
$this
->
dao
->
search
(
Product
::
class
,
new
DAOCondition
(
'reference'
,
'='
,
$prd
->
getReference
()));
$this
->
assertEquals
(
1
,
count
(
$dbPrd
));
}
public
function
testPutId
()
{
$prd
=
new
Product
();
$prd
->
setCategory
(
$this
->
cat
);
$prd
->
setTax
(
$this
->
tax
);
$prd
->
setReference
(
'ref'
);
$prd
->
setLabel
(
'label'
);
$prd
->
setPriceSell
(
10
);
$json
=
$prd
->
toStruct
();
$json
[
'id'
]
=
1
;
curl_setopt
(
$this
->
curl
,
CURLOPT_CUSTOMREQUEST
,
'PUT'
);
curl_setopt
(
$this
->
curl
,
CURLOPT_URL
,
apiUrl
(
sprintf
(
'api/product/%s'
,
urlencode
(
$prd
->
getReference
()))));
curl_setopt
(
$this
->
curl
,
CURLOPT_POST
,
true
);
curl_setopt
(
$this
->
curl
,
CURLOPT_POSTFIELDS
,
json_encode
(
$json
));
curl_setopt
(
$this
->
curl
,
CURLOPT_HTTPHEADER
,
[
Login
::
TOKEN_HEADER
.
': '
.
static
::
$token
,
'Content-Type: application/json'
]);
$resp
=
curl_exec
(
$this
->
curl
);
$this
->
assertEquals
(
400
,
curl_getinfo
(
$this
->
curl
,
CURLINFO_HTTP_CODE
));
$this
->
markTestIncomplete
(
'Test response message, not available easily with curl'
);
}
public
function
testPutRefMismatch
()
{
$prd
=
new
Product
();
$prd
->
setCategory
(
$this
->
cat
);
$prd
->
setTax
(
$this
->
tax
);
$prd
->
setReference
(
'ref'
);
$prd
->
setLabel
(
'label'
);
$prd
->
setPriceSell
(
10
);
curl_setopt
(
$this
->
curl
,
CURLOPT_CUSTOMREQUEST
,
'PUT'
);
curl_setopt
(
$this
->
curl
,
CURLOPT_URL
,
apiUrl
(
'api/product/notRef'
));
curl_setopt
(
$this
->
curl
,
CURLOPT_POST
,
true
);
curl_setopt
(
$this
->
curl
,
CURLOPT_POSTFIELDS
,
json_encode
(
$prd
->
toStruct
()));
curl_setopt
(
$this
->
curl
,
CURLOPT_HTTPHEADER
,
[
Login
::
TOKEN_HEADER
.
': '
.
static
::
$token
,
'Content-Type: application/json'
]);
$resp
=
curl_exec
(
$this
->
curl
);
$this
->
assertEquals
(
200
,
curl_getinfo
(
$this
->
curl
,
CURLINFO_HTTP_CODE
));
$dbPrd
=
$this
->
dao
->
search
(
Product
::
class
,
new
DAOCondition
(
'reference'
,
'='
,
'notRef'
));
$this
->
assertEquals
(
1
,
count
(
$dbPrd
));
}
public
function
testPutRefExisting
()
{
$prd
=
new
Product
();
$prd
->
setCategory
(
$this
->
cat
);
$prd
->
setTax
(
$this
->
tax
);
$prd
->
setReference
(
'ref'
);
$prd
->
setLabel
(
'label'
);
$prd
->
setPriceSell
(
10
);
$this
->
dao
->
write
(
$prd
);
$this
->
dao
->
commit
();
$prd2
=
new
Product
();
$prd2
->
setCategory
(
$this
->
cat
);
$prd2
->
setTax
(
$this
->
tax
);
$prd2
->
setReference
(
'ref'
);
$prd2
->
setLabel
(
'other label'
);
$prd2
->
setPriceSell
(
10
);
curl_setopt
(
$this
->
curl
,
CURLOPT_CUSTOMREQUEST
,
'PUT'
);
curl_setopt
(
$this
->
curl
,
CURLOPT_URL
,
apiUrl
(
sprintf
(
'api/product/%s'
,
urlencode
(
$prd2
->
getReference
()))));
curl_setopt
(
$this
->
curl
,
CURLOPT_POST
,
true
);
curl_setopt
(
$this
->
curl
,
CURLOPT_POSTFIELDS
,
json_encode
(
$prd2
->
toStruct
()));
curl_setopt
(
$this
->
curl
,
CURLOPT_HTTPHEADER
,
[
Login
::
TOKEN_HEADER
.
': '
.
static
::
$token
,
'Content-Type: application/json'
]);
$resp
=
curl_exec
(
$this
->
curl
);
$this
->
assertEquals
(
400
,
curl_getinfo
(
$this
->
curl
,
CURLINFO_HTTP_CODE
));
$this
->
markTestIncomplete
(
'Test response message, not available easily with curl'
);
}
}
tests/testsuites.xml
View file @
1a9aef25
...
...
@@ -37,6 +37,7 @@
<file>
http/HttpCustomerTest.php
</file>
<file>
http/HttpImageTest.php
</file>
<file>
http/HttpCategoryTest.php
</file>
<file>
http/HttpProductTest.php
</file>
</testsuite>
</testsuites>
</phpunit>
Write
Preview
Supports
Markdown
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