PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/database/Mvc/Model/FindFirstCest.php

http://github.com/phalcon/cphalcon
PHP | 361 lines | 205 code | 50 blank | 106 comment | 0 complexity | 347f7a18579c97e8082e8eefac4e897d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Phalcon Framework.
  4. *
  5. * (c) Phalcon Team <team@phalcon.io>
  6. *
  7. * For the full copyright and license information, please view the LICENSE.txt
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Phalcon\Test\Database\Mvc\Model;
  12. use Codeception\Example;
  13. use DatabaseTester;
  14. use PDO;
  15. use Phalcon\Mvc\Model;
  16. use Phalcon\Mvc\Model\Exception;
  17. use Phalcon\Mvc\Model\Row;
  18. use Phalcon\Test\Fixtures\Migrations\InvoicesMigration;
  19. use Phalcon\Test\Fixtures\Migrations\StringPrimaryMigration;
  20. use Phalcon\Test\Fixtures\Traits\DiTrait;
  21. use Phalcon\Test\Models\Invoices;
  22. use Phalcon\Test\Models\InvoicesExtended;
  23. use Phalcon\Test\Models\InvoicesMap;
  24. use Phalcon\Test\Models\ModelWithStringPrimary;
  25. use function uniqid;
  26. /**
  27. * Class FindFirstCest
  28. */
  29. class FindFirstCest
  30. {
  31. use DiTrait;
  32. public function _before(DatabaseTester $I)
  33. {
  34. $this->setNewFactoryDefault();
  35. $this->setDatabase($I);
  36. /** @var PDO $connection */
  37. $connection = $I->getConnection();
  38. (new InvoicesMigration($connection));
  39. }
  40. /**
  41. * Tests Phalcon\Mvc\Model :: findFirst()
  42. *
  43. * @author Phalcon Team <team@phalcon.io>
  44. * @since 2020-02-01
  45. *
  46. * @group mysql
  47. * @group pgsql
  48. * @group sqlite
  49. */
  50. public function mvcModelFindFirst(DatabaseTester $I)
  51. {
  52. $I->wantToTest('Mvc\Model - findFirst()');
  53. $title = uniqid('inv-');
  54. /** @var PDO $connection */
  55. $connection = $I->getConnection();
  56. $migration = new InvoicesMigration($connection);
  57. $migration->insert(4, null, 0, $title);
  58. $invoice = Invoices::findFirst();
  59. $I->assertInstanceOf(
  60. Invoices::class,
  61. $invoice
  62. );
  63. $I->assertEquals(
  64. 4,
  65. $invoice->inv_id
  66. );
  67. $invoice = Invoices::findFirst(null);
  68. $I->assertInstanceOf(
  69. Invoices::class,
  70. $invoice
  71. );
  72. $I->assertEquals(
  73. 4,
  74. $invoice->inv_id
  75. );
  76. }
  77. /**
  78. * Tests Phalcon\Mvc\Model :: findFirst()
  79. *
  80. * @author Phalcon Team <team@phalcon.io>
  81. * @since 2020-02-01
  82. *
  83. * @group mysql
  84. * @group pgsql
  85. * @group sqlite
  86. */
  87. public function mvcModelFindFirstColumnMap(DatabaseTester $I)
  88. {
  89. $I->wantToTest('Mvc\Model - findFirst() - with column map');
  90. $title = uniqid('inv-');
  91. /** @var PDO $connection */
  92. $connection = $I->getConnection();
  93. $migration = new InvoicesMigration($connection);
  94. $migration->insert(4, null, 0, $title);
  95. $invoice = InvoicesMap::findFirst();
  96. $I->assertInstanceOf(
  97. InvoicesMap::class,
  98. $invoice
  99. );
  100. $I->assertEquals(
  101. 4,
  102. $invoice->id
  103. );
  104. $I->assertEquals(
  105. $title,
  106. $invoice->title
  107. );
  108. $invoice = InvoicesMap::findFirst(null);
  109. $I->assertInstanceOf(
  110. InvoicesMap::class,
  111. $invoice
  112. );
  113. $I->assertEquals(
  114. 4,
  115. $invoice->id
  116. );
  117. $I->assertEquals(
  118. $title,
  119. $invoice->title
  120. );
  121. }
  122. /**
  123. * Tests Phalcon\Mvc\Model :: findFirst() - not found
  124. *
  125. * @author Phalcon Team <team@phalcon.io>
  126. * @since 2020-02-01
  127. *
  128. * @group mysql
  129. * @group pgsql
  130. * @group sqlite
  131. */
  132. public function mvcModelFindFirstNotFound(DatabaseTester $I)
  133. {
  134. $I->wantToTest('Mvc\Model - findFirst() - not found');
  135. $robot = Invoices::findFirst(
  136. [
  137. 'conditions' => 'inv_id < 0',
  138. ]
  139. );
  140. $I->assertNull($robot);
  141. }
  142. /**
  143. * Tests Phalcon\Mvc\Model :: findFirstBy() - not found
  144. *
  145. * @author Phalcon Team <team@phalcon.io>
  146. * @since 2020-02-01
  147. *
  148. * @group mysql
  149. * @group pgsql
  150. * @group sqlite
  151. */
  152. public function mvcModelFindFirstByNotFound(DatabaseTester $I)
  153. {
  154. $I->wantToTest('Mvc\Model - findFirstBy() - not found');
  155. $I->assertNull(
  156. Invoices::findFirstByInvTitle('unknown')
  157. );
  158. }
  159. /**
  160. * Tests Phalcon\Mvc\Model :: findFirst() - extended
  161. *
  162. * @author Phalcon Team <team@phalcon.io>
  163. * @since 2020-02-01
  164. *
  165. * @group mysql
  166. * @group pgsql
  167. * @group sqlite
  168. */
  169. public function mvcModelFindFirstExtended(DatabaseTester $I)
  170. {
  171. $I->wantToTest('Mvc\Model - findFirst() - extended');
  172. $title = uniqid('inv-');
  173. /** @var PDO $connection */
  174. $connection = $I->getConnection();
  175. $migration = new InvoicesMigration($connection);
  176. $migration->insert(4, null, 0, $title);
  177. $invoice = InvoicesExtended::findFirst(4);
  178. $I->assertInstanceOf(
  179. InvoicesExtended::class,
  180. $invoice
  181. );
  182. $I->assertEquals(4, $invoice->inv_id);
  183. $invoice = InvoicesExtended::findFirst(0);
  184. $I->assertNull($invoice);
  185. }
  186. /**
  187. * Tests Phalcon\Mvc\Model :: findFirst() - exception
  188. *
  189. * @author Phalcon Team <team@phalcon.io>
  190. * @since 2020-02-01
  191. *
  192. * @group mysql
  193. * @group pgsql
  194. * @group sqlite
  195. */
  196. public function mvcModelFindFirstException(DatabaseTester $I)
  197. {
  198. $I->wantToTest('Mvc\Model - findFirst() - exception');
  199. $I->expectThrowable(
  200. new Exception(
  201. 'Parameters passed must be of type array, string, numeric or null'
  202. ),
  203. function () {
  204. Invoices::findFirst(false);
  205. }
  206. );
  207. }
  208. /**
  209. * Tests Phalcon\Mvc\Model :: findFirst() - option 'column'
  210. *
  211. * @author Phalcon Team <team@phalcon.io>
  212. * @since 2020-11-22
  213. *
  214. * @group mysql
  215. * @group pgsql
  216. * @group sqlite
  217. *
  218. * @param DatabaseTester $I
  219. */
  220. public function mvcModelFindFirstColumn(DatabaseTester $I): void
  221. {
  222. $I->wantToTest('Mvc\Model - findFirst() - column option');
  223. /** @var PDO $connection */
  224. $connection = $I->getConnection();
  225. $migration = new InvoicesMigration($connection);
  226. $migration->insert(4);
  227. $invoice = Invoices::findFirst([
  228. 'columns' => 'inv_id',
  229. ]);
  230. $I->assertInstanceOf(
  231. Row::class,
  232. $invoice
  233. );
  234. $I->assertEquals(
  235. 4,
  236. $invoice->inv_id
  237. );
  238. $I->assertEquals(
  239. ['inv_id' => 4],
  240. $invoice->toArray()
  241. );
  242. }
  243. /**
  244. * Tests Phalcon\Mvc\Model :: findFirst() - exception
  245. *
  246. * @dataProvider findFirstProvider
  247. *
  248. * @param DatabaseTester $I
  249. * @param Example $example
  250. *
  251. * @author Phalcon Team <team@phalcon.io>
  252. * @since 2020-01-27
  253. *
  254. * @group mysql
  255. * @group pgsql
  256. * @group sqlite
  257. */
  258. public function mvcModelFindFirstStringPrimaryKey(DatabaseTester $I, Example $example)
  259. {
  260. $I->wantToTest('Mvc\Model - findFirst() - string primary key');
  261. $connection = $I->getConnection();
  262. $migration = new StringPrimaryMigration($connection);
  263. $migration->insert(
  264. '5741bfd7-6870-40b7-adf6-cbacb515b9a9',
  265. 1
  266. );
  267. $migration->insert(
  268. '1c53079c-249e-0c63-af8d-52413bfa2a2b',
  269. 2
  270. );
  271. $model = ModelWithStringPrimary::findFirst($example['params']);
  272. $I->assertSame($example['found'], $model instanceof Model);
  273. }
  274. /**
  275. * @return array
  276. */
  277. protected function findFirstProvider(): array
  278. {
  279. return [
  280. [
  281. 'params' => [
  282. 'uuid = ?0',
  283. 'bind' => ['5741bfd7-6870-40b7-adf6-cbacb515b9a9'],
  284. ],
  285. 'found' => true,
  286. ],
  287. [
  288. 'params' => [
  289. 'uuid = ?0',
  290. 'bind' => ['1c53079c-249e-0c63-af8d-52413bfa2a2b'],
  291. ],
  292. 'found' => true,
  293. ],
  294. [
  295. 'params' => [
  296. 'uuid = ?0',
  297. 'bind' => ['1c53079c-249e-0c63-af8d-52413bfa2a2c'],
  298. ],
  299. 'found' => false,
  300. ],
  301. [
  302. 'params' => '134',
  303. 'found' => false,
  304. ],
  305. [
  306. 'params' => "uuid = '134'",
  307. 'found' => false,
  308. ],
  309. ];
  310. }
  311. }