driver = $this->getMockForAbstractClass(Driver::class); } /** * Test if building the object throws an exception if we're not passing * required config data. * * @expectedException \InvalidArgumentException * @expectedExceptionMessage Please pass "username" instead of "login" for connecting to the database * @return void */ public function testConstructorException() { $arg = ['login' => 'Bear']; $this->getMockForAbstractClass(Driver::class, [$arg]); } /** * Test the constructor. * * @return void */ public function testConstructor() { $arg = ['quoteIdentifiers' => true]; $driver = $this->getMockForAbstractClass(Driver::class, [$arg]); $this->assertTrue($driver->autoQuoting()); $arg = ['username' => 'GummyBear']; $driver = $this->getMockForAbstractClass(Driver::class, [$arg]); $this->assertFalse($driver->autoQuoting()); } /** * Test supportsSavePoints(). * * @return void */ public function testSupportsSavePoints() { $result = $this->driver->supportsSavePoints(); $this->assertTrue($result); } /** * Test supportsQuoting(). * * @return void */ public function testSupportsQuoting() { $result = $this->driver->supportsQuoting(); $this->assertTrue($result); } /** * Test schemaValue(). * Uses a provider for all the different values we can pass to the method. * * @dataProvider schemaValueProvider * @return void */ public function testSchemaValue($input, $expected) { $result = $this->driver->schemaValue($input); $this->assertSame($expected, $result); } /** * Test schemaValue(). * Asserting that quote() is being called because none of the conditions were met before. * * @return void */ public function testSchemaValueConnectionQuoting() { $value = 'string'; $this->driver->_connection = $this->getMockBuilder(Mysql::class) ->disableOriginalConstructor() ->setMethods(['quote']) ->getMock(); $this->driver->_connection ->expects($this->once()) ->method('quote') ->with($value, PDO::PARAM_STR); $this->driver->schemaValue($value); } /** * Test lastInsertId(). * * @return void */ public function testLastInsertId() { $connection = $this->getMockBuilder(Mysql::class) ->disableOriginalConstructor() ->setMethods(['lastInsertId']) ->getMock(); $connection ->expects($this->once()) ->method('lastInsertId') ->willReturn('all-the-bears'); $this->driver->_connection = $connection; $this->assertSame('all-the-bears', $this->driver->lastInsertId()); } /** * Test isConnected(). * * @return void */ public function testIsConnected() { $this->driver->_connection = 'connection'; $this->assertTrue($this->driver->isConnected()); $this->driver->_connection = null; $this->assertFalse($this->driver->isConnected()); } /** * test autoQuoting(). * * @return void */ public function testAutoQuoting() { $this->assertFalse($this->driver->autoQuoting()); $this->driver->autoQuoting(true); $this->assertTrue($this->driver->autoQuoting()); $this->assertTrue($this->driver->autoQuoting(true)); $this->assertFalse($this->driver->autoQuoting(false)); $this->assertTrue($this->driver->autoQuoting('string')); $this->assertFalse($this->driver->autoQuoting('0')); $this->assertTrue($this->driver->autoQuoting(1)); $this->assertFalse($this->driver->autoQuoting(0)); } /** * Test compileQuery(). * * @return void */ public function testCompileQuery() { $compiler = $this->getMockBuilder(QueryCompiler::class) ->setMethods(['compile']) ->getMock(); $compiler ->expects($this->once()) ->method('compile') ->willReturn(true); $driver = $this->getMockBuilder(Driver::class) ->setMethods(['newCompiler', 'queryTranslator']) ->getMockForAbstractClass(); $driver ->expects($this->once()) ->method('newCompiler') ->willReturn($compiler); $driver ->expects($this->once()) ->method('queryTranslator') ->willReturn(function ($query) { return $query; }); $query = $this->getMockBuilder(Query::class) ->disableOriginalConstructor() ->getMock(); $result = $driver->compileQuery($query, new ValueBinder); $this->assertInternalType('array', $result); $this->assertSame($query, $result[0]); $this->assertTrue($result[1]); } /** * Test newCompiler(). * * @return void */ public function testNewCompiler() { $this->assertInstanceOf(QueryCompiler::class, $this->driver->newCompiler()); } /** * Test __destruct(). * * @return void */ public function testDestructor() { $this->driver->_connection = true; $this->driver->__destruct(); $this->assertNull($this->driver->_connection); } /** * Data provider for testSchemaValue(). * * @return array */ public function schemaValueProvider() { return [ [null, 'NULL'], [false, 'FALSE'], [true, 'TRUE'], [1, 1], ['0', '0'], ['42', '42'] ]; } }