<?php require_once '1trainSongs.php'; require_once '1trainArtists.php'; require_once '1trainUsers.php'; $songid = $_REQUEST['id']; $artistid = $_REQUEST['user_id']; $title = $_REQUEST['title']; $artist = $_REQUEST['user']['username']; $art = $_REQUEST['artwork_url']; $avatar = $_REQUEST['user']['avatar_url']; $user_id = $_REQUEST['whoLikedID']; $profile = $_REQUEST['whoLiked']; Artists::create($artistid, $artist); Users::create($user_id, $profile); if ($art == "") { $avatar = explode("large", $avatar)[0] . "crop.jpg"; //gets a higher quality image Songs::create($songid, $title, $artistid, $avatar, $user_id); } elseif ($art == "https://sndcdn.com/images/default_avatar_crop.jpg") { //if no song art or user avatar $art = "photoshop/carousel-headphones-black-red.png"; Songs::create($songid, $title, $artistid, $art, $user_id); } else { $art = explode("large", $art)[0] . "crop.jpg"; //gets a higher quality image Songs::create($songid, $title, $artistid, $art, $user_id); } header('Content-type: application/json'); print "success from uploadToDB"; exit;
public function testCollections() { if (!class_exists('Mongo')) { $this->markTestSkipped("Mongo class does not exist, test skipped"); return; } Phalcon\DI::reset(); $di = new Phalcon\DI(); $di->set('mongo', function () { $mongo = new MongoClient(); return $mongo->phalcon_test; }); $di->set('collectionManager', function () { return new Phalcon\Mvc\Collection\Manager(); }); $songs = Songs::find(); $this->assertTrue(is_array($songs)); foreach ($songs as $song) { $this->assertTrue($song->delete()); } $song = new Songs(); $song->artist = 'Radiohead'; $song->name = 'Lotus Flower'; $success = $song->save(); $this->assertTrue($success); $this->assertInstanceOf('MongoId', $song->_id); $firstSongId = $song->_id; $songs = Songs::find(); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 1); $this->assertEquals($songs[0]->name, 'Lotus Flower'); $this->assertEquals($songs[0]->artist, 'Radiohead'); $song = new Songs(); $song->artist = 'Massive Attack'; $song->name = 'Teardrop'; $success = $song->save(); $this->assertTrue($success); $this->assertInstanceOf('MongoId', $song->_id); $this->assertNotEquals((string) $firstSongId->{'$id'}, (string) $song->_id->{'$id'}); $secondSongId = $song->_id; $songs = Songs::find(); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 2); $this->assertEquals($songs[0]->name, 'Lotus Flower'); $this->assertEquals($songs[0]->artist, 'Radiohead'); $this->assertEquals($songs[1]->name, 'Teardrop'); $this->assertEquals($songs[1]->artist, 'Massive Attack'); $song = new Songs(); $song->artist = 'Massive Attack'; $song->name = 'Paradise Circus'; $success = $song->save(); $this->assertTrue($success); $this->assertInstanceOf('MongoId', $song->_id); $this->assertNotEquals((string) $firstSongId->{'$id'}, (string) $song->_id->{'$id'}); $this->assertNotEquals((string) $secondSongId->{'$id'}, (string) $song->_id->{'$id'}); $songs = Songs::find(); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 3); $song = Songs::findFirst(); $this->assertInstanceOf('Songs', $song); $this->assertEquals($song->name, 'Lotus Flower'); $this->assertEquals($song->artist, 'Radiohead'); $song = Songs::findFirst(array(array('artist' => 'Massive Attack'))); $this->assertInstanceOf('Songs', $song); $this->assertEquals($song->artist, 'Massive Attack'); $song = Songs::findFirst(array('conditions' => array('artist' => 'Massive Attack'))); $this->assertInstanceOf('Songs', $song); $this->assertEquals($song->artist, 'Massive Attack'); $song = Songs::findFirst(array('conditions' => array('name' => 'Paradise Circus'))); $this->assertInstanceOf('Songs', $song); $this->assertEquals($song->name, 'Paradise Circus'); //No results $song = Songs::findFirst(array(array('artist' => 'Lana'))); $this->assertFalse($song); $song = Songs::findFirst(array('conditions' => array('artist' => 'Lana'))); $this->assertFalse($song); $song = Songs::findFirst(array(array('artist' => 'Lana'))); $this->assertFalse($song); //Passing parameters to find $songs = Songs::find(array(array('artist' => 'Massive Attack'))); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 2); $this->assertEquals($songs[0]->name, 'Teardrop'); $this->assertEquals($songs[1]->name, 'Paradise Circus'); $songs = Songs::find(array('conditions' => array('artist' => 'Massive Attack'))); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 2); $this->assertEquals($songs[0]->name, 'Teardrop'); $this->assertEquals($songs[1]->name, 'Paradise Circus'); $songs = Songs::find(array('conditions' => array('artist' => 'Massive Attack'), 'sort' => array('name' => 1))); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 2); $this->assertEquals($songs[0]->name, 'Paradise Circus'); $this->assertEquals($songs[1]->name, 'Teardrop'); $songs = Songs::find(array('conditions' => array('artist' => 'Massive Attack'), 'sort' => array('name' => 1), 'limit' => 1)); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 1); $this->assertEquals($songs[0]->name, 'Paradise Circus'); $songs = Songs::find(array('conditions' => array('artist' => 'Massive Attack'), 'limit' => 1)); $this->assertTrue(is_array($songs)); $this->assertEquals(count($songs), 1); $this->assertEquals($songs[0]->name, 'Teardrop'); //Find first $song = Songs::findFirst(array(array('artist' => 'Massive Attack'))); $this->assertInstanceOf('Songs', $song); $this->assertEquals($song->name, 'Teardrop'); $song = Songs::findFirst(array('conditions' => array('artist' => 'Massive Attack'))); $this->assertInstanceOf('Songs', $song); $this->assertEquals($song->name, 'Teardrop'); //Count $this->assertEquals(Songs::count(), 3); $this->assertEquals(Songs::count(array(array('artist' => 'Massive Attack'))), 2); //Create $song = new Songs(); $song->artist = 'Massive Attack'; $song->name = 'Paradise Circus'; $success = $song->create(); $this->assertTrue($success); //Update $song = new Songs(); $song->artist = 'Massive Attack'; $song->name = 'Paradise Circus'; $success = $song->update(); $this->assertFalse($success); $song = Songs::findFirst(); $song->artist = 'Massive Attack'; $song->name = 'Paradise Circus'; $success = $song->update(); $this->assertTrue($success); }