From b86f5a14f7815622342002a2b4bff71a10de1bd7 Mon Sep 17 00:00:00 2001
From: Alexander Makarov <sam@rmcreative.ru>
Date: Tue, 6 May 2014 16:56:51 +0400
Subject: [PATCH] #2435 Tests for integrity exception

---
 framework/db/IntegrityException.php     | 25 +++++++++++++++++++++++++
 tests/unit/framework/db/CommandTest.php | 12 ++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 framework/db/IntegrityException.php

diff --git a/framework/db/IntegrityException.php b/framework/db/IntegrityException.php
new file mode 100644
index 0000000..8489926
--- /dev/null
+++ b/framework/db/IntegrityException.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright (c) 2008 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+namespace yii\db;
+
+/**
+ * Exception represents an exception that is caused by violation of DB constraints.
+ *
+ * @author Alexander Makarov <sam@rmcreative.ru>
+ * @since 2.0
+ */
+class IntegrityException extends Exception
+{
+    /**
+     * @return string the user-friendly name of this exception
+     */
+    public function getName()
+    {
+        return 'Integrity constraint violation';
+    }
+}
diff --git a/tests/unit/framework/db/CommandTest.php b/tests/unit/framework/db/CommandTest.php
index 03f9589..0bb51bf 100644
--- a/tests/unit/framework/db/CommandTest.php
+++ b/tests/unit/framework/db/CommandTest.php
@@ -287,4 +287,16 @@ class CommandTest extends DatabaseTestCase
     public function testDropIndex()
     {
     }
+
+    public function testIntegrityViolation()
+    {
+        $this->setExpectedException('\yii\db\IntegrityException');
+
+        $db = $this->getConnection();
+
+        $sql = 'INSERT INTO profile(id, description) VALUES (123, \'duplicate\')';
+        $command = $db->createCommand($sql);
+        $command->execute();
+        $command->execute();
+    }
 }
--
libgit2 0.27.1