Skip to content

Commit 00c73d2

Browse files
committed
add tests
1 parent cfee1a5 commit 00c73d2

File tree

7 files changed

+192
-11
lines changed

7 files changed

+192
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ credentials.*
1010
.php_cs.cache
1111
.vscode/
1212
.kokoro/secrets.sh
13+
.phpunit.result.cache

cloud_sql/mysql/pdo/composer.json

+9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
"Google\\Cloud\\Samples\\CloudSQL\\MySQL\\": "src"
66
}
77
},
8+
"autoload-dev": {
9+
"psr-4": {
10+
"Google\\Cloud\\Samples\\CloudSQL\\MySQL\\Tests\\": "src"
11+
}
12+
},
813
"require": {
14+
"php": ">= 7.2",
915
"slim/slim": "^4.5",
1016
"slim/twig-view": "^3.1",
1117
"pimple/pimple": "^3.3",
1218
"guzzlehttp/psr7": "^1.6",
1319
"http-interop/http-factory-guzzle": "^1.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^8.5"
1423
}
1524
}

cloud_sql/mysql/pdo/index.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
$app = include __DIR__ . '/src/app.php';
2525

2626
$app->get('/', function ($request, $response) {
27+
$this->get('votes')->createTableIfNotExists();
28+
2729
return $this->get('view')->render($response, 'template.twig', [
2830
'votes' => $this->get('votes')->listVotes(),
2931
'tabCount' => $this->get('votes')->getCountByValue('TABS'),
@@ -32,6 +34,8 @@
3234
});
3335

3436
$app->post('/', function ($request, $response) {
37+
$this->get('votes')->createTableIfNotExists();
38+
3539
$message = 'Invalid vote. Choose Between TABS and SPACES';
3640

3741
$formData = $request->getParsedBody() + [

cloud_sql/mysql/pdo/phpunit.xml.dist

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true">
3+
<testsuites>
4+
<testsuite name="CloudSQLMySQLSample">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory suffix=".php">src</directory>
11+
</whitelist>
12+
</filter>
13+
</phpunit>

cloud_sql/mysql/pdo/src/Votes.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,28 @@ class Votes
3939
public function __construct(PDO $connection)
4040
{
4141
$this->connection = $connection;
42-
$this->createTable();
4342
}
4443

4544
/**
4645
* Creates the table if it does not yet exist.
4746
*
4847
* @return void
4948
*/
50-
private function createTable()
49+
public function createTableIfNotExists()
5150
{
52-
$sql = "CREATE TABLE IF NOT EXISTS votes (
53-
vote_id INT NOT NULL AUTO_INCREMENT,
54-
time_cast DATETIME NOT NULL,
55-
vote_value VARCHAR(6) NOT NULL,
56-
PRIMARY KEY (vote_id)
57-
);";
58-
59-
$this->connection->exec($sql);
51+
try {
52+
$stmt = $this->connection->prepare('SELECT 1 FROM votes');
53+
$stmt->execute();
54+
} catch (PDOException $e) {
55+
$sql = "CREATE TABLE votes (
56+
vote_id INT NOT NULL AUTO_INCREMENT,
57+
time_cast DATETIME NOT NULL,
58+
vote_value VARCHAR(6) NOT NULL,
59+
PRIMARY KEY (vote_id)
60+
);";
61+
62+
$this->connection->exec($sql);
63+
}
6064
}
6165

6266
/**

cloud_sql/mysql/pdo/src/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
if ($cloud_sql_connection_name) {
5353
// Connect using UNIX sockets
5454
$dsn = sprintf(
55-
'mysql:dbname=%s;unix_socket=/cloudsql/%s',
55+
'mysql:dbname=%s;unix_socket=/Users/jdp/cloudsql/%s',
5656
$dbName,
5757
$cloud_sql_connection_name
5858
);
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\CloudSQL\MySQL\Tests;
19+
20+
use Google\Cloud\Samples\CloudSQL\MySQL\Votes;
21+
use PDO;
22+
use PDOException;
23+
use PDOStatement;
24+
use PHPUnit\Framework\TestCase;
25+
use Prophecy\Argument;
26+
use RuntimeException;
27+
28+
class VotesTest extends TestCase
29+
{
30+
private $conn;
31+
32+
public function setUp() : void
33+
{
34+
$this->conn = $this->prophesize(PDO::class);
35+
}
36+
37+
public function testCreateTableIfNotExistsTableExists()
38+
{
39+
$stmt = $this->prophesize(PDOStatement::class);
40+
$stmt->execute()->shouldBeCalled();
41+
42+
$this->conn->prepare('SELECT 1 FROM votes')
43+
->shouldBeCalled()
44+
->willReturn($stmt->reveal());
45+
46+
$this->conn->exec(Argument::any())->shouldNotBeCalled();
47+
48+
$votes = new Votes($this->conn->reveal());
49+
$votes->createTableIfNotExists();
50+
}
51+
52+
public function testCreateTableIfNotExistsTableDoesNotExist()
53+
{
54+
$stmt = $this->prophesize(PDOStatement::class);
55+
$stmt->execute()->shouldBeCalled()->willThrow(
56+
new PDOException('foo')
57+
);
58+
59+
$this->conn->prepare('SELECT 1 FROM votes')
60+
->shouldBeCalled()
61+
->willReturn($stmt->reveal());
62+
63+
$this->conn->exec(Argument::containingString('CREATE TABLE votes'))
64+
->shouldBeCalled();
65+
66+
$votes = new Votes($this->conn->reveal());
67+
$votes->createTableIfNotExists();
68+
}
69+
70+
public function testListVotes()
71+
{
72+
$rows = [
73+
['foo' => 'bar']
74+
];
75+
76+
$stmt = $this->prophesize(PDOStatement::class);
77+
$stmt->execute()->shouldBeCalled();
78+
$stmt->fetchAll(PDO::FETCH_ASSOC)->shouldBeCalled()
79+
->willReturn($rows);
80+
81+
$this->conn->prepare(Argument::type('string'))
82+
->shouldBeCalled()
83+
->willReturn($stmt->reveal());
84+
85+
$votes = new Votes($this->conn->reveal());
86+
87+
$this->assertEquals($rows, $votes->listVotes());
88+
}
89+
90+
public function testGetCountByValue()
91+
{
92+
$val = 'TABS';
93+
$res = 10;
94+
95+
$stmt = $this->prophesize(PDOStatement::class);
96+
$stmt->execute([$val])
97+
->shouldBeCalled();
98+
99+
$stmt->fetch(PDO::FETCH_COLUMN)
100+
->shouldBeCalled()
101+
->willReturn((string) $res);
102+
103+
$this->conn->prepare(Argument::containingString('SELECT COUNT(vote_id)'))
104+
->shouldBeCalled()
105+
->willReturn($stmt->reveal());
106+
107+
$votes = new Votes($this->conn->reveal());
108+
109+
$this->assertEquals($res, $votes->getCountByValue($val));
110+
}
111+
112+
public function testInsertVote()
113+
{
114+
$val = 'TABS';
115+
116+
$stmt = $this->prophesize(PDOStatement::class);
117+
$stmt->bindParam('voteValue', $val)
118+
->shouldBeCalled();
119+
120+
$stmt->execute()->shouldBeCalled()->willReturn(true);
121+
122+
$this->conn->prepare(Argument::containingString('INSERT INTO votes'))
123+
->shouldBeCalled()
124+
->willReturn($stmt->reveal());
125+
126+
$votes = new Votes($this->conn->reveal());
127+
$this->assertTrue($votes->insertVote($val));
128+
}
129+
130+
public function testInsertVoteFailed()
131+
{
132+
$this->expectException(RuntimeException::class);
133+
134+
$val = 'TABS';
135+
136+
$stmt = $this->prophesize(PDOStatement::class);
137+
$stmt->bindParam('voteValue', $val)
138+
->shouldBeCalled();
139+
140+
$stmt->execute()->shouldBeCalled()
141+
->willThrow(new PDOException('Op failed'));
142+
143+
$this->conn->prepare(Argument::containingString('INSERT INTO votes'))
144+
->shouldBeCalled()
145+
->willReturn($stmt->reveal());
146+
147+
$votes = new Votes($this->conn->reveal());
148+
$votes->insertVote($val);
149+
}
150+
}

0 commit comments

Comments
 (0)