|
| 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